Workaround for javac compilation order bug in maven

后端 未结 5 2011
栀梦
栀梦 2020-12-09 16:34

I\'m encountering a bug in the Java compiler where the order of files submitted for compilation can cause code not to compile. I\'ve drilled down the code to isolate the sma

相关标签:
5条回答
  • 2020-12-09 17:18

    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.

    0 讨论(0)
  • 2020-12-09 17:22

    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.

    0 讨论(0)
  • 2020-12-09 17:23

    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

    0 讨论(0)
  • 2020-12-09 17:29

    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>
    
    0 讨论(0)
  • 2020-12-09 17:31

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

    0 讨论(0)
提交回复
热议问题