Workaround for javac compilation order bug in maven

旧巷老猫 提交于 2019-11-28 09:04:12

I played around, and found that adding simple cast:

public static void main(String[] args) {
    ((ActionSpec)Actions.SKIP).run("hello");
}

solves this problem. Passing this enum as method parameter as interface would also do the trick

It's the bug reported in http://bugs.sun.com/view_bug.do?bug_id=6724345

The workaround suggested should work if you are still using Java 6 compiler. The bug is fixed in Java 7.

Here's how I would do it:

  • Move the ActionSpec interface to another Maven project. We usually have interfaces and common domain classes in their own project, e.g. foo-service-specs.
  • Keep the other classes in the implementation project, e.g. foo-service-impl.
  • Include the foo-service-specs project as a dependency in foo-service-impl.

By doing this, you can make sure that the compilation order is working, and it should also work for continuous integration.

Try multiple executions of the compiler plugin. Using default-compile as the first execution ID adds the new config to Maven's default compiler execution. Use the <includes/> config elements in the default execution to compile the enums first. For the second execution you'd use a combination of <includes> and <excludes> , where <includes> would be all of your code and excludes would be the enums already compiled.

I think this will work for your example program but I did not test it. I have tested something similar before with Maven 3 and it worked fine.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
    <configuration>
        <source>1.6</source>
        <target>1.6</target>
    </configuration>
    <executions>
        <execution>
           <id>default-compile</id>
           <goals><goal>compile</goal></goals>
           <configuration>
              <includes>
                  <include>**/Actions.*</include>
              </includes>
           </configuration>
        </execution>
        <execution>
           <id>second</id>
           <goals><goal>compile</goal></goals>
           <configuration>
              <includes>
                  <include>**/*</include>
              </includes>
              <excludes>
                  <exclude>**/Actions.*</exclude>
              </excludes>
           </configuration>
        </execution>
    </executions>
</plugin>
Christian

We had the same problem. Multiple executions of the maven compiler plugin worked for us...

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!