Gradle's project configuration

First, we add plugins into build.gradle used for the project:

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'maven'

With these lines, we specify the use of three plugins: the Java plugin to compile and assemble our project, the Eclipse plugin to generate files that are used to import the project into the Eclipse IDE (if you want to use Eclipse), and the Maven plugin to deploy artifacts into the Maven repository.

In the next section, we will declare the data, Java version compatibility, group ID, and project version, which are relative to the project:

sourceCompatibility = 1.6
group = 'org.gradle.test'
version = '1.1'

Thanks to the Groovy syntax, we can declare what the jar manifest contains, in a simple and elegant way:

jar {
    manifest {
      attributes 'Implementation-Title': 'Gradle Test', 'Implementation-Version': version
    }
}

The version number refers to the global variable, version, just declared in the previous statement.

In the repositories section, we can see how Gradle makes use of Maven official repositories and our custom repository:

repositories {
  mavenCentral()
  mavenLocal()

  maven { url "http://ourserver:8080/nexus/content/repositories/ourrepo" }

}

Thanks to the mavenCentral() object, Gradle downloads dependencies from the Maven 2 repository (http://repo1.maven.org/maven2), and the mavenLocal() object indicates to get dependencies from the PATH_TO/.m2 local repository.

As we can see, we add the custom repository while looking for dependencies.

The following code snippet shows us how to declare dependencies:

configurations {
  deployerJars
}
dependencies {
    compile 'commons-collections:commons-collections:3.2'
    testCompile 'junit:junit:4.+'
    deployerJars "org.apache.maven.wagon:wagon-http:2.2"
}

The first line of the code represents a local variable to import utility libraries used to perform a Maven deploy. In the second statement, we have the dependencies declaration since we can use different scopes for dependency import in Maven, as shown in the following table:

Scope

Function/objective

Default

compile

Required to compile the source

 

runtime

Required at runtime

Includes the compile time dependencies

testCompile

Required in order to compile a test source

Includes compiled production and compile time

testRuntime

Required in order to run tests

Includes compile, runtime, and test dependencies

To import a version greater or equal to a certain version, Gradle uses the + notation for a JUnit import case.

We assigned the wagon-http dependency to the deployerJars variable used to deploy the JAR into a repository.

We are able to perform a build of the project with this minimal configuration. To perform a build positioning in the project's base directory, launch the following command:

$ gradle build

This input will generate a simple output:

:compileJava
:processResources
:classes
:jar
:assemble
:compileTestJava
:processTestResources
:testClasses
:test
:check
:build

BUILD SUCCESSFUL

In this case, we can execute without having the declared task, build, which is possible because of the Java plugin.

The Java plugin adds a build file command to configuration, without declaration in the build.gradle file.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset