我正在尝试使用 Google Error Prone 库为我的项目创建 .refaster 规则。
我的项目正在使用 Maven 和 JDK 17 并具有以下 pom.xml:
<dependencies>
<dependency>
<groupId>com.google.errorprone</groupId>
<artifactId>error_prone_refaster</artifactId>
<version>2.23.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<release>17</release>
<compilerArgs>
<arg>-Xplugin:RefasterRuleCompiler --out ${project.basedir}/../emptystring.refaster</arg>
</compilerArgs>
<annotationProcessorPaths>
<path>
<groupId>com.google.errorprone</groupId>
<artifactId>error_prone_core</artifactId>
<version>2.23.0</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
当我执行命令
mvn clean install
时,抛出以下错误:
Compilation failure
plug-in not found: RefasterRuleCompiler
此外,我还在 .mvn/jvm.config 中添加了以下配置,以便与 Java 17 一起运行,如官方文档中所示:
--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED
--add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED
--add-exports jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED
--add-exports jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED
--add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED
--add-exports jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED
--add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED
--add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED
--add-opens jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED
--add-opens jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED
注意: 我之前也使用 Error Prone Core 启动了一个项目,并且成功地使用 JDK 17 运行,但是当我尝试使用 Error Prone Refaster 运行该项目时,它失败了。
另外,我将
JAVA_HOME
设置为 JDK 17 并尝试从终端运行该项目,错误仍然存在。
如何解决这个问题?
发现问题了。请正确阅读文档。
我遵循了 Google Error Prone 的官方文档 这里。
有几件事需要解决。
error_prone_refaster
中添加 annotationProcessorPaths
依赖项才能使插件正常工作。这将修复错误:错误:找不到插件:RefasterRuleCompiler。
注意: 文档中没有提到这一点。我必须通过点击和尝试方法才能找到解决方案。
否则,如果你不将
Error: plug-in not found: RefasterRuleCompiler
依赖添加到 error_prone_refaster
中,你将面临 annotationProcessorPaths
:
➜ maven-error-prone-test git:(master) ✗ mvn clean install
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------< org.example:maven-error-prone-test >-----------------
[INFO] Building maven-error-prone-test 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven-error-prone-test ---
[INFO] Deleting /Users/anish/maven-error-prone-test/target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-error-prone-test ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.10.1:compile (default-compile) @ maven-error-prone-test ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/anish/maven-error-prone-test/target/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] error: plug-in not found: RefasterRuleCompiler
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.526 s
[INFO] Finished at: 2023-10-26T20:45:15+05:30
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.10.1:compile (default-compile) on project maven-error-prone-test: Compilation failure
[ERROR] error: plug-in not found: RefasterRuleCompiler
[ERROR]
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
所以,必须添加它。
我看到你错过了。所以,请解决这个问题。
我的做法示例:
<arg>-Xplugin:RefasterRuleCompiler --out ${project.basedir}/src/main/resources/refaster_files/emptystring.refaster ${project.basedir}/src/main/org/example/StringIsEmpty.java</arg>
Scratch 演示项目:
项目结构(运行之前
mvn clean install
):
StringIsEmpty.java(示例代码取自 Google Error Prone):
package org.example;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.AlsoNegation;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
public class StringIsEmpty {
@BeforeTemplate
boolean equalsEmptyString(String string) {
return string.equals("");
}
@BeforeTemplate
boolean lengthEquals0(String string) {
return string.length() == 0;
}
@AfterTemplate
@AlsoNegation
boolean optimizedMethod(String string) {
return string.isEmpty();
}
}
工作pom.xml:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>maven-error-prone-test</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<error-prone.version>2.23.0</error-prone.version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.google.errorprone/error_prone_core -->
<dependency>
<groupId>com.google.errorprone</groupId>
<artifactId>error_prone_core</artifactId>
<version>${error-prone.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.errorprone/error_prone_core -->
<dependency>
<groupId>com.google.errorprone</groupId>
<artifactId>error_prone_refaster</artifactId>
<version>${error-prone.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>17</source>
<target>17</target>
<encoding>UTF-8</encoding>
<fork>true</fork>
<compilerArgs>
<arg>-Xplugin:RefasterRuleCompiler --out ${project.basedir}/src/main/resources/refaster_files/emptystring.refaster ${project.basedir}/src/main/org/example/StringIsEmpty.java</arg>
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED</arg>
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED</arg>
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED</arg>
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED</arg>
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED</arg>
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED</arg>
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED</arg>
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED</arg>
<arg>-J--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED</arg>
<arg>-J--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED</arg>
</compilerArgs>
<annotationProcessorPaths>
<path>
<groupId>com.google.errorprone</groupId>
<artifactId>error_prone_refaster</artifactId>
<version>${error-prone.version}</version>
</path>
<path>
<groupId>com.google.errorprone</groupId>
<artifactId>error_prone_core</artifactId>
<version>${error-prone.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>
mvn clean install
成功日志:
➜ maven-error-prone-test git:(master) ✗ mvn clean install
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------< org.example:maven-error-prone-test >-----------------
[INFO] Building maven-error-prone-test 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven-error-prone-test ---
[INFO] Deleting /Users/anish/maven-error-prone-test/target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-error-prone-test ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.10.1:compile (default-compile) @ maven-error-prone-test ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/anish/maven-error-prone-test/target/classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-error-prone-test ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/anish/maven-error-prone-test/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.10.1:testCompile (default-testCompile) @ maven-error-prone-test ---
[INFO] Changes detected - recompiling the module!
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-error-prone-test ---
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ maven-error-prone-test ---
[INFO] Building jar: /Users/anish/maven-error-prone-test/target/maven-error-prone-test-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ maven-error-prone-test ---
[INFO] Installing /Users/anish/maven-error-prone-test/target/maven-error-prone-test-1.0-SNAPSHOT.jar to /Users/anish/.m2/repository/org/example/maven-error-prone-test/1.0-SNAPSHOT/maven-error-prone-test-1.0-SNAPSHOT.jar
[INFO] Installing /Users/anish/maven-error-prone-test/pom.xml to /Users/anish/.m2/repository/org/example/maven-error-prone-test/1.0-SNAPSHOT/maven-error-prone-test-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.502 s
[INFO] Finished at: 2023-10-26T20:39:21+05:30
[INFO] ------------------------------------------------------------------------
项目结构(运行后
mvn clean install
):
如您所见,生成了 .refaster 文件:)