Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Eclipse Java Compiler (ecj) in maven builds

Eclipse uses it's own compiler (ECJ) to compile Java code. Debugging a program compiled with Eclipse is easier, because simple code changes can be applied instantly (by the hot code replacement).

Maven on the other hand uses (by default) oracle JDK, that generates different byte code preventing hot code replacement in a Eclipse debug session.

So I would like to use Eclipse ECJ compiler with my maven build, if I plan to debug the program. A convenient way for me would be a "ecj" profile:

  • Compile release

    $ mvn package
    
  • Compile snapshot with enabled hot code replacement

    $ mvn -P ecj package
    

Also the profile activation can be specified in settings.xml or even Eclipse project properties.

My questions are:

  1. Is this the right way to go?
  2. How this can be configured?
  3. Can maven toolchain be used for this?
like image 933
Boris Brodski Avatar asked Oct 16 '15 07:10

Boris Brodski


People also ask

Which Java compiler does Maven use?

The default Java compiler version used by Maven is Java 1.5 .

How do I run a Maven build in Eclipse?

Building the Maven Project in Eclipse First of all, select the project and go to “Run As -> Maven Build”. The “Edit Configuration” popup window will open. Enter the “Goals” as “package” to build the project and click on the Run button.

Does Eclipse come with a Java compiler?

Java Compilation in EclipseThe Eclipse IDE comes bundled with its own Java compiler called Eclipse Compiler for Java (ECJ). This is an incremental compiler that can compile only the modified files instead of having to always compile the entire application.


1 Answers

It is possible to change the default javac compiler that is used by the maven-compiler-plugin. The Eclipse compiler is bundled in the artifact plexus-compiler-eclipse and it is declared by setting eclipse to the compilerId attribute of the maven-compiler-plugin.

If you want to activate this change for a custom profile, you could have the following configuration:

<profile>
  <id>ecj</id>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.6.0</version>
        <configuration>
          <compilerId>eclipse</compilerId>
        </configuration>
        <dependencies>
          <dependency>
            <groupId>org.codehaus.plexus</groupId>
            <artifactId>plexus-compiler-eclipse</artifactId>
            <version>2.8.1</version>
          </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>
</profile>

The plugin is maintained in the plexus-compiler GitHub repository. Version 2.8.1 uses 3.11.1.v20150902-1521 of JDT, although you could use your own version by adding a dependency on org.eclipse.tycho:org.eclipse.jdt.core after the Plexus Compiler dependency.

like image 139
Tunaki Avatar answered Sep 21 '22 05:09

Tunaki