使用Maven编译器插件与Eclipse编译器和Lombok [英] Use Maven Compiler Plugin with Eclipse Compiler and Lombok

查看:359
本文介绍了使用Maven编译器插件与Eclipse编译器和Lombok的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



下面的代码片段只能工作如果我用lombok作为代理运行mvn

 < profile> 
< id> ecj< / id>
< build>
< pluginManagement>
< plugins>
< plugin>
< artifactId> maven-compiler-plugin< / artifactId>
< configuration>
< compilerId> eclipse< / compilerId>
< / configuration>
<依赖关系>
<依赖关系>
< groupId> org.codehaus.plexus< / groupId>
< artifactId> plexus-compiler-eclipse< / artifactId>
< version> 2.8-SNAPSHOT< / version>
< / 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错误: __无法解析为类型



我尝试使用 com.reubenpeeris.maven:lombok-eclipse-compiler: 1.3
但是这个失败,而编译失败
无法识别的选项:target / generated-sources / annotations
这个编译器太旧了。



我还尝试添加

  ;叉>真< /叉> 
< compilerArgs>
< arg> -javaagent:lombok.jar< / arg>
< / compilerArgs>

但似乎没有任何效果。

解决方案

总之,没办法。但是,有wrokaround。



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



但是,默认实现是plexus-compiler- javac,它支持自定义可执行文件。解决方法可能会这样:将 fork 设置为 true ,并指定可执行文件。它可能是这样的:

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

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

 #!/ bin / bash 

#另存为maven-compiler-javac
for last;做

done

如果[$ {last:0:1} ==@];那么
file = $ {last:1}
如果[-f$ file];那么
#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 =];然后
exec java $ {last:8} $ @
else
exec javac $ @
fi

更改pom.xml:

  ;! - 使用$ {org.projectlombok:lombok:jar}等等 - > 
< plugin>
< groupId> org.apache.maven.plugins< / groupId>
< artifactId> maven-dependency-plugin< / artifactId>
< version> 2.10< / version>
<执行>
< 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使用org.codehaus.plexus:plexus-compiler-eclipse,我喜欢org.eclipse.jdt.core.compiler:ecj - >
<! - 如果你正在开发lombok扩展,你应该添加-Xbootclasspath / a: - >
< compilerArgument> -AJAVAC = -javaagent:$ {org.projectlombok:lombok:jar} = ECJ -jar $ {org.eclipse.jdt.core.compiler:ecj:jar}< / compilerArgument>
< / configuration>
< / plugin>


Is there a way to compile lomboked code in ECJ without setting lombok as javaaagent for the maven process?

The snippet below only works if I run mvn with lombok as agent

<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 results in successfull compilation.

However running just mvn -P ecj spews the usual no-lombok errors like: __ cannot be resolved to a type

I tried using com.reubenpeeris.maven:lombok-eclipse-compiler:1.3 but this fails with Compilation failure Unrecognized option: target/generated-sources/annotations, which I think means this compiler is too old.

I also tried adding

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

but it doesn't seem to have any effect.

解决方案

In short, there is no way. However, there is wrokaround.

The eclipse implementation is plexus-compiler-eclipse, it doesn't accept fork argument, as it uses the internal jvm. So, it can only accept jvm options like MAVEN_OPTS.

However, the default implementation is "plexus-compiler-javac", which supports custom executable. The workaround may like this: set fork to true, and specify the executable. It maybe like this:

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

or, use the ecj from the project directly, define -AJAVAC in the pom.xml: (-AXXX=XXX is acceptable by 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

change the 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编译器插件与Eclipse编译器和Lombok的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