将Maven编译器插件与Eclipse Compiler和Lombok一起使用

时间:2016-04-18 14:03:12

标签: eclipse maven lombok maven-compiler-plugin

有没有办法在ECJ 中编译lomboked代码而不为maven进程设置lombok as javaaagent

下面的代码段仅在我使用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会产生通常的无错误错误:__ 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>

但它似乎没有任何效果。

2 个答案:

答案 0 :(得分:1)

简而言之,没有办法。但是,有一个问题。

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

但是,默认实现是&#34; plexus-compiler-javac&#34;,它支持自定义executable。解决方法可能是这样的:将fork设置为true,并指定executable。它可能是这样的:

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

或者,直接使用项目中的ecj,在-AJAVAC中定义pom.xml :( -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>

答案 1 :(得分:0)

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

def id[X](x: X): X = x

map(id[Int] _)(l1) == List(3)
id(l1) == List(3,2,1)