Home > Mobile >  Building maven projects with JDT Core compiler
Building maven projects with JDT Core compiler

Time:11-19

I am a new Maven user. I am aware that the default compiler which Maven uses to build its projects with is Javac. However, I want to use JDT Core compiler in order to build.

I tried adding this plugin into my plugins in my root pom, but it doesn't work.

<plugin>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>3.8.0</version>
  <configuration>
    <compilerId>eclipse</compilerId>
  </configuration>
  <dependencies>
    <dependency>
      <groupId>org.eclipse.jdt.core.compiler</groupId>
      <artifactId>ecj</artifactId>
      <version>4.4.2</version>
    </dependency>
  </dependencies>
</plugin>

would appreciate any help.

CodePudding user response:

I got it working:

  1. Use the documented plugin/configuration style.
  2. Adjust the version, that suits you best (I had "issues" with lower (than 2.8.8) and 2.9.0 versions).

like so:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <build>
        <plugins> 
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <compilerId>eclipse</compilerId>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.codehaus.plexus</groupId>
                        <artifactId>plexus-compiler-eclipse</artifactId>
                        <version>2.8.8</version> <!-- => ecj.3.2.2 -->
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>

with:

mvn clean compile -X

we get:

Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: C:\Program Files\NetBeans-12.5\netbeans\java\maven\bin\..
Java version: 16.0.2, vendor: Oracle Corporation, runtime: C:\Program 
Files\Java\jdk-16.0.2
Default locale: de_DE, platform encoding: UTF-8
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows
...
... 
-- end configuration --
Using compiler 'eclipse'.
Adding ...
CompilerReuseStrategy: reuseCreated
useIncrementalCompilation enabled
Stale source detected: ...
Changes detected - recompiling the module!
Classpath:
 ...
Source roots:
 ...\src\main\java
 ...\target\generated-sources\annotations
incrementalBuildHelper#beforeRebuildExecution
Using JSR-199 EclipseCompiler
ecj: using character set UTF-8
ecj command line: [-noExit, -preserveAllLocals, -g:lines,vars,source, -source, 1.6, -target, 1.6, -encoding, UTF-8, -warn:none, -d, ...\target\classes, -classpath, ...\target\classes;...\target\classes;]
ecj input source files: [C:\DEV\projects\...\Application.java]

incrementalBuildHelper#afterRebuildExecution
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time:  3.254 s
Finished at: 2021-11-18T18:03:02 01:00
------------------------------------------------------------------------

To use(try!;) an alternative ecj version, we can:

<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.1</version>
    <configuration>
        <compilerId>eclipse</compilerId>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.codehaus.plexus</groupId>
            <artifactId>plexus-compiler-eclipse</artifactId>
            <version>2.8.8</version>
            <exclusions>
                <exclusion>
                    <groupId>org.eclipse.jdt</groupId>
                    <artifactId>ecj</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jdt</groupId>
            <artifactId>ecj</artifactId>
            <version>3.27.0</version> <!-- 09/15/2021 -->
        </dependency>
    </dependencies>
</plugin>    
  • Related