I have an situation where some Java 1.6 sources won't compile in Maven and only compile in javac when I specify sources in a certain order.
pom.xml is bare-bones but specifies -source
and -target
of 1.6:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
ComputeOperation.java
package test.domain;
public interface ComputeOperation<T> {
public T compute(T arg1, T arg2);
}
Computations.java
package test.domain;
public enum Computations implements ComputeOperation<Integer> {
ADD {
@Override
public Integer compute(Integer arg1, Integer arg2) {
return arg1 + arg2;
}
},
SUBTRACT {
@Override
public Integer compute(Integer arg1, Integer arg2) {
return arg1 - arg2;
}
}
}
App.java
package test.app;
import test.domain.*;
public class App {
public static void main( String[] args ) {
System.out.println(Computations.ADD.compute(1, 1));
}
}
Maven build fails as follows (full debug log here):
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /home/nzucker/projects/maven-test/src/main/java/test/app/App.java:[7,44] cannot find symbol
symbol : method compute(int,int)
location: class test.domain.Computations
[INFO] 1 error
Plain old javac
compilation fails succeeds if App.java comes before Computations.java:
[nzucker:maven-test]$ javac -source 1.6 -target 1.6 -g -d target/classes src/main/java/test/app/App.java src/main/java/test/domain/Computations.java src/main/java/test/domain/ComputeOperation.java
src/main/java/test/app/App.java:7: cannot find symbol
symbol : method compute(int,int)
location: class test.domain.Computations
System.out.println(Computations.ADD.compute(1, 1));
^
1 error
But the build succeeds if I put App.java last in the input sources:
[nzucker:maven-test]$ javac -source 1.6 -target 1.6 -g -d target/classes src/main/java/test/domain/Computations.java src/main/java/test/domain/ComputeOperation.java src/main/java/test/app/App.java
[nzucker:maven-test]$ java -cp target/classes/ test.app.App
2
Finally, if I change the import
in App.java to a static import, compilation succeeds:
package test.app;
import static test.domain.Computations.*;
public class App {
public static void main( String[] args ) {
System.out.println(ADD.compute(1, 1));
}
}
Questions:
I am aware of "Workaround for javac compilation order bug in maven" - so my question is why is this occurring? Is this a true defect in Maven? If so, what is the JIRA issue if so?
Also, why does compilation fail? I thought that javac resolve dependencies and compilation order between classes during a pre-processing phase? I guess I was wrong? Is there a specification that covers this?
Ok, the other question I linked to has a response that points to a bug in Java 1.6. The answer is to upgrade to Java 1.7 or implement a work-around.