【问题标题】:Building maven projects with JDT Core compiler使用 JDT Core 编译器构建 maven 项目
【发布时间】:2021-12-29 12:51:34
【问题描述】:

我是一个新的 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>

将不胜感激。

【问题讨论】:

    标签: maven maven-compiler-plugin ecj


    【解决方案1】:

    我搞定了:

    1. 使用记录在案的插件/配置样式。
    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>    
    

    【讨论】:

    • 试过了,得到了很多类似这样的错误: 1. ERROR in .../ROJO.java (at line 75) public default UDI.Complex toUDI(String udiName, String udiType) ^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 默认方法仅在源级别 1.8 或更高版本中允许
    • see here: 项目>属性>&lt;maven.compiler.source&gt;YOUR_SRC_VERSION&lt;/maven.compiler.source&gt;&lt;maven.compiler.target&gt;YOUR_TARGET_VERSION&lt;/maven.compiler.target&gt;
    • ..or: .../configuration>:&lt;source/&gt;&lt;target/&gt; ;)
    • 感谢您的帮助..我有很多问题要解决,但我想您提供了答案..
    • 感谢您的接受并祝您好运,@eaRobust! ;)
    猜你喜欢
    • 2014-11-21
    • 2020-10-21
    • 1970-01-01
    • 1970-01-01
    • 2021-11-05
    • 2014-11-27
    • 2018-07-10
    • 2020-08-29
    • 1970-01-01
    相关资源
    最近更新 更多