本文介绍了在 Maven 构建中使用 Eclipse Java Compiler (ecj)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Eclipse 使用它自己的编译器 (ECJ) 来编译 Java 代码.调试使用 Eclipse 编译的程序更容易,因为可以立即应用简单的代码更改(通过热代码替换).

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 使用(默认情况下)oracle JDK,它生成不同的字节码,防止在 Eclipse 调试会话中进行热代码替换.

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

所以如果我打算调试程序,我想在我的 maven 构建中使用 Eclipse ECJ 编译器.对我来说一个方便的方法是ecj"配置文件:

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
    

  • 还可以在 settings.xml 甚至 Eclipse 项目属性中指定配置文件激活.

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

    我的问题是:

    1. 这是正确的方式吗?
    2. 如何配置?
    3. maven 工具链可以用于此目的吗?

    推荐答案

    可以更改.Eclipse 编译器捆绑在工件 plexus-compiler-eclipse 并通过将 eclipse 设置为 属性.

    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>
    

    该插件维护在 plexus-compiler GitHub 存储库中.版本 2.8.1 使用 JDT 3.11.1.v20150902-1521,尽管您可以通过添加对 org.eclipse.tycho:org.eclipse.jdt.core 之后Plexus 编译器依赖项.

    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.

    这篇关于在 Maven 构建中使用 Eclipse Java Compiler (ecj)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    08-20 19:23