【问题标题】:Use Maven Compiler Plugin with Eclipse Compiler and Lombok将 Maven 编译器插件与 Eclipse 编译器和 Lombok 一起使用
【发布时间】:2016-08-10 07:47:22
【问题描述】:

有没有办法在 ECJ 中编译 lomboked 代码不将 lombok 设置为 javaaagent 用于 maven 进程?

下面的 sn-p 仅在我以 lombok 作为代理运行 mvn 时才有效

<profile>
    <id>ecj</id>
    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <compilerId>eclipse</compilerId>
                    </configuration>
                    <dependencies>
                        <dependency>
                            <groupId>org.codehaus.plexus</groupId>
                            <artifactId>plexus-compiler-eclipse</artifactId>
                            <version>2.8-SNAPSHOT</version>
                        </dependency>
                        <dependency>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>1.16.8</version>
                        </dependency>
                    </dependencies>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</profile>

MAVEN_OPTS=-javaagent:lombok.jar mvn -P ecj 编译成功。

但是只运行 mvn -P ecj 会产生常见的 no-lombok 错误,例如:__ cannot be resolved to a type

我尝试使用com.reubenpeeris.maven:lombok-eclipse-compiler:1.3 但是Compilation failure Unrecognized option: target/generated-sources/annotations 失败了,我认为这意味着这个编译器太旧了。

我也试过添加

<fork>true</fork>
<compilerArgs>
    <arg>-javaagent:lombok.jar</arg>
</compilerArgs>

但是好像没什么效果。

【问题讨论】:

    标签: eclipse maven lombok maven-compiler-plugin


    【解决方案1】:

    简而言之,没有办法。但是,有变通办法。

    eclipse 实现是plexus-compiler-eclipse,它不接受fork 参数,因为它使用内部jvm。所以,它只能接受像MAVEN_OPTS这样的jvm选项。

    但是,默认实现是“plexus-compiler-javac”,它支持自定义executable。解决方法可能如下:将fork 设置为true,并指定executable。大概是这样的:

    #!/bin/bash
    exec java -javaagent:/path/to/lombok.jar -jar /path/to/ecj.jar $@
    

    或者,直接使用项目中的ecj,在pom.xml中定义-AJAVAC:(-AXXX=XXX可以被javac接受)

    #!/bin/bash
    
    # save as maven-compiler-javac
    for last; do
      :
    done
    
    if [ ${last:0:1} == "@" ]; then
      file=${last:1}
      if [ -f "$file" ]; then
        # tail -1 $file
        while read line; do
          last="$line"
        done < "$file"
      fi
      # "xxx" -> xxx
      length=${#last}
      length=$((length - 2))
      last=${last:1:$length}
    fi
    
    if [ ${last:0:8} == "-AJAVAC=" ]; then
      exec java ${last:8} $@
    else
      exec javac $@
    fi
    

    更改 pom.xml:

    <!-- to use ${org.projectlombok:lombok:jar} and so -->
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>2.10</version>
        <executions>
            <execution>
                <id>set-properties</id>
                <goals>
                    <goal>properties</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
            <fork>true</fork>
            <executable>${basedir}/maven-compiler-javac</executable>
            <!-- plexus-compiler-eclipse use org.codehaus.plexus:plexus-compiler-eclipse, i prefer org.eclipse.jdt.core.compiler:ecj -->
            <!-- if you're developing lombok extension, you should add -Xbootclasspath/a: -->
            <compilerArgument>-AJAVAC=-javaagent:${org.projectlombok:lombok:jar}=ECJ -jar ${org.eclipse.jdt.core.compiler:ecj:jar}</compilerArgument>
        </configuration>
    </plugin>
    

    【讨论】:

    • maven-compiler-javac 可执行文件应该是上面的 bash 脚本之一?
    • @JakubBochenski 是的,您可以使用第二个 shell 脚本。但是,如果您正在开发 lombok 扩展,则可以使用更复杂的扩展,它会自动从类路径添加引导类路径。您可以访问github.com/liudongmiao/lombok-mybatis-param了解更多详情:
    【解决方案2】:

    这是我能够做到的最好 为整个 maven 进程设置 javaagent:

       <profile>
            <id>ecj</id>
            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-dependency-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>copy-compiler</id>
                                <goals>
                                    <goal>copy</goal>
                                </goals>
                                <phase>compile</phase>
                                <configuration>
                                    <artifactItems>
                                        <artifactItem>
                                            <artifactId>lombok</artifactId>
                                            <groupId>org.projectlombok</groupId>
                                            <version>${lombok.version}</version>
                                        </artifactItem>
                                        <artifactItem>
                                            <groupId>org.eclipse.jdt.core.compiler</groupId>
                                            <artifactId>ecj</artifactId>
                                            <version>4.5.1</version>
                                        </artifactItem>
                                    </artifactItems>
                                    <stripVersion>true</stripVersion>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>exec-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>ecj-compile</id>
                                <goals>
                                    <goal>exec</goal>
                                </goals>
                                <phase>compile</phase>
                                <configuration>
                                    <executable>java</executable>
                                    <classpathScope>compile</classpathScope>
                                    <arguments>
                                        <argument>-javaagent:target/dependency/lombok.jar</argument>
                                        <argument>-jar</argument>
                                        <argument>target/dependency/ecj.jar</argument>
                                        <argument>-d</argument>
                                        <argument>none</argument>
                                        <argument>-properties</argument>
                                        <argument>.settings/org.eclipse.jdt.core.prefs</argument>
                                        <argument>-cp</argument>
                                        <classpath />
                                        <argument>${project.build.sourceDirectory}</argument>
                                    </arguments>
                                </configuration>
                            </execution>
                            <execution>
                                <id>ecj-test-compile</id>
                                <goals>
                                    <goal>exec</goal>
                                </goals>
                                <phase>test-compile</phase>
                                <configuration>
                                    <executable>java</executable>
                                    <classpathScope>test</classpathScope>
                                    <arguments>
                                        <argument>-javaagent:target/dependency/lombok.jar</argument>
                                        <argument>-jar</argument>
                                        <argument>target/dependency/ecj.jar</argument>
                                        <argument>-d</argument>
                                        <argument>none</argument>
                                        <argument>-properties</argument>
                                        <argument>.settings/org.eclipse.jdt.core.prefs</argument>
                                        <argument>-cp</argument>
                                        <classpath />
                                        <argument>${project.build.testSourceDirectory}</argument>
                                    </arguments>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    

    【讨论】:

      猜你喜欢
      • 2019-09-18
      • 1970-01-01
      • 1970-01-01
      • 2013-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多