Skip to content

Commit e9b0ca6

Browse files
committed
Refactor compiler mojo to use dependency injection and improve string handling
1 parent 8ea735c commit e9b0ca6

7 files changed

Lines changed: 34 additions & 28 deletions

File tree

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ under the License.
117117
<artifactId>maven-shared-utils</artifactId>
118118
<version>3.4.2</version>
119119
</dependency>
120+
<dependency>
121+
<groupId>commons-io</groupId>
122+
<artifactId>commons-io</artifactId>
123+
<version>2.22.0</version>
124+
</dependency>
120125
<dependency>
121126
<groupId>org.apache.maven.shared</groupId>
122127
<artifactId>maven-shared-incremental</artifactId>

src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
*/
1919
package org.apache.maven.plugin.compiler;
2020

21+
import javax.inject.Inject;
22+
2123
import java.io.File;
2224
import java.io.IOException;
2325
import java.io.InputStream;
@@ -54,7 +56,6 @@
5456
import org.apache.maven.plugin.AbstractMojo;
5557
import org.apache.maven.plugin.MojoExecution;
5658
import org.apache.maven.plugin.MojoExecutionException;
57-
import org.apache.maven.plugins.annotations.Component;
5859
import org.apache.maven.plugins.annotations.Parameter;
5960
import org.apache.maven.project.MavenProject;
6061
import org.apache.maven.shared.incremental.IncrementalBuildHelper;
@@ -103,7 +104,7 @@
103104
* @since 2.0
104105
*/
105106
public abstract class AbstractCompilerMojo extends AbstractMojo {
106-
protected static final String PS = System.getProperty("path.separator");
107+
protected static final String PS = File.pathSeparator;
107108

108109
private static final String INPUT_FILES_LST_FILENAME = "inputFiles.lst";
109110

@@ -473,7 +474,7 @@ public abstract class AbstractCompilerMojo extends AbstractMojo {
473474
/**
474475
*
475476
*/
476-
@Component
477+
@Inject
477478
private ToolchainManager toolchainManager;
478479

479480
/**
@@ -527,7 +528,7 @@ public abstract class AbstractCompilerMojo extends AbstractMojo {
527528
/**
528529
* Plexus compiler manager.
529530
*/
530-
@Component
531+
@Inject
531532
private CompilerManager compilerManager;
532533

533534
/**
@@ -648,13 +649,13 @@ public abstract class AbstractCompilerMojo extends AbstractMojo {
648649
/**
649650
* Resolves the artifacts needed.
650651
*/
651-
@Component
652+
@Inject
652653
private RepositorySystem repositorySystem;
653654

654655
/**
655656
* Artifact handler manager.
656657
*/
657-
@Component
658+
@Inject
658659
private ArtifactHandlerManager artifactHandlerManager;
659660

660661
protected abstract SourceInclusionScanner getSourceInclusionScanner(int staleMillis);
@@ -722,13 +723,13 @@ private void addGeneratedSourcesToProject() {
722723
if (isTestCompile()) {
723724
getLog().debug("Adding " + generatedSourcesPath
724725
+ " to the project test-compile source roots but NOT the actual test-compile source roots:\n "
725-
+ StringUtils.join(project.getTestCompileSourceRoots().iterator(), "\n "));
726+
+ String.join("\n ", project.getTestCompileSourceRoots()));
726727

727728
project.addTestCompileSourceRoot(generatedSourcesPath);
728729
} else {
729730
getLog().debug("Adding " + generatedSourcesPath
730731
+ " to the project compile source roots but NOT the actual compile source roots:\n "
731-
+ StringUtils.join(project.getCompileSourceRoots().iterator(), "\n "));
732+
+ String.join("\n ", project.getCompileSourceRoots()));
732733

733734
project.addCompileSourceRoot(generatedSourcesPath);
734735
}
@@ -787,7 +788,7 @@ private void executeReal() throws MojoExecutionException, CompilationFailureExce
787788

788789
writePlugin(mb);
789790

790-
getLog().warn(mb.toString());
791+
getLog().warn(mb.build());
791792
}
792793

