I’ve been trying unsuccessfully to accomplish incremental compile of java files in a large multi-project build with “project to project” compile dependencies. After a number of iterations, I can’t get it to recompile a ‘referencing’ java file when the ‘referenced’ java file in another project changes. I have a relatively simple test bed that illustrates what I’m seeing. I don’t see this problem when the ‘referencer’ is in the same gradle project. I’ve edited my original post to show in bold what appears to be the problem (Gradle sees a class has changed but does not recompile a class that references it unless that class is in the same project).
I’m hoping to get some advice on what I am doing wrong in my build scripts. I’ve tried different ways of defining my source/inputs and my compile dependencies and haven’t gotten this to work yet.
What I see is everything compiles fine the 1st time. Then if I alter the Person.java file in the src directory, that file is recompiled and so is the Person2.java file that references it in the same project. However, the DeptInSrc2.java (that also references it) in the src2 project is not recompiled.
When I use the --info flag on the gradle command line, the detail shows that the src2 project’s inputs sees that Person.class changed. But then, it says there is nothing to compile. (I pasted a snippet of that info at the bottom of this post).
I’m using Gradle 2.7 and my simple test bed for this currently has the following structure:
- gradle-test (project’s root directory)
build.gradle ( ‘:’ project’s build script to compile src java files into output)
settings.gradle
gradle.properties- buildSrc (directory containing some groovy utilites for path resolution etc)
- output (directory where resulting class files are being put)
- src (dir simulates common source component for other components to reference)
com.my.test.Person.java (referenced in Person2.java & in “src2” project’s DeptInSrc2.java)
com.my.test.Person2.java (java source that references Person’s getName() method - src2 (dir simulates a source component that references a common component)
build.gradle (dependent gradle project’s build script compiles src2 files into output)
com.my.test.DeptInSrc2.java (references Person’s getName() method from src)
The build.gradle in root project (that compiles the src) looks like this:
apply plugin: 'java’
apply plugin: 'eclipse’
ext { // add these to project to use for constants
propBasedJavaFlags = ["-encoding",“UTF-8”,"-g","-deprecation","-implicit:none","-proc:none",
“-target”,“1.5”,"-source",“1.5”]
}
sourceSets {
main {
java {
srcDirs = [‘src’]
include ‘**/*.java’
}
}
}
compileJava {
destinationDir=file(‘output/src’)
options.incremental = true
options.compilerArgs.addAll(project.propBasedJavaFlags)
source = sourceSets.main.java
}
The build.gradle in the src2 (simulated dependant component) looks like this:
apply plugin: ‘java’
apply plugin: ‘eclipse’
sourceSets {
main {
java {
srcDirs = [’…/src2’]
include ‘**/*.java’
}
}
}
compileJava {
destinationDir=file(’…/output/src2’)
options.incremental = true
options.compilerArgs.addAll(project.propBasedJavaFlags)
source = sourceSets.main.java
dependencies {
compile files(’…/output/src’)
// next line no better for incremental than above
//compile project(’:’)
}
}
Here’s what a snippet of the relevant --info shows for a compile where I’ve altered a method in Person.java that is referenced in Person2.java of src and DeptInSrc2.java of src2.
Note: Even if I delete a method from Person.java that is referenced in the DeptInSrc2.java file, that file is not recompiled. Notice though that it does recognize an input for consideration in src2:compileJava has changed. Just does not recompile anything. What am I doing wrong here that is causing this behaviour?
:compileJava (Thread[Daemon worker,5,main]) started.
:compileJava
Executing task ‘:compileJava’ (up-to-date check took 0.023 secs) due to:
Input file C:\hmc_workspaces\trunk_ws\gradle-test\src\com\my\test\Person.java has changed.
Created jar classpath snapshot for incremental compilation in 0.0 secs.
Compiling with JDK Java compiler API.
Incremental compilation of 2 classes completed in 0.457 secs.
Class dependency analysis for incremental compilation took 0.009 secs.
Written jar classpath snapshot for incremental compilation in 0.003 secs.
:compileJava (Thread[Daemon worker,5,main]) completed. Took 0.535 secs.
:src2:compileJava (Thread[Daemon worker,5,main]) started.
:src2:compileJava
Executing task ‘:src2:compileJava’ (up-to-date check took 0.018 secs) due to:
Input file C:\hmc_workspaces\trunk_ws\gradle-test\output\src\com\my\test\Person.class has changed.
Created jar classpath snapshot for incremental compilation in 0.001 secs.
None of the classes needs to be compiled! Analysis took 0.007 secs.
Written jar classpath snapshot for incremental compilation in 0.003 secs.
:src2:compileJava (Thread[Daemon worker,5,main]) completed. Took 0.049 secs.
BUILD SUCCESSFUL