I am trying to create .refaster rules using Google Error Prone library for my project.
My project is using Maven & JDK 17 and has the following 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>
When I'm executing the command mvn clean install
, the following error is thrown:
Compilation failure
plug-in not found: RefasterRuleCompiler
Also, I have added the following configuration in the .mvn/jvm.config to run with JDK 17 like indicated in the official documentation:
--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
Note: I started a project with Error Prone Core before also and I have succeeded to run with JDK 17 but when I try to run the project with Error Prone Refaster, it's failing.
Also, I set the JAVA_HOME
to JDK 17 and tried running the project from the terminal, the error still persisted.
How to fix this?
Found the issue. Please read the docs properly.
I followed the Official Docs from Google Error Prone here.
Couple of things need to be fixed.
error_prone_refaster
dependency in your annotationProcessorPaths
to make the plugin work.This will fix the error: Error: plug-in not found: RefasterRuleCompiler.
Note: This was not mentioned in the docs. I had to do hit and trial method to get to the solution.
Otherwise, you will face Error: plug-in not found: RefasterRuleCompiler
if you don't add error_prone_refaster
dependency to the 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
So, it's mandatory to add it.
I saw that you missed to do it. So, fix this please.
Example on how I did:
<arg>-Xplugin:RefasterRuleCompiler --out ${project.basedir}/src/main/resources/refaster_files/emptystring.refaster ${project.basedir}/src/main/org/example/StringIsEmpty.java</arg>
Scratch Project for demonstration:
Project Structure (Before running mvn clean install
):
StringIsEmpty.java (example code taken from 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();
}
}
Working 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_refaster -->
<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
success logs:
➜ 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] ------------------------------------------------------------------------
Project Structure (after running mvn clean install
):
As you can see the .refaster file was generated :)