Chapter 3
The Java Projects Appetizer

We have in-depth coverage with many examples about the Java plugin, the dependency management and multi-project builds in the later chapters. In this chapter we want to give you just a first idea.

3.1 Examples

Provided your build script contains the single line

  usePlugin('java')

Executing gradle libs will compile, test and jar your code. If you specify a remote repository, executing gradle uploadLibs will do additionally an upload of your jar to a remote repository. Builds have usually more requirements. Let’s look at a typical multi-project build.

  D- ultimateApp
    D- api
    D- webservice
    D- shared

We have three projects. api is shipped to the client to provide them a Java client for your XML webservice. webservice is a webapp which returns XML. shared is code used both by api and webservice. Let’s look at the Gradle build scripts of those projects.

  subprojects {
      manifest.mainAttributes([
       'Implementation-Title': 'Gradle',
   'Implementation-Version': '0.1'
   ])
   dependencies {
   compile "commons-lang:commons-lang:3.1"
   testCompile "junit:junit:4.4"
   }
   sourceCompatibility = 1.5
   targetCompatibility = 1.5
   test {
       include '**/*Test.class'
       exclude '**/Abstract*'
   }
  }

The commons stuff for all Java projects we do define in the root project. Not by inheritance but via Configuration Injection. The root project is like a container and subprojects iterates over the elements of this container and injects the specified configuration. This way we can easily define the manifest content for all archives.

  dependencies {
   compile "commons-httpclient:commons-httpclient:3.1", project(":shared")
  }
  
  dists {
   zip() {
   files(dependencies.resolve("runtime")) // add dependencies to zip
   fileset(dir: "path/distributionFiles")
   }
  }

In the api build script we add further dependencies. One dependency are the jars of the shared project. Due to this dependency shared gets now always build before api. We also add a distribution, that gets shipped to the client.