{"id":1626,"date":"2012-08-27T16:00:00","date_gmt":"2012-08-27T16:00:00","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/2012\/10\/gradle-custom-plugin.html"},"modified":"2012-10-25T09:06:26","modified_gmt":"2012-10-25T09:06:26","slug":"gradle-custom-plugin","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2012\/08\/gradle-custom-plugin.html","title":{"rendered":"Gradle Custom Plugin"},"content":{"rendered":"<div dir=\"ltr\" style=\"text-align: left\">This tutorial describes the way of creating Gradle standalone custom plugin. It covers the following topics<\/p>\n<ul>\n<li>Creating task, and using it in Custom plugin<\/li>\n<li>Stand alone Custom plugin<\/li>\n<li>Short plugin id<\/li>\n<li>Customize Gradle setting using settings.gradle<\/li>\n<\/ul>\n<p><strong><u>Project info :<\/u><\/strong><br \/>\n<strong><br \/>\n<\/strong>Gradle version : 1.1<br \/>\nOS platform : Ubuntu 12.10<br \/>\nPrerequisite : Basic understanding of Gradle script.<br \/>\n<strong><br \/>\n<\/strong><strong>Creating the Stand alone custom plugin<\/strong>       <\/p>\n<ol>\n<li> create the directory structure\n<pre class=\"brush:java\">   |-custom-plugin\r\n   |  |-plugin\r\n   |    |-src\r\n   |      |-main\r\n   |      |  |-groovy\r\n   |      |  | |-com\r\n   |      |  |   |-code4reference \r\n   |      |  |     |-gradle\r\n   |      |  |-resources   \r\n   |      |  | |-META-INF\r\n   |      |  |   |-gradle-plugins \r\n   |      |-test\r\n   |      |  |-groovy\r\n   |      |  | |-com\r\n   |      |  |   |-code4reference \r\n   |      |  |     |-gradle \r\n   |-user<\/pre>\n<p>Here <strong><em>plugin<\/em><\/strong> directory contains all source code and resource files whereas the <strong><em>user<\/em><\/strong> directory contains the consumer script which uses custom plugin. Execute the following command to create the directory structure. Here <strong><em>groovy<\/em><\/strong> folder contains the source code package.  <\/p>\n<pre class=\"brush:bash\">  $ mkdir -p custom-plugin\/plugin\/src\/main\/groovy\/com\/code4reference\/gradle\r\n  $ mkdir -p custom-plugin\/plugin\/src\/main\/resources\/META-INF\/gradle-plugins\r\n  $ mkdir -p custom-plugin\/user\r\n  <\/pre>\n<\/li>\n<li> <strong>Custom plugin source code<\/strong>\n<p>Every plugin should have a implementation class to extend the <strong><em>Plugin<\/em><\/strong> class. Let\u2019s define the plugin class.  <\/p>\n<pre class=\"brush:java\">package com.code4reference.gradle;\r\n\r\nimport org.gradle.api.*;\r\n\r\nclass Code4ReferencePlugin implements Plugin {\r\n    def void apply(Project project) {\r\n        \/\/c4rTask task has been defined below.\r\n        project.task('c4rTask') &lt;&lt; {\r\n            println 'Hi from Code4Reference plugin!'\r\n        }\r\n    }\r\n}<\/pre>\n<p>Put this file in <strong><em>custom-plugin\/plugin\/src\/main\/groovy\/com\/code4reference\/gradle <\/em><\/strong> directory. Here, <strong><em>c4rTask<\/em><\/strong> task has been defined to print a simple line. <\/li>\n<li> <strong>Short plugin ID<\/strong>\n<p>In order to apply a plugin, we usually use a short ID e.g apply plugin : \u2018java\u2019. Here \u2018java\u2019 is the short plugin id for the class <em>org.gradle.api.plugins.JavaPlugin<\/em>. The short plugin id can be defined in easy steps. For this, we need to create a property file and put it in the <em>META-INF\/gradle-plugins<\/em> directory which comes under the class path. The name of the file will be our short id. This property file must contain the line shown below and it should point to the plugin implementation class. Let\u2019s create the property file as code4reference.properties and point it to the Code4ReferencePlugin class.  <\/p>\n<pre class=\"brush:bash\">   implementation-class=com.code4reference.gradle.Code4ReferencePlugin<\/pre>\n<\/li>\n<li> <strong>Gradle script to generate the plugin<\/strong>\n<p>For compiling and building this plugin, we will write the gradle script. Create the file named <strong><em>build.gradle<\/em><\/strong> in plugin directory and copy the content below in it.  <\/p>\n<pre class=\"brush:java\">apply plugin: 'groovy'\r\napply plugin: 'maven'\r\ndependencies {\r\n    compile gradleApi()\r\n    groovy localGroovy()\r\n}\r\nrepositories {\r\n    mavenCentral()\r\n}\r\n\r\ngroup='com.code4reference'   \/\/Group name makes easier to manager the packages.\r\nversion='1.1-SNAPSHOT'\r\n\r\nuploadArchives {\r\n    repositories {\r\n        mavenDeployer {\r\n            repository(url: uri('..\/repo'))\r\n        }\r\n    }\r\n}<\/pre>\n<p>In this gradle script, we use groovy plugin to compile groovy source code and declare gradleAPI as the compile time dependencies. You may have noticed that we use <strong><em>maven<\/em><\/strong> plugin. It basically creates the plugin jar file and stores in the maven repository. Here we create the maven repository named <strong><em>repo <\/em><\/strong> in the parent directory and store the jar file in it. <\/li>\n<li> <strong>Building plugin and putting in repository<\/strong>\n<pre class=\"brush:bash\"> $ gradle uploadArchives   #This will put the plugin-version.jar in maven repository.\r\n\r\n:compileJava UP-TO-DATE\r\n:compileGroovy UP-TO-DATE\r\n:processResources UP-TO-DATE\r\n:classes UP-TO-DATE\r\n:jar\r\n:uploadArchives\r\nUploading: com\/code4reference\/plugin\/1.1-SNAPSHOT\/plugin-1.1-20120816.163101-1.jar to repository remote at file:\/home\/rakesh\/programming\/mygitrepo\/Code4Reference\/GradleExample\/custom-plugin-1\/repo\/\r\nTransferring 5K from remote\r\nUploaded 5K\r\n\r\nBUILD SUCCESSFUL\r\n\r\nTotal time: 34.892 secs<\/pre>\n<\/li>\n<li> <strong>Porject settings using settings.gradle<\/strong>\n<p>When the above command is executed, gradle tires to get the project name from the settings.gradle. If settings.gradle file is not present in the current directory, then it gets the name of the current directory and assumes it as the project name.  It then forms the path to store the jar file. The file path convention is as following <strong><em> \/group\/name\/projectName\/version\/projectname-version-timestamp.jar<\/em><\/strong>. You may notice in the above output that the jar path name and the jar file name have plugin word because the the current directory name is plugin and gradle assumes it as project name. If we want to override this property and put <em><strong>code4ReferencePlugin<\/strong><\/em> as the project name, we need to create a <strong><em>settings.gradle <\/em><\/strong>file in the plugin directory and put the following line.  <\/p>\n<pre class=\"brush:java\">rootProject.name = 'code4ReferencePlugin'<\/pre>\n<p>Now again execute the command to generate the plugin jar file.  <\/p>\n<pre class=\"brush:bash\">$gradle uploadArchives\r\ncompileJava UP-TO-DATE\r\n:compileGroovy UP-TO-DATE\r\n:processResources UP-TO-DATE\r\n:classes UP-TO-DATE\r\n:jar UP-TO-DATE\r\n:uploadArchives\r\nUploading: com\/code4reference\/code4ReferencePlugin\/1.1-SNAPSHOT\/code4ReferencePlugin-1.1-20120816.164441-5.jar to repository remote at file:\/home\/rakesh\/programming\/mygitrepo\/Code4Reference\/GradleExample\/custom-plugin-1\/repo\/\r\nTransferring 5K from remote\r\nUploaded 5K\r\n\r\nBUILD SUCCESSFUL\r\n\r\nTotal time: 8.61 secs<\/pre>\n<p>Now the problem is solved. The jar is getting generated with name <strong><em>code4ReferencePlugin-[version]-timestamp.jar . <\/em><\/strong><em><\/em>If you want to find more about the gradle and system properties, find it <a href=\"http:\/\/gradle.org\/docs\/current\/userguide\/tutorial_this_and_that.html\" rel=\"nofollow\" title=\"gradle and system properties\">here<\/a>. <\/li>\n<\/ol>\n<p><strong>Using the custom plugin<\/strong>       <div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p>This is really a simple step. Although we use the other plugin, the custom plugin can also be used in similar way. Now create another <strong><em>build.gradle <\/em><\/strong>file in user directory and copy the code given below.        <\/p>\n<pre class=\"brush:java\">buildscript {\r\n    repositories {\r\n        maven {\r\n            url uri('..\/repo')\r\n        }\r\n    }\r\n    dependencies {\r\n        classpath group: 'com.code4reference',\r\n                  name: 'code4ReferencePlugin',\r\n                  version: '1.1-SNAPSHOT'\r\n    }\r\n}\r\napply plugin: 'code4reference'<\/pre>\n<p><em><strong>build.gradle<\/strong><\/em> script accesses maven repository present in the parent directory. We have also defined dependency which basically accesses the particular version of jar file from the maven. Last but not the least, we apply the short plugin id \u201ccode4reference\u201d. To run this gradle script, execute the command below on the terminal in the <em><strong>user<\/strong><\/em> directory.        <\/p>\n<pre class=\"brush:bash\">$ gradle c4rTask   #Remember we have created c4rTask in Code4ReferencePlugin class.\r\n                   #You will get the following output.\r\n:c4rTask\r\nHi from Code4Reference plugin!\r\n\r\nBUILD SUCCESSFUL\r\n\r\nTotal time: 3.908 secs<\/pre>\n<p>Voil\u00e0!! you just created custom plugin and used it in a different project script. You can find the source code for this <a href=\"https:\/\/github.com\/rakeshcusat\/Code4Reference\/tree\/master\/GradleExample\/custom-plugin-1\" rel=\"nofollow\" title=\"Gradle custom plugin source code.\">tutorial over here<\/a>.                   <strong><a href=\"http:\/\/code4reference.com\/\">Code4Reference<\/a><\/strong><\/p>\n<p>Now, will cover the following topics.<\/p>\n<ul>\n<li>Define custom Task class<\/li>\n<li>Passing arguments to custom plugin task<\/li>\n<li>Nested arguments<\/li>\n<li><strong>Testing <\/strong>the custom plugin<\/li>\n<\/ul>\n<p><strong><u>Project info :<\/u><\/strong><br \/>\nProject name : Gradle custom plugin<br \/>\nGradle version : 1.1<br \/>\nOS platform : Ubuntu 12.10<br \/>\n<strong>Prerequisite : Basic understanding of Gradle script.<\/strong><\/p>\n<p>Here, we will follow the same directory hierarchy listed in the first part.        <\/p>\n<ol>\n<li> <strong>Define custom Task<\/strong>\n<p>Let\u2019s define a custom class named Code4ReferenceTask which extends DefaultTask class and put this file in the same folder where Code4ReferencePlugin.groovy is kept. This class contains a method named showMessage() which is annotated with <strong><em>@TaskAction<\/em><\/strong>. Gradle calls this method when the task is executed.  <\/p>\n<pre class=\"brush:java\">package com.code4reference.gradle;\r\n\r\nimport org.gradle.api.DefaultTask\r\nimport org.gradle.api.tasks.TaskAction\r\n\r\nclass Code4ReferenceTask extends DefaultTask {\r\n\r\n    @TaskAction\r\n    def showMessage() {\r\n        println '----------showMessage-------------'\r\n    }\r\n}\r\n<\/pre>\n<p>&nbsp;Now we need to do some minor modifications in the Code4ReferencePlugin.groovy to include the custom task. The modified Code4ReferencePlugin class is as following.  <\/p>\n<pre class=\"brush:java\">package com.code4reference.gradle;\r\n\r\nimport org.gradle.api.*;\r\n\r\nclass Code4ReferencePlugin implements Plugin {\r\n    def void apply(Project project) {\r\n           \/\/Define the task named c4rTask of type Code4ReferenceTask\r\n           project.task('c4rTask', type: Code4ReferenceTask)\r\n    }\r\n}<\/pre>\n<p>You may notice that only the highlighted line has been changed from the past implementation. Now the \u201cc4rTask\u201d is of Code4ReferenceTask type. Execute the <strong><em>gradle uploadArchives<\/em><\/strong> command in the <strong><em>plugin<\/em><\/strong> directory. This will update the jar file in Maven repo. Now execute the command below in <strong><em>user<\/em><\/strong> directory with the same old build.gradle. We will get the following output.  <\/p>\n<pre class=\"brush:bash\">$gradle c4rTask\r\n:c4rTask\r\n----------showMessage-------------\r\nBUILD SUCCESSFUL\r\n\r\nTotal time: 14.057 secs\r\n<\/pre>\n<\/li>\n<li> <strong>Passing arguments to custom plugin task<\/strong>\n<p>The above implementation is the simplest one and doesn\u2019t do much. What if we want to pass the arguments from Gradle script to this task? We can achieve it by accessing <strong><em>extension object<\/em><\/strong>. The Gradle Project has an associated ExtensionContainer object that helps keep track of all the settings and properties being passed to plugins class. Let\u2019s define an extension class which can hold the arguments and pass those to the Task class. The highlighted lines in the Code4ReferencePlugin class help to pass the arguments to the Task class.  <\/p>\n<pre class=\"brush:java\">package com.code4reference.gradle;\r\n\r\nimport org.gradle.api.*;\r\n\r\n\/\/For passing arguments from gradle script.\r\nclass Code4ReferencePluginExtension {\r\n    String message = 'Hello from Code4Reference'\r\n    String sender = 'Code4Reference'\r\n}\r\nclass Code4ReferencePlugin implements Plugin {\r\n    def void apply(Project project) {\r\n           project.extensions.create('c4rArgs', Code4ReferencePluginExtension)\r\n           project.task('c4rTask', type: Code4ReferenceTask)\r\n    }\r\n}<\/pre>\n<p>We have defined Code4ReferencePluginExtension as Extension class which contains two variables message and sender. These serve as the arguments for the custom defined task. We need to modify the Code4RefernceTask class to access the arguments. The highlighted lines have been added to the previous Code4ReferenceTask class implementation.  <\/p>\n<pre class=\"brush:java\">package com.code4reference.gradle;\r\n\r\nimport org.gradle.api.DefaultTask\r\nimport org.gradle.api.tasks.TaskAction\r\n\r\nclass Code4ReferenceTask extends DefaultTask {\r\n\r\n    @TaskAction\r\n    def showMessage() {\r\n        println '------------showMessage-------------------'\r\n        println 'From : ${project.c4rArgs.sender},\\\r\n                  message : ${project.c4rArgs.message}'\r\n    }\r\n}<\/pre>\n<p>Execute the <strong><em>gradle uploadArchives<\/em><\/strong> command in the plugin directory. This will update the jar file in Maven repo. Also, we need to update the build.gradle in the <strong><i>user<\/i><\/strong> directory.  <\/p>\n<pre class=\"brush:java\">\/\/custom-plugin-2\/user\r\nbuildscript {\r\n    repositories {\r\n        maven {\r\n            url uri('..\/repo')\r\n        }\r\n    }\r\n    dependencies {\r\n        classpath group: 'com.code4reference',\r\n                  name: 'code4ReferencePlugin',\r\n                  version: '1.2-SNAPSHOT'\r\n    }\r\n}\r\n\r\napply plugin: 'code4reference'\r\n\r\nc4rArgs {\r\n    sender = 'Rakesh'\r\n    message = 'Hello there !!!!'\r\n}\r\n<\/pre>\n<p>You may have noticed that c4rArgs closure has been added and sender and message variables are set in the closure. These two variables are accessible in the showMessage() method. Now run the build.gradle present in user directory. we get the following output.  <\/p>\n<pre class=\"brush:bash\">$gradle c4rTask\r\n:c4rTask\r\n-------------------------showMessage-----------------------------\r\nFrom : Rakesh, message : Hello there !!!!\r\n\r\nBUILD SUCCESSFUL\r\n\r\nTotal time: 15.817 secs\r\n<\/pre>\n<\/li>\n<li> <strong>Nested arguments<\/strong>\n<p>What if we want to pass the nested arguments? We can achieve this by nesting the Extension objects. Here is the code for Code4ReferencePlugin class. Only highlighted lines have been added in this class.  <\/p>\n<pre class=\"brush:java\">package com.code4reference.gradle;\r\n\r\nimport org.gradle.api.*;\r\n\r\n\/\/Extension class for nested argumetns\r\nclass C4RNestedPluginExtention {\r\n     String receiver = 'Admin'\r\n     String email = 'admin@code4reference.com'\r\n\r\n }\r\n\/\/For keeping passing arguments from gradle script.\r\nclass Code4ReferencePluginExtension {\r\n    String message = 'Hello from Code4Reference'\r\n    String sender = 'Code4Reference'\r\n    C4RNestedPluginExtention nested = new C4RNestedPluginExtention()\r\n}\r\nclass Code4ReferencePlugin implements Plugin {\r\n    def void apply(Project project) {\r\n           project.extensions.create('c4rArgs', Code4ReferencePluginExtension)\r\n           project.c4rArgs.extensions.create('nestedArgs',C4RNestedPluginExtention)\r\n           project.task('c4rTask', type: Code4ReferenceTask)\r\n    }\r\n}\r\n<\/pre>\n<p>It\u2019s time to modify the Code4ReferenceTask class as well. Highlighted lines have been added in this class to access the nested arguments.  <\/p>\n<pre class=\"brush:java\">package com.code4reference.gradle;\r\n\r\nimport org.gradle.api.DefaultTask\r\nimport org.gradle.api.tasks.TaskAction\r\n\r\nclass Code4ReferenceTask extends DefaultTask {\r\n\r\n    @TaskAction\r\n    def showMessage() {\r\n        println '------------showMessage-------------------'\r\n        println 'From : ${project.c4rArgs.sender},\\\r\n                 message : ${project.c4rArgs.message}'\r\n        println 'To : ${project.c4rArgs.nestedArgs.receiver},\\\r\n                 email : ${project.c4rArgs.nestedArgs.email}'\r\n    }\r\n}<\/pre>\n<p>Execute the <strong><em>gradle uploadArchives<\/em><\/strong> command again in the plugin directory to update the jar file in Maven repo. Now modify the build.gradle file present in user directory to pass the nested arguments.  <\/p>\n<pre class=\"brush:java\">buildscript {\r\n    repositories {\r\n        maven {\r\n            url uri('..\/repo')\r\n        }\r\n    }\r\n    dependencies {\r\n        classpath group: 'com.code4reference',\r\n                  name: 'code4ReferencePlugin',\r\n                  version: '1.2-SNAPSHOT'\r\n    }\r\n}\r\n\r\napply plugin: 'code4reference'\r\n\r\nc4rArgs {\r\n    sender = 'Rakesh'\r\n    message = 'Hello there !!!!'\r\n\r\n    nestedArgs{\r\n       receiver = 'gradleAdmin'\r\n       email = 'gradleAdmin@code4reference.com'\r\n    }\r\n}\r\n<\/pre>\n<p>We have added the highlighted line in the build.gradle file. <\/li>\n<li> <strong>Testing plugin and task<\/strong>\n<p>Testing of code is an important aspect of code development. Now we are going to add the unit test for the custom task and plugin. For this, we need to create the directory structure for the test classes. We need to put the test folder in the src directory. Execute the command below in plugin directory to create the test directories.  <\/p>\n<pre class=\"brush:bash\">$mkdir -p src\/test\/groovy\/com\/code4reference\/gradle\/<\/pre>\n<p>Test directory structure follows the same package directory structure which has been used for source code package directory. In this directory, put the test classes for Code4ReferencePlugin and Code4ReferenceTask. In test class, ProjectBuilder is used to access the project object. These test cases are easy to write, similar to the Junit test cases. The code of test classes is as following:  <\/p>\n<pre class=\"brush:java\">package com.code4reference.gradle;\r\n\r\nimport org.junit.Test\r\nimport org.gradle.testfixtures.ProjectBuilder\r\nimport org.gradle.api.Project\r\nimport static org.junit.Assert.*\r\n\r\nclass Code4ReferenceTaskTest {\r\n    @Test\r\n    public void canAddTaskToProject() {\r\n        Project project = ProjectBuilder.builder().build()\r\n        def task = project.task('c4rtakstest', type: Code4ReferenceTask)\r\n        assertTrue(task instanceof Code4ReferenceTask)\r\n    }\r\n}\r\n<\/pre>\n<pre class=\"brush:java\">package com.code4reference.gradle;\r\n\r\nimport org.junit.Test\r\nimport org.gradle.testfixtures.ProjectBuilder\r\nimport org.gradle.api.Project\r\nimport static org.junit.Assert.*\r\n\r\nclass Code4ReferencePluginTest {\r\n    @Test\r\n    public void code4referencePluginAddsCode4ReferenceTaskToProject() {\r\n        Project project = ProjectBuilder.builder().build()\r\n        project.apply plugin: 'code4reference'\r\n        println 'code4referencePluginAddsCode4ReferenceTaskToProject'\r\n        assertTrue(project.tasks.c4rTask instanceof Code4ReferenceTask)\r\n    }\r\n}<\/pre>\n<p>To run the test, execute the following command in plugin folder.  <\/p>\n<pre class=\"brush:bash\">$gradle test                #For success test cases.\r\n:compileJava UP-TO-DATE\r\n:compileGroovy UP-TO-DATE\r\n:processResources UP-TO-DATE\r\n:classes UP-TO-DATE\r\n:compileTestJava UP-TO-DATE\r\n:compileTestGroovy\r\n:processTestResources UP-TO-DATE\r\n:testClasses\r\n:test\r\n\r\nBUILD SUCCESSFUL\r\n\r\nTotal time: 42.799 secs\r\n\r\n$gradle test    #In case of test case failure,\r\n                #you can expect output similar to given below.\r\n:compileJava UP-TO-DATE\r\n:compileGroovy UP-TO-DATE\r\n:processResources UP-TO-DATE\r\n:classes UP-TO-DATE\r\n:compileTestJava UP-TO-DATE\r\n:compileTestGroovy\r\n:processTestResources UP-TO-DATE\r\n:testClasses\r\n:test\r\n\r\ncom.code4reference.gradle.Code4ReferencePluginTest &gt; code4referencePluginAddsCode4ReferenceTaskToProject FAILED\r\n    java.lang.AssertionError at Code4ReferencePluginTest.groovy:14\r\n\r\n2 tests completed, 1 failed\r\n\r\nFAILURE: Build failed with an exception.\r\n\r\n* What went wrong:\r\nExecution failed for task ':test'.\r\n&gt; There were failing tests. See the report at: file:\/\/\/home\/rakesh\/programming\/mygitrepo\/Code4Reference\/GradleExample\/custom-plugin-2\/plugin\/build\/reports\/tests\/index.html\r\n\r\n* Try:\r\nRun with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.\r\n\r\nBUILD FAILED\r\n<\/pre>\n<p>Gradle test provides the test report and its location. This file can be opened using any browser to examine the stack trace.<\/li>\n<\/ol>\n<p>You can find the &nbsp;<a href=\"https:\/\/github.com\/rakeshcusat\/Code4Reference\/tree\/master\/GradleExample\/custom-plugin-2\" rel=\"nofollow\" title=\"Gradle custom plugin part -2\">source code here<\/a>.<\/p>\n<p><strong><i>Reference: <\/i><\/strong><a href=\"http:\/\/code4reference.com\/2012\/08\/gradle-custom-plugin-part-1\/\">Gradle custom plugin (Part-1)<\/a>,<strong><i>&nbsp;<\/i><\/strong><a href=\"http:\/\/code4reference.com\/2012\/08\/gradle-custom-plugin-part-2\/\">Gradle custom plugin (Part-2)<\/a>&nbsp;from our <a href=\"http:\/\/www.javacodegeeks.com\/p\/jcg.html\">JCG partner<\/a> Rakesh Cusat at the <a href=\"http:\/\/code4reference.com\/\">Code4Reference<\/a> blog.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial describes the way of creating Gradle standalone custom plugin. It covers the following topics Creating task, and using it in Custom plugin Stand alone Custom plugin Short plugin id Customize Gradle setting using settings.gradle Project info : Gradle version : 1.1 OS platform : Ubuntu 12.10 Prerequisite : Basic understanding of Gradle script. &hellip;<\/p>\n","protected":false},"author":274,"featured_media":129,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[484],"class_list":["post-1626","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-enterprise-java","tag-gradle"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Gradle Custom Plugin - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"This tutorial describes the way of creating Gradle standalone custom plugin. It covers the following topics Creating task, and using it in Custom\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2012\/08\/gradle-custom-plugin.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Gradle Custom Plugin - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"This tutorial describes the way of creating Gradle standalone custom plugin. It covers the following topics Creating task, and using it in Custom\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2012\/08\/gradle-custom-plugin.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2012-08-27T16:00:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2012-10-25T09:06:26+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/gradle-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Rakesh Cusat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Rakesh Cusat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"12 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/08\\\/gradle-custom-plugin.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/08\\\/gradle-custom-plugin.html\"},\"author\":{\"name\":\"Rakesh Cusat\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/913ac3950434ad48c2c2b98e23d86a49\"},\"headline\":\"Gradle Custom Plugin\",\"datePublished\":\"2012-08-27T16:00:00+00:00\",\"dateModified\":\"2012-10-25T09:06:26+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/08\\\/gradle-custom-plugin.html\"},\"wordCount\":1384,\"commentCount\":2,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/08\\\/gradle-custom-plugin.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/gradle-logo.jpg\",\"keywords\":[\"Gradle\"],\"articleSection\":[\"Enterprise Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/08\\\/gradle-custom-plugin.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/08\\\/gradle-custom-plugin.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/08\\\/gradle-custom-plugin.html\",\"name\":\"Gradle Custom Plugin - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/08\\\/gradle-custom-plugin.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/08\\\/gradle-custom-plugin.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/gradle-logo.jpg\",\"datePublished\":\"2012-08-27T16:00:00+00:00\",\"dateModified\":\"2012-10-25T09:06:26+00:00\",\"description\":\"This tutorial describes the way of creating Gradle standalone custom plugin. It covers the following topics Creating task, and using it in Custom\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/08\\\/gradle-custom-plugin.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/08\\\/gradle-custom-plugin.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/08\\\/gradle-custom-plugin.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/gradle-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/gradle-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2012\\\/08\\\/gradle-custom-plugin.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Enterprise Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/enterprise-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Gradle Custom Plugin\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/913ac3950434ad48c2c2b98e23d86a49\",\"name\":\"Rakesh Cusat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/232ae7fa9c21c4a55e50bb934653939d45522ec03f0c2045ae3bb139faa275bb?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/232ae7fa9c21c4a55e50bb934653939d45522ec03f0c2045ae3bb139faa275bb?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/232ae7fa9c21c4a55e50bb934653939d45522ec03f0c2045ae3bb139faa275bb?s=96&d=mm&r=g\",\"caption\":\"Rakesh Cusat\"},\"sameAs\":[\"http:\\\/\\\/code4reference.com\\\/\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/Rakesh-Cusat\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Gradle Custom Plugin - Java Code Geeks","description":"This tutorial describes the way of creating Gradle standalone custom plugin. It covers the following topics Creating task, and using it in Custom","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/2012\/08\/gradle-custom-plugin.html","og_locale":"en_US","og_type":"article","og_title":"Gradle Custom Plugin - Java Code Geeks","og_description":"This tutorial describes the way of creating Gradle standalone custom plugin. It covers the following topics Creating task, and using it in Custom","og_url":"https:\/\/www.javacodegeeks.com\/2012\/08\/gradle-custom-plugin.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2012-08-27T16:00:00+00:00","article_modified_time":"2012-10-25T09:06:26+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/gradle-logo.jpg","type":"image\/jpeg"}],"author":"Rakesh Cusat","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Rakesh Cusat","Est. reading time":"12 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2012\/08\/gradle-custom-plugin.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2012\/08\/gradle-custom-plugin.html"},"author":{"name":"Rakesh Cusat","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/913ac3950434ad48c2c2b98e23d86a49"},"headline":"Gradle Custom Plugin","datePublished":"2012-08-27T16:00:00+00:00","dateModified":"2012-10-25T09:06:26+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2012\/08\/gradle-custom-plugin.html"},"wordCount":1384,"commentCount":2,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2012\/08\/gradle-custom-plugin.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/gradle-logo.jpg","keywords":["Gradle"],"articleSection":["Enterprise Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2012\/08\/gradle-custom-plugin.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2012\/08\/gradle-custom-plugin.html","url":"https:\/\/www.javacodegeeks.com\/2012\/08\/gradle-custom-plugin.html","name":"Gradle Custom Plugin - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2012\/08\/gradle-custom-plugin.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2012\/08\/gradle-custom-plugin.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/gradle-logo.jpg","datePublished":"2012-08-27T16:00:00+00:00","dateModified":"2012-10-25T09:06:26+00:00","description":"This tutorial describes the way of creating Gradle standalone custom plugin. It covers the following topics Creating task, and using it in Custom","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2012\/08\/gradle-custom-plugin.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2012\/08\/gradle-custom-plugin.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2012\/08\/gradle-custom-plugin.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/gradle-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/gradle-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2012\/08\/gradle-custom-plugin.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/www.javacodegeeks.com\/category\/java"},{"@type":"ListItem","position":3,"name":"Enterprise Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/enterprise-java"},{"@type":"ListItem","position":4,"name":"Gradle Custom Plugin"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/913ac3950434ad48c2c2b98e23d86a49","name":"Rakesh Cusat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/232ae7fa9c21c4a55e50bb934653939d45522ec03f0c2045ae3bb139faa275bb?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/232ae7fa9c21c4a55e50bb934653939d45522ec03f0c2045ae3bb139faa275bb?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/232ae7fa9c21c4a55e50bb934653939d45522ec03f0c2045ae3bb139faa275bb?s=96&d=mm&r=g","caption":"Rakesh Cusat"},"sameAs":["http:\/\/code4reference.com\/"],"url":"https:\/\/www.javacodegeeks.com\/author\/Rakesh-Cusat"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/1626","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/274"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=1626"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/1626\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/129"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=1626"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=1626"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=1626"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}