This is an automated email from the ASF dual-hosted git repository.
slachiewicz pushed a commit to branch maven-compiler-plugin-3.x
in repository https://gitbox.apache.org/repos/asf/maven-compiler-plugin.git
The following commit(s) were added to refs/heads/maven-compiler-plugin-3.x by
this push:
new e9b0ca6 Refactor compiler mojo to use dependency injection and
improve string handling
e9b0ca6 is described below
commit e9b0ca6b2093e6d078c190d4a24d42fc231f629e
Author: Sylwester Lachiewicz <[email protected]>
AuthorDate: Sun Jun 28 15:05:22 2026 +0200
Refactor compiler mojo to use dependency injection and improve string
handling
---
pom.xml | 5 +++++
.../maven/plugin/compiler/AbstractCompilerMojo.java | 21 +++++++++++----------
.../compiler/CompilationFailureException.java | 3 +--
.../apache/maven/plugin/compiler/CompilerMojo.java | 12 ++++++------
.../maven/plugin/compiler/TestCompilerMojo.java | 15 ++++++++-------
.../maven/plugin/compiler/CompilerMojoTest.java | 4 ++--
.../maven/plugin/compiler/TestCompilerMojoTest.java | 2 +-
7 files changed, 34 insertions(+), 28 deletions(-)
diff --git a/pom.xml b/pom.xml
index b3415e3..eed5175 100644
--- a/pom.xml
+++ b/pom.xml
@@ -117,6 +117,11 @@ under the License.
<artifactId>maven-shared-utils</artifactId>
<version>3.4.2</version>
</dependency>
+ <dependency>
+ <groupId>commons-io</groupId>
+ <artifactId>commons-io</artifactId>
+ <version>2.22.0</version>
+ </dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-shared-incremental</artifactId>
diff --git
a/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java
b/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java
index 1aebf4c..edb9ad7 100644
--- a/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java
+++ b/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java
@@ -18,6 +18,8 @@
*/
package org.apache.maven.plugin.compiler;
+import javax.inject.Inject;
+
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
@@ -54,7 +56,6 @@ import org.apache.maven.model.DependencyManagement;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.MojoExecutionException;
-import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.shared.incremental.IncrementalBuildHelper;
@@ -103,7 +104,7 @@ import org.objectweb.asm.Opcodes;
* @since 2.0
*/
public abstract class AbstractCompilerMojo extends AbstractMojo {
- protected static final String PS = System.getProperty("path.separator");
+ protected static final String PS = File.pathSeparator;
private static final String INPUT_FILES_LST_FILENAME = "inputFiles.lst";
@@ -473,7 +474,7 @@ public abstract class AbstractCompilerMojo extends
AbstractMojo {
/**
*
*/
- @Component
+ @Inject
private ToolchainManager toolchainManager;
/**
@@ -527,7 +528,7 @@ public abstract class AbstractCompilerMojo extends
AbstractMojo {
/**
* Plexus compiler manager.
*/
- @Component
+ @Inject
private CompilerManager compilerManager;
/**
@@ -648,13 +649,13 @@ public abstract class AbstractCompilerMojo extends
AbstractMojo {
/**
* Resolves the artifacts needed.
*/
- @Component
+ @Inject
private RepositorySystem repositorySystem;
/**
* Artifact handler manager.
*/
- @Component
+ @Inject
private ArtifactHandlerManager artifactHandlerManager;
protected abstract SourceInclusionScanner getSourceInclusionScanner(int
staleMillis);
@@ -722,13 +723,13 @@ public abstract class AbstractCompilerMojo extends
AbstractMojo {
if (isTestCompile()) {
getLog().debug("Adding " + generatedSourcesPath
+ " to the project test-compile source roots but NOT the
actual test-compile source roots:\n "
- +
StringUtils.join(project.getTestCompileSourceRoots().iterator(), "\n "));
+ + String.join("\n ",
project.getTestCompileSourceRoots()));
project.addTestCompileSourceRoot(generatedSourcesPath);
} else {
getLog().debug("Adding " + generatedSourcesPath
+ " to the project compile source roots but NOT the actual
compile source roots:\n "
- +
StringUtils.join(project.getCompileSourceRoots().iterator(), "\n "));
+ + String.join("\n ", project.getCompileSourceRoots()));
project.addCompileSourceRoot(generatedSourcesPath);
}
@@ -787,7 +788,7 @@ public abstract class AbstractCompilerMojo extends
AbstractMojo {
writePlugin(mb);
- getLog().warn(mb.toString());
+ getLog().warn(mb.build());
}
//
----------------------------------------------------------------------
@@ -906,7 +907,7 @@ public abstract class AbstractCompilerMojo extends
AbstractMojo {
getLog().warn("You are in a multi-thread build and
compilerReuseStrategy is set to reuseSame."
+ " This can cause issues in some environments
(os/jdk)!"
+ " Consider using reuseCreated strategy."
- + System.getProperty("line.separator")
+ + System.lineSeparator()
+ "If your env is fine with reuseSame, you can
skip this warning with the "
+ "configuration field skipMultiThreadWarning "
+ "or
-Dmaven.compiler.skipMultiThreadWarning=true");
diff --git
a/src/main/java/org/apache/maven/plugin/compiler/CompilationFailureException.java
b/src/main/java/org/apache/maven/plugin/compiler/CompilationFailureException.java
index fa55b16..4239115 100644
---
a/src/main/java/org/apache/maven/plugin/compiler/CompilationFailureException.java
+++
b/src/main/java/org/apache/maven/plugin/compiler/CompilationFailureException.java
@@ -27,9 +27,8 @@ import org.codehaus.plexus.compiler.CompilerMessage;
* @author <a href="mailto:[email protected]">Jason van Zyl</a>
* @since 2.0
*/
-@SuppressWarnings("serial")
public class CompilationFailureException extends MojoFailureException {
- private static final String LS = System.getProperty("line.separator");
+ private static final String LS = System.lineSeparator();
/**
* Wrap error messages from the compiler
diff --git a/src/main/java/org/apache/maven/plugin/compiler/CompilerMojo.java
b/src/main/java/org/apache/maven/plugin/compiler/CompilerMojo.java
index 295ca39..0638d29 100644
--- a/src/main/java/org/apache/maven/plugin/compiler/CompilerMojo.java
+++ b/src/main/java/org/apache/maven/plugin/compiler/CompilerMojo.java
@@ -43,7 +43,7 @@ import org.apache.maven.project.MavenProject;
import org.apache.maven.shared.utils.StringUtils;
import org.apache.maven.shared.utils.logging.MessageUtils;
import org.apache.maven.toolchain.Toolchain;
-import org.apache.maven.toolchain.java.DefaultJavaToolChain;
+import org.apache.maven.toolchain.java.JavaToolchainImpl;
import org.codehaus.plexus.compiler.util.scan.SimpleSourceInclusionScanner;
import org.codehaus.plexus.compiler.util.scan.SourceInclusionScanner;
import org.codehaus.plexus.compiler.util.scan.StaleSourceScanner;
@@ -72,7 +72,7 @@ public class CompilerMojo extends AbstractCompilerMojo {
/**
* The source directories containing the sources to be compiled.
*/
- @Parameter(defaultValue = "${project.compileSourceRoots}", readonly =
false, required = true)
+ @Parameter(defaultValue = "${project.compileSourceRoots}", required = true)
private List<String> compileSourceRoots;
/**
@@ -90,8 +90,7 @@ public class CompilerMojo extends AbstractCompilerMojo {
@Parameter(
property = "maven.compiler.outputDirectory",
defaultValue = "${project.build.outputDirectory}",
- required = true,
- readonly = false)
+ required = true)
private File outputDirectory;
/**
@@ -277,8 +276,8 @@ public class CompilerMojo extends AbstractCompilerMojo {
.setMainModuleDescriptor(moduleDeclaration.get().toFile());
Toolchain toolchain = getToolchain();
- if (toolchain instanceof DefaultJavaToolChain) {
- request.setJdkHome(new File(((DefaultJavaToolChain)
toolchain).getJavaHome()));
+ if (toolchain instanceof JavaToolchainImpl) {
+ request.setJdkHome(new File(((JavaToolchainImpl)
toolchain).getJavaHome()));
}
resolvePathsResult = locationManager.resolvePaths(request);
@@ -437,6 +436,7 @@ public class CompilerMojo extends AbstractCompilerMojo {
return compilerArgument;
}
+ @SuppressWarnings("deprecation")
@Override
protected Map<String, String> getCompilerArguments() {
return compilerArguments;
diff --git
a/src/main/java/org/apache/maven/plugin/compiler/TestCompilerMojo.java
b/src/main/java/org/apache/maven/plugin/compiler/TestCompilerMojo.java
index 236fd67..1e0b76e 100644
--- a/src/main/java/org/apache/maven/plugin/compiler/TestCompilerMojo.java
+++ b/src/main/java/org/apache/maven/plugin/compiler/TestCompilerMojo.java
@@ -39,7 +39,7 @@ import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.shared.utils.StringUtils;
import org.apache.maven.toolchain.Toolchain;
-import org.apache.maven.toolchain.java.DefaultJavaToolChain;
+import org.apache.maven.toolchain.java.JavaToolchainImpl;
import org.codehaus.plexus.compiler.util.scan.SimpleSourceInclusionScanner;
import org.codehaus.plexus.compiler.util.scan.SourceInclusionScanner;
import org.codehaus.plexus.compiler.util.scan.StaleSourceScanner;
@@ -74,7 +74,7 @@ public class TestCompilerMojo extends AbstractCompilerMojo {
/**
* The source directories containing the test-source to be compiled.
*/
- @Parameter(defaultValue = "${project.testCompileSourceRoots}", readonly =
false, required = true)
+ @Parameter(defaultValue = "${project.testCompileSourceRoots}", required =
true)
private List<String> compileSourceRoots;
/**
@@ -85,7 +85,7 @@ public class TestCompilerMojo extends AbstractCompilerMojo {
*
* @see CompilerMojo#outputDirectory
*/
- @Parameter(defaultValue = "${project.build.testOutputDirectory}", required
= true, readonly = false)
+ @Parameter(defaultValue = "${project.build.testOutputDirectory}", required
= true)
private File outputDirectory;
/**
@@ -260,8 +260,8 @@ public class TestCompilerMojo extends AbstractCompilerMojo {
.setMainModuleDescriptor(mainModuleDescriptorClassFile.getAbsolutePath());
Toolchain toolchain = getToolchain();
- if (toolchain instanceof DefaultJavaToolChain) {
- request.setJdkHome(((DefaultJavaToolChain)
toolchain).getJavaHome());
+ if (toolchain instanceof JavaToolchainImpl) {
+ request.setJdkHome(((JavaToolchainImpl)
toolchain).getJavaHome());
}
result = locationManager.resolvePaths(request);
@@ -298,8 +298,8 @@ public class TestCompilerMojo extends AbstractCompilerMojo {
.setMainModuleDescriptor(testModuleDescriptorJavaFile.getAbsolutePath());
Toolchain toolchain = getToolchain();
- if (toolchain instanceof DefaultJavaToolChain) {
- request.setJdkHome(((DefaultJavaToolChain)
toolchain).getJavaHome());
+ if (toolchain instanceof JavaToolchainImpl) {
+ request.setJdkHome(((JavaToolchainImpl)
toolchain).getJavaHome());
}
result = locationManager.resolvePaths(request);
@@ -462,6 +462,7 @@ public class TestCompilerMojo extends AbstractCompilerMojo {
return testCompilerArgument == null ? compilerArgument :
testCompilerArgument;
}
+ @SuppressWarnings("deprecation")
protected Map<String, String> getCompilerArguments() {
return testCompilerArguments == null ? compilerArguments :
testCompilerArguments;
}
diff --git
a/src/test/java/org/apache/maven/plugin/compiler/CompilerMojoTest.java
b/src/test/java/org/apache/maven/plugin/compiler/CompilerMojoTest.java
index a7ec138..260fd94 100644
--- a/src/test/java/org/apache/maven/plugin/compiler/CompilerMojoTest.java
+++ b/src/test/java/org/apache/maven/plugin/compiler/CompilerMojoTest.java
@@ -68,7 +68,7 @@ class CompilerMojoTest {
setVariableValueToObject(compilerMojo, "targetOrReleaseSet", false);
compilerMojo.execute();
- Artifact projectArtifact = (Artifact)
getVariableValueFromObject(compilerMojo, "projectArtifact");
+ Artifact projectArtifact = getVariableValueFromObject(compilerMojo,
"projectArtifact");
assertNotNull(
projectArtifact.getFile(),
"MCOMPILER-94: artifact file should only be null if there is
nothing to compile");
@@ -108,7 +108,7 @@ class CompilerMojoTest {
assertFalse(compilerMojo.getOutputDirectory().exists());
- Artifact projectArtifact = (Artifact)
getVariableValueFromObject(compilerMojo, "projectArtifact");
+ Artifact projectArtifact = getVariableValueFromObject(compilerMojo,
"projectArtifact");
assertNull(
projectArtifact.getFile(), "MCOMPILER-94: artifact file should
be null if there is nothing to compile");
}
diff --git
a/src/test/java/org/apache/maven/plugin/compiler/TestCompilerMojoTest.java
b/src/test/java/org/apache/maven/plugin/compiler/TestCompilerMojoTest.java
index 4f18b77..20bc7ea 100644
--- a/src/test/java/org/apache/maven/plugin/compiler/TestCompilerMojoTest.java
+++ b/src/test/java/org/apache/maven/plugin/compiler/TestCompilerMojoTest.java
@@ -170,7 +170,7 @@ class TestCompilerMojoTest {
}
private void setUpCompilerMojoTestEnv(TestCompilerMojo mojo) throws
Exception {
- File buildDir = (File) getVariableValueFromObject(mojo,
"buildDirectory");
+ File buildDir = getVariableValueFromObject(mojo, "buildDirectory");
File testClassesDir = new File(buildDir, "test-classes");
setVariableValueToObject(mojo, "outputDirectory", testClassesDir);