793794
// ----------------------------------------------------------------------
@@ -906,7 +907,7 @@ private void executeReal() throws MojoExecutionException, CompilationFailureExce
906907
getLog().warn("You are in a multi-thread build and compilerReuseStrategy is set to reuseSame."
907908
+ " This can cause issues in some environments (os/jdk)!"
908909
+ " Consider using reuseCreated strategy."
909-
+ System.getProperty("line.separator")
910+
+ System.lineSeparator()
910911
+ "If your env is fine with reuseSame, you can skip this warning with the "
911912
+ "configuration field skipMultiThreadWarning "
912913
+ "or -Dmaven.compiler.skipMultiThreadWarning=true");

src/main/java/org/apache/maven/plugin/compiler/CompilationFailureException.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@
2727
* @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
2828
* @since 2.0
2929
*/
30-
@SuppressWarnings("serial")
3130
public class CompilationFailureException extends MojoFailureException {
32-
private static final String LS = System.getProperty("line.separator");
31+
private static final String LS = System.lineSeparator();
3332

3433
/**
3534
* Wrap error messages from the compiler

src/main/java/org/apache/maven/plugin/compiler/CompilerMojo.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
import org.apache.maven.shared.utils.StringUtils;
4444
import org.apache.maven.shared.utils.logging.MessageUtils;
4545
import org.apache.maven.toolchain.Toolchain;
46-
import org.apache.maven.toolchain.java.DefaultJavaToolChain;
46+
import org.apache.maven.toolchain.java.JavaToolchainImpl;
4747
import org.codehaus.plexus.compiler.util.scan.SimpleSourceInclusionScanner;
4848
import org.codehaus.plexus.compiler.util.scan.SourceInclusionScanner;
4949
import org.codehaus.plexus.compiler.util.scan.StaleSourceScanner;
@@ -72,7 +72,7 @@ public class CompilerMojo extends AbstractCompilerMojo {
7272
/**
7373
* The source directories containing the sources to be compiled.
7474
*/
75-
@Parameter(defaultValue = "${project.compileSourceRoots}", readonly = false, required = true)
75+
@Parameter(defaultValue = "${project.compileSourceRoots}", required = true)
7676
private List<String> compileSourceRoots;
7777

7878
/**
@@ -90,8 +90,7 @@ public class CompilerMojo extends AbstractCompilerMojo {
9090
@Parameter(
9191
property = "maven.compiler.outputDirectory",
9292
defaultValue = "${project.build.outputDirectory}",
93-
required = true,
94-
readonly = false)
93+
required = true)
9594
private File outputDirectory;
9695

9796
/**
@@ -277,8 +276,8 @@ protected void preparePaths(Set<File> sourceFiles) {
277276
.setMainModuleDescriptor(moduleDeclaration.get().toFile());
278277

279278
Toolchain toolchain = getToolchain();
280-
if (toolchain instanceof DefaultJavaToolChain) {
281-
request.setJdkHome(new File(((DefaultJavaToolChain) toolchain).getJavaHome()));
279+
if (toolchain instanceof JavaToolchainImpl) {
280+
request.setJdkHome(new File(((JavaToolchainImpl) toolchain).getJavaHome()));
282281
}
283282

284283
resolvePathsResult = locationManager.resolvePaths(request);
@@ -437,6 +436,7 @@ protected String getCompilerArgument() {
437436
return compilerArgument;
438437
}
439438

439+
@SuppressWarnings("deprecation")
440440
@Override
441441
protected Map<String, String> getCompilerArguments() {
442442
return compilerArguments;

src/main/java/org/apache/maven/plugin/compiler/TestCompilerMojo.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
import org.apache.maven.plugins.annotations.ResolutionScope;
4040
import org.apache.maven.shared.utils.StringUtils;
4141
import org.apache.maven.toolchain.Toolchain;
42-
import org.apache.maven.toolchain.java.DefaultJavaToolChain;
42+
import org.apache.maven.toolchain.java.JavaToolchainImpl;
4343
import org.codehaus.plexus.compiler.util.scan.SimpleSourceInclusionScanner;
4444
import org.codehaus.plexus.compiler.util.scan.SourceInclusionScanner;
4545
import org.codehaus.plexus.compiler.util.scan.StaleSourceScanner;
@@ -74,7 +74,7 @@ public class TestCompilerMojo extends AbstractCompilerMojo {
7474
/**
7575
* The source directories containing the test-source to be compiled.
7676
*/
77-
@Parameter(defaultValue = "${project.testCompileSourceRoots}", readonly = false, required = true)
77+
@Parameter(defaultValue = "${project.testCompileSourceRoots}", required = true)
7878
private List<String> compileSourceRoots;
7979

8080
/**
@@ -85,7 +85,7 @@ public class TestCompilerMojo extends AbstractCompilerMojo {
8585
*
8686
* @see CompilerMojo#outputDirectory
8787
*/
88-
@Parameter(defaultValue = "${project.build.testOutputDirectory}", required = true, readonly = false)
88+
@Parameter(defaultValue = "${project.build.testOutputDirectory}", required = true)
8989
private File outputDirectory;
9090

9191
/**
@@ -260,8 +260,8 @@ protected void preparePaths(Set<File> sourceFiles) {
260260
.setMainModuleDescriptor(mainModuleDescriptorClassFile.getAbsolutePath());
261261

262262
Toolchain toolchain = getToolchain();
263-
if (toolchain instanceof DefaultJavaToolChain) {
264-
request.setJdkHome(((DefaultJavaToolChain) toolchain).getJavaHome());
263+
if (toolchain instanceof JavaToolchainImpl) {
264+
request.setJdkHome(((JavaToolchainImpl) toolchain).getJavaHome());
265265
}
266266

267267
result = locationManager.resolvePaths(request);
@@ -298,8 +298,8 @@ protected void preparePaths(Set<File> sourceFiles) {
298298
.setMainModuleDescriptor(testModuleDescriptorJavaFile.getAbsolutePath());
299299

300300
Toolchain toolchain = getToolchain();
301-
if (toolchain instanceof DefaultJavaToolChain) {
302-
request.setJdkHome(((DefaultJavaToolChain) toolchain).getJavaHome());
301+
if (toolchain instanceof JavaToolchainImpl) {
302+
request.setJdkHome(((JavaToolchainImpl) toolchain).getJavaHome());
303303
}
304304

305305
result = locationManager.resolvePaths(request);
@@ -462,6 +462,7 @@ protected String getCompilerArgument() {
462462
return testCompilerArgument == null ? compilerArgument : testCompilerArgument;
463463
}
464464

465+
@SuppressWarnings("deprecation")
465466
protected Map<String, String> getCompilerArguments() {
466467
return testCompilerArguments == null ? compilerArguments : testCompilerArguments;
467468
}

src/test/java/org/apache/maven/plugin/compiler/CompilerMojoTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ void testCompilerBasic(CompilerMojo compilerMojo) throws Exception {
6868
setVariableValueToObject(compilerMojo, "targetOrReleaseSet", false);
6969
compilerMojo.execute();
7070

71-
Artifact projectArtifact = (Artifact) getVariableValueFromObject(compilerMojo, "projectArtifact");
71+
Artifact projectArtifact = getVariableValueFromObject(compilerMojo, "projectArtifact");
7272
assertNotNull(
7373
projectArtifact.getFile(),
7474
"MCOMPILER-94: artifact file should only be null if there is nothing to compile");
@@ -108,7 +108,7 @@ void testCompilerEmptySource(CompilerMojo compilerMojo) throws Exception {
108108

109109
assertFalse(compilerMojo.getOutputDirectory().exists());
110110

111-
Artifact projectArtifact = (Artifact) getVariableValueFromObject(compilerMojo, "projectArtifact");
111+
Artifact projectArtifact = getVariableValueFromObject(compilerMojo, "projectArtifact");
112112
assertNull(
113113
projectArtifact.getFile(), "MCOMPILER-94: artifact file should be null if there is nothing to compile");
114114
}

src/test/java/org/apache/maven/plugin/compiler/TestCompilerMojoTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ void testCompileSkipTest(TestCompilerMojo testCompilerMojo) throws Exception {
170170
}
171171

172172
private void setUpCompilerMojoTestEnv(TestCompilerMojo mojo) throws Exception {
173-
File buildDir = (File) getVariableValueFromObject(mojo, "buildDirectory");
173+
File buildDir = getVariableValueFromObject(mojo, "buildDirectory");
174174
File testClassesDir = new File(buildDir, "test-classes");
175175
setVariableValueToObject(mojo, "outputDirectory", testClassesDir);
176176

0 commit comments

Comments
 (0)