使用 JDT Core 编译器构建 Maven 项目

Building maven projects with JDT Core compiler

我是 Maven 新用户。我知道 Maven 用来构建其项目的默认编译器是 Javac。但是,我想使用 JDT Core 编译器来构建。

我尝试将此插件添加到我的根 pom 中的插件中,但它不起作用。

<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>

非常感谢任何帮助。

我成功了:

  1. 使用记录的 plugin/configuration 样式。
  2. 调整最适合您的版本(我遇到了较低(低于 2.8.8)和 2.9.0 版本的“问题”)。

像这样:

<?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>

与:

mvn clean compile -X

我们得到:

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
------------------------------------------------------------------------

要使用(尝试!;)替代 ecj 版本,我们可以:

<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>