How to compile with 3 different steps (gradle 1.5)?

(tuomas.kassila) #1

I have almost all source files which are java files, and some are groovy source files. And then I have lib/*.jar files those I should take into compiling. How can I compile with 3 steps with Gradle? Some source files are included and some are excluded into/from compiling.

Source structure:

src/main/java src/main/groovy

My build.gradle:

apply plugin: ‘groovy’ // apply plugin: ‘java’

version = 1.1

defaultTasks ‘clean’, ‘main1classes’, ‘main2classes’, ‘main3classes’, ‘myjar’

sourceCompatibility=1.6 targetCompatibility=1.6

repositories {

mavenCentral() }

def libs = files(‘lib/activation.jar’, ‘…/xalan.jar’)

dependencies {

groovy group: ‘org.codehaus.groovy’, name: ‘groovy-all’, version: ‘2.0.5’

testCompile group: ‘junit’, name: ‘junit’, version: ‘4.8.2’

compile libs

// main1classes libs

// main1classes2 libs

// compileMain3Java libs

// main1classes3 libs }

sourceSets {

main1 {

java {

srcDirs = [‘src/main/java’]

compileClasspath += libs

include ‘fi/nkl/thp/app/**/*/XmlReader’, …

exclude ‘fi/nkl/thp/app/**/*/*Article’



main2 {

groovy {

srcDirs = [‘src/main/groovy’]

compileClasspath += sourceSets.main1.output

compileClasspath += libs

include ‘fi/nkl/thp/app/**/*/HameensanomatArticle’



main3 {

java {

srcDirs = [‘src/main/java’]

compileClasspath += sourceSets.main1.output

compileClasspath += sourceSets.main2.output

compileClasspath += libs

include ‘fi/**//




task myjar(type: Jar) {

baseName = ‘hameensanomat’

from sourceSets.main1.output

from sourceSets.main2.output

from sourceSets.main3.output }

// = “test.tree” project.version = “” // manifest.mainAttributes(“Main-Class” : “test.tree.App”)

compileJava.options.encoding = ‘UTF-8’

Thank in advance,


(Peter Niederwieser) #2

I’m not sure what the question is, but it’s probably easier to use Java-Groovy joint compilation. Either put all Java code into ‘src/main/groovy’, or reconfigure the source directories as follows:

sourceSets {
    main {
        groovy.srcDirs += java.srcDirs
        java.srcDirs = []

Now, ‘gradle compileGroovy’ will compile both your Java and Groovy code, and you can have arbitrary dependencies between them.

PS: If you are using Gradle 1.4 or higher, it’s preferable to put Groovy on the ‘compile’ configuration, rather than the ‘groovy’ configuration.

(tuomas.kassila) #3

Thanks, with your sample it works now :). I would have to write more clear question.

My question actually was: If there comes situations where must compiling with several different steps or phases, how define gradle build then? (I have seen impl/interface gradle sample, it is ok, but if I will use include/exclude defines:)

I can define different include/exclude sourcesets, but my problem was how get library (.jar) files

defined into compile/compileGroovy/compileJava default tasks? Or my own compile task?


(Peter Niederwieser) #4

It depends. If you leverage source sets and declare a source set ‘foo’, you’ll automatically get a ‘compileFooJava’ task and ‘fooCompile’ and ‘fooRuntime’ configurations. To add dependencies, you only need to assign them to the configurations.

If you don’t leverage source sets, you’ll have to declare tasks and configurations yourself, and have to configure the tasks yourself (e.g. their ‘classpath’). See ‘JavaCompile’ in the Gradle Build Language Reference.