Creating Tasks
So far, we have only used tasks provided by built-in plugins. Licensing terms is a commonly included part of a project. Let's create a task that allows you to set the LICENSE.txt file to GPL 3.0. Gradle's API gives us full control over the project's file system as well as leveraging Groovy and the JVM. As a result, we can retrieve the license text and save it to a local file in only ten lines of code:
task GPL(description:'Sets project to GPL 3.0 license') << { licenseFile = new File('LICENSE.txt') u = 'http://www.gnu.org/licenses/gpl-3.0.txt' println 'Retrieving GNU license text' def url = new URL(u) println 'Saving to LICENSE.txt' licenseFile.setText(url.getText()) }
Adding Repositories to Your Project
So far we have relied on libraries provided by Java. While there are many things you can do without any dependencies, in many cases you will need at least one or two. Repositories hold the dependencies that our projects might need. To use libraries contained in repos, we need to add a repositories block to the build.gradle file:
repositories { }
Remote repositories can be something like Maven Central, which holds thousands of libraries for anyone to reference and use, or they could be a repository behind your corporate firewall, either of the Maven or Ivy varieties. Maven Central is referenced so often that it uses a special function for brevity. To use a remote repo, create a new code block in repositories beginning with the type of repository (either maven or ivy) and a URL indicating its location:
repositories { mavenCentral() maven { url 'http://maven.mysite.com/maven2' } }
The URL string can also refer to a local Maven or Ivy repository. In that case, the URL is relative to the location of the build.gradle file that references it.
If you would rather not bother with repositories or aren't using them, you can tell Gradle to look for the files in a local directory using the following snippet of code:
repositories { flatDir name:'localRepo', dirs:'lib' }
Now that we have our repos set up, we can retrieve libraries for use in our applications. To do so, we need to add a dependencies code block. In it, we indicate the point in the Gradle app life cycle in which the libraries are needed and its name, version number, and other details to help Gradle locate the file. For the Java plugin, there are four of these states, which are also called dependency configurations: compile, runtime, testCompile, and testRuntime. These indicate the dependencies needed to compile or run the application or its test suite, respectively.
Here is a dependencies block with some sample dependencies:
dependencies { // Short form compile 'com.miglayout:miglayout:3.7.4' testCompile 'junit:junit:4.10' // Long form compile group:'com.miglayout', name:'miglayout', version:'3.7.4' }
In addition to search.maven.org, you can use MVN Repository website to search for libraries. I prefer it over search.maven.org because it shows how to reference libraries in a handful of different build systems. You can find links to download libraries for use in a flatDir repo or get the text to reference them from Maven.