gradleInitializing Gradle

Remarks

Terminology

  • Task - an atomic piece of work which a build performs. Tasks have inputs, outputs and task dependencies.
  • dependencies {} - Declares File or binary dependencies necessary to execute tasks. For example, org.slf4j:slf4j-api:1.7.21 is shorthand coordinates to a Maven dependency.
  • repositories {} - How Gradle finds files for external dependencies. Really, just a collection of files organized by group, name, and version. For example: jcenter() is a convenience method for maven { url 'http://jcenter.bintray.com/' } }, a Bintray Maven repository.

Initializing a New Java Library

Prerequisite: Installing Gradle

Once you have Gradle installed, you can setup a new or existing project by running

cd $PROJECT_DIR
gradle init --type=java-library

Note that there are other project types like Scala you can get started with, but we'll use Java for this example.

You will end up with:

.
├── build.gradle
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
└── src
    ├── main
    │   └── java
    │       └── Library.java
    └── test
        └── java
            └── LibraryTest.java

You can now run gradle tasks and see that you can build a jar, run tests, produce javadocs and much more even though your build.gradle file is:

apply plugin: 'java'

repositories {
    jcenter()
}

dependencies {
    compile 'org.slf4j:slf4j-api:1.7.21'
    testCompile 'junit:junit:4.12'
}