-->

Incremental java compile with maven (like Eclipse

2019-04-17 11:51发布

问题:

I want to use maven to build projects in which there are unresolved compilation problems.

The main purpose is package and deploy or run aplications using some kind of stubs for classes that contains compilation errors, like I understand that Eclipse does (thanks to JDT Core).

I configurate maven java compiler plugin following Apache Maven documentation at Using Non-Javac compiler to use Eclipse compiler. Thinking that maybe should set some arguments to modify the compiler/builder behaivor I was reading Help Eclipse - Compiling Java code but I don't realize which compiler/builder option or combination of these does the trick.

So far, the next configuration of the maven java compiler plugins compile using the eclipse compiler and package the application including generated .class (jvm bytecode) only for java classes without compilation errors. To get this behaivor it just require use the eclipse compiler (see compilerId and the dependency) and set failOnError=false.

<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration>
        <compilerId>eclipse</compilerId>
        <source>1.7</source>
        <target>1.7</target>
        <optimize>true</optimize>
        <showWarnings>true</showWarnings>
        <showDeprecation>true</showDeprecation>
        <failOnError>false</failOnError>
        <compilerArguments>
            <org.eclipse.jdt.core.compiler.problem.fatalOptionalError>disabled</org.eclipse.jdt.core.compiler.problem.fatalOptionalError>
            <org.eclipse.jdt.core.compiler.problem.forbiddenReference>ignore</org.eclipse.jdt.core.compiler.problem.forbiddenReference>
        </compilerArguments>
    </configuration>

    <dependencies>
         <dependency>
              <groupId>org.codehaus.plexus</groupId>
              <artifactId>plexus-compiler-eclipse</artifactId>
              <version>2.3</version>
         </dependency>
    </dependencies>
</plugin>

With this configuration I could run java application as long as the execution doesn't use classes not included for compilation errors (because the stubs aren't generated) but on a Java EE container, the classloading will faild so the application can never be deployed.

I appreciate any help on this.

回答1:

Just for share the solution, at that moment I just replace the plexus-compiler-eclipse with tycho-compiler-jdt to get the desire behaivor.

The proceedOnError parameter indicates that it must keep compiling in spite of errors, dumping class files with problem methods or problem types how to deal with the compilation errors.

Next is the final configuration sample.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration>
        <compilerId>jdt</compilerId>
        <source>1.7</source>
        <target>1.7</target>
        <optimize>true</optimize>
        <failOnError>false</failOnError>
        <compilerArguments>
            <proceedOnError/>
        </compilerArguments>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.eclipse.tycho</groupId>
            <artifactId>tycho-compiler-jdt</artifactId>
            <version>0.22.0</version>
        </dependency>
    </dependencies>
</plugin>

There is more plugin configuration examples in the Tycho FAQ. And the possible compiler arguments are described in section Using the batch compiler of the Java development user guide (Eclipse Help site).