I have a java project with many source files, however one of the files needs to be compiled first to then generate other source files for the full compilation.
Historically a simple ant javac task has done the trick. But attempting to do the same with gradle is proving troublesome. So as an example I’ve created a simple example with just two files.
Both files are in a package called some.pkg and we therefore have the following directory structure:
.\build.gradle .\src\main\java\some\pkg\ClassOne.java .\src\main\java\some\pkg\ClassTwo.java
ClassOne:
package some.pkg;
import some.pkg.*;
public class ClassOne {
public void doStuff() {
}
}
ClassTwo
package some.pkg;
import some.pkg.*;
public class ClassTwo {
public void ClassTwo() {
ClassOne cone = new ClassOne();
cone.doStuff();
}
}
As you can see, ClassTwo (our class we want to compile standalone) depends on ClassOne.
Normally (ant aside) it would be completely fine to run javac from the command line as follows:
.\src\main\java> javac some\pkg\ClassOne.java
And in ant, it’s a simple case of:
<project>
<property name="build.dir"
value="build"/>
<property name="lib.dir"
value="lib"/>
<property name="src.dir"
value="src/main/java"/>
<target name="generate" >
<mkdir dir="${build.dir}"/>
<javac source="1.6" target="1.6" srcdir="${src.dir}" destdir="${build.dir}"
debug="true" includeantruntime="false"
includes="some/pkg/ClassTwo.java">
</javac>
</target>
</project>
But in gradle, I keep having an issue where javac can not find ClassOne. It’s as though the sourcedir is not pointing where it should. I was thinking the gradle ‘JavaCompile’ task’s ‘source’ property was working like the ant ‘srcdir’ property, but that appears to not be the case. So here is the gradle script I’m currently trying:
apply plugin: 'java'
task compileOne (type: JavaCompile) {
source = sourceSets.main.java.srcDirs
include 'some/pkg/ClassTwo.java'
classpath = sourceSets.main.compileClasspath
destinationDir = sourceSets.main.output.classesDir
}
But it results in:
C:\test\>gradle generate
:generate
C:\test\src\main\java\some\pkg\ClassTwo.java:7: cannot find symbol
symbol
: class ClassOne
location: class some.pkg.ClassTwo
ClassOne cone = new ClassOne();
^
C:\test\src\main\java\some\pkg\ClassTwo.java:7: cannot find symbol
symbol
: class ClassOne
location: class some.pkg.ClassTwo
ClassOne cone = new ClassOne();
^
2 errors
:generate FAILED
So do I achieve an ant javac equivalent in gradle? (Obviously I’m attempting to avoid doing ant.javac()…)
Thank you!