From 53fbc2d381afe2888325f3beac697a5cf4da693c Mon Sep 17 00:00:00 2001
From: Henning Schmiedehausen
Date: Thu, 6 Jul 2017 14:43:39 -0700
Subject: [PATCH] Adds new "failOnWarning" configuration setting.
This allows using a flag to turn compilation failure based on warnings on or off without
having to modify the compiler arguments. Uses boolean property exposed by newer plexus compiler API.
---
.../src/it/fail-on-warning/invoker.properties | 20 ++++++
.../src/it/fail-on-warning/pom.xml | 66 +++++++++++++++++++
.../src/main/java/org/maven/test/Main.java | 35 ++++++++++
.../src/it/fail-on-warning/verify.groovy | 25 +++++++
.../plugin/compiler/AbstractCompilerMojo.java | 41 ++++++------
.../plugin/compiler/CompilerMojoTestCase.java | 19 ++++++
.../compiler/stubs/CompilerManagerStub.java | 12 +++-
.../plugin/compiler/stubs/CompilerStub.java | 27 ++++++--
.../plugin-config.xml | 38 +++++++++++
.../src/main/java/TestCompile0.java | 32 +++++++++
.../src/test/java/TestCompile0Test.java | 29 ++++++++
11 files changed, 318 insertions(+), 26 deletions(-)
create mode 100644 maven-compiler-plugin/src/it/fail-on-warning/invoker.properties
create mode 100644 maven-compiler-plugin/src/it/fail-on-warning/pom.xml
create mode 100644 maven-compiler-plugin/src/it/fail-on-warning/src/main/java/org/maven/test/Main.java
create mode 100644 maven-compiler-plugin/src/it/fail-on-warning/verify.groovy
create mode 100644 maven-compiler-plugin/src/test/resources/unit/compiler-failonwarning-test/plugin-config.xml
create mode 100644 maven-compiler-plugin/src/test/resources/unit/compiler-failonwarning-test/src/main/java/TestCompile0.java
create mode 100644 maven-compiler-plugin/src/test/resources/unit/compiler-failonwarning-test/src/test/java/TestCompile0Test.java
diff --git a/maven-compiler-plugin/src/it/fail-on-warning/invoker.properties b/maven-compiler-plugin/src/it/fail-on-warning/invoker.properties
new file mode 100644
index 0000000000..5a2c556d01
--- /dev/null
+++ b/maven-compiler-plugin/src/it/fail-on-warning/invoker.properties
@@ -0,0 +1,20 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+invoker.goals = clean compile
+invoker.buildResult = failure
+invoker.java.version = 1.6+
diff --git a/maven-compiler-plugin/src/it/fail-on-warning/pom.xml b/maven-compiler-plugin/src/it/fail-on-warning/pom.xml
new file mode 100644
index 0000000000..5c7ae2e802
--- /dev/null
+++ b/maven-compiler-plugin/src/it/fail-on-warning/pom.xml
@@ -0,0 +1,66 @@
+
+
+
+
+ 4.0.0
+ org.apache.maven.plugins.compiler.it
+ fail-on-warning
+ 1.0-SNAPSHOT
+ Werror warnings build
+
+
+
+ junit
+ junit
+ 4.4
+ test
+
+
+
+ log4j
+ log4j
+ 1.2.14
+
+
+
+
+
+
+ maven-compiler-plugin
+ @project.version@
+
+ 1.6
+ 1.6
+ true
+ true
+ true
+ true
+ true
+ true
+
+ -Xlint:all,-options,-path
+
+ true
+
+
+
+
+
diff --git a/maven-compiler-plugin/src/it/fail-on-warning/src/main/java/org/maven/test/Main.java b/maven-compiler-plugin/src/it/fail-on-warning/src/main/java/org/maven/test/Main.java
new file mode 100644
index 0000000000..15e30db330
--- /dev/null
+++ b/maven-compiler-plugin/src/it/fail-on-warning/src/main/java/org/maven/test/Main.java
@@ -0,0 +1,35 @@
+package org.maven.test;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.util.ArrayList;
+import java.util.List;
+
+
+
+public class Main {
+ /**
+ * @param args
+ */
+ public static void main(String[] args) {
+ List blah = new ArrayList();
+ blah.add("hello");
+ }
+}
diff --git a/maven-compiler-plugin/src/it/fail-on-warning/verify.groovy b/maven-compiler-plugin/src/it/fail-on-warning/verify.groovy
new file mode 100644
index 0000000000..6295c7f89b
--- /dev/null
+++ b/maven-compiler-plugin/src/it/fail-on-warning/verify.groovy
@@ -0,0 +1,25 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+def logFile = new File( basedir, 'build.log' )
+assert logFile.exists()
+content = logFile.text
+
+assert content.contains( 'Compilation failure' )
+assert !content.contains( 'invalid flag' )
+assert content.contains( 'unchecked call to add(E) as a member of the raw type ' ) // List or java.util.List
diff --git a/maven-compiler-plugin/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java b/maven-compiler-plugin/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java
index a7857e9a78..8ee3dd6332 100644
--- a/maven-compiler-plugin/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java
+++ b/maven-compiler-plugin/src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java
@@ -85,12 +85,12 @@ public abstract class AbstractCompilerMojo
{
static final String DEFAULT_SOURCE = "1.5";
-
+
static final String DEFAULT_TARGET = "1.5";
-
+
// Used to compare with older targets
static final String MODULE_INFO_TARGET = "1.9";
-
+
// ----------------------------------------------------------------------
// Configurables
// ----------------------------------------------------------------------
@@ -102,14 +102,14 @@ public abstract class AbstractCompilerMojo
*/
@Parameter( property = "maven.compiler.failOnError", defaultValue = "true" )
private boolean failOnError = true;
-
+
/**
* Indicates whether the build will continue even if there are compilation warnings.
*
* @since 3.6
*/
@Parameter( property = "maven.compiler.failOnWarning", defaultValue = "false" )
- private boolean failOnWarning;
+ private boolean failOnWarning;
/**
* Set to true to include debugging information in the compiled class files.
@@ -162,12 +162,12 @@ public abstract class AbstractCompilerMojo
/**
* The -release argument for the Java compiler, supported since Java9
- *
+ *
* @since 3.6
*/
@Parameter( property = "maven.compiler.release" )
protected String release;
-
+
/**
* The -encoding argument for the Java compiler.
*
@@ -372,7 +372,7 @@ public abstract class AbstractCompilerMojo
* This overrules the toolchain selected by the maven-toolchain-plugin.
*
* note: requires at least Maven 3.3.1
- *
+ *
* @since 3.6
*/
@Parameter
@@ -493,7 +493,7 @@ public abstract class AbstractCompilerMojo
protected abstract List getModulepathElements();
protected abstract List getCompileSourceRoots();
-
+
protected abstract void preparePaths( Set sourceFiles );
protected abstract File getOutputDirectory();
@@ -608,7 +608,7 @@ public void execute()
compilerConfiguration.setSourceVersion( getSource() );
compilerConfiguration.setTargetVersion( getTarget() );
-
+
compilerConfiguration.setReleaseVersion( getRelease() );
compilerConfiguration.setProc( proc );
@@ -689,6 +689,8 @@ public void execute()
}
}
+ compilerConfiguration.setFailOnWarning( failOnWarning );
+
compilerConfiguration.setExecutable( executable );
compilerConfiguration.setWorkingDirectory( basedir );
@@ -747,7 +749,7 @@ else if ( CompilerConfiguration.CompilerReuseStrategy.ReuseSame.getStrategy().eq
canUpdateTarget = compiler.canUpdateTarget( compilerConfiguration );
sources = getCompileSources( compiler, compilerConfiguration );
-
+
preparePaths( sources );
incrementalBuildHelperRequest = new IncrementalBuildHelperRequest().inputFiles( sources );
@@ -802,7 +804,7 @@ else if ( CompilerConfiguration.CompilerReuseStrategy.ReuseSame.getStrategy().eq
{
compilerConfiguration.setSourceFiles( staleSources );
}
-
+
preparePaths( compilerConfiguration.getSourceFiles() );
}
catch ( CompilerException e )
@@ -817,12 +819,12 @@ else if ( CompilerConfiguration.CompilerReuseStrategy.ReuseSame.getStrategy().eq
return;
}
}
-
+
// Dividing pathElements of classPath and modulePath is based on sourceFiles
compilerConfiguration.setClasspathEntries( getClasspathElements() );
compilerConfiguration.setModulepathEntries( getModulepathElements() );
-
+
Map effectiveCompilerArguments = getCompilerArguments();
String effectiveCompilerArgument = getCompilerArgument();
@@ -998,7 +1000,8 @@ else if ( message.getKind() == CompilerMessage.Kind.WARNING
}
}
- if ( failOnError && !compilerResult.isSuccess() )
+ if ( ( failOnError && !compilerResult.isSuccess() )
+ || ( failOnWarning && !warnings.isEmpty() ) )
{
for ( CompilerMessage message : others )
{
@@ -1230,7 +1233,7 @@ else if ( ( isDigits( setting.substring( 0, setting.length() - 1 ) ) )
private Toolchain getToolchain()
{
Toolchain tc = null;
-
+
if ( jdkToolchain != null )
{
// Maven 3.3.1 has plugin execution scoped Toolchain Support
@@ -1271,12 +1274,12 @@ private Toolchain getToolchain()
// ignore
}
}
-
+
if ( tc == null )
{
tc = toolchainManager.getToolchainFromBuildContext( "jdk", session );
}
-
+
return tc;
}
@@ -1408,7 +1411,7 @@ protected boolean isDependencyChanged()
List pathElements = new ArrayList();
pathElements.addAll( getClasspathElements() );
pathElements.addAll( getModulepathElements() );
-
+
for ( String pathElement : pathElements )
{
// ProjectArtifacts are artifacts which are available in the local project
diff --git a/maven-compiler-plugin/src/test/java/org/apache/maven/plugin/compiler/CompilerMojoTestCase.java b/maven-compiler-plugin/src/test/java/org/apache/maven/plugin/compiler/CompilerMojoTestCase.java
index 70221550bf..38c33dab3a 100644
--- a/maven-compiler-plugin/src/test/java/org/apache/maven/plugin/compiler/CompilerMojoTestCase.java
+++ b/maven-compiler-plugin/src/test/java/org/apache/maven/plugin/compiler/CompilerMojoTestCase.java
@@ -293,6 +293,25 @@ public void testCompileFailure()
}
}
+ public void testCompileFailOnWarning()
+ throws Exception
+ {
+ CompilerMojo compileMojo = getCompilerMojo( "target/test-classes/unit/compiler-failonwarning-test/plugin-config.xml" );
+
+ setVariableValueToObject( compileMojo, "compilerManager", new CompilerManagerStub( false, true ) );
+
+ try
+ {
+ compileMojo.execute();
+
+ fail( "Should throw an exception" );
+ }
+ catch ( CompilationFailureException e )
+ {
+ //expected
+ }
+ }
+
public void testCompileFailOnError()
throws Exception
{
diff --git a/maven-compiler-plugin/src/test/java/org/apache/maven/plugin/compiler/stubs/CompilerManagerStub.java b/maven-compiler-plugin/src/test/java/org/apache/maven/plugin/compiler/stubs/CompilerManagerStub.java
index efa5ee9fb8..e2614b6541 100644
--- a/maven-compiler-plugin/src/test/java/org/apache/maven/plugin/compiler/stubs/CompilerManagerStub.java
+++ b/maven-compiler-plugin/src/test/java/org/apache/maven/plugin/compiler/stubs/CompilerManagerStub.java
@@ -30,19 +30,27 @@ public class CompilerManagerStub
{
private boolean shouldFail;
+ private boolean shouldWarn;
+
public CompilerManagerStub()
{
- this( false );
+ this( false, false );
}
public CompilerManagerStub( boolean shouldFail )
+ {
+ this (shouldFail, false );
+ }
+
+ public CompilerManagerStub ( boolean shouldFail, boolean shouldWarn )
{
this.shouldFail = shouldFail;
+ this.shouldWarn = shouldWarn;
}
public org.codehaus.plexus.compiler.Compiler getCompiler( String compilerId )
throws NoSuchCompilerException
{
- return new CompilerStub( shouldFail );
+ return new CompilerStub( shouldFail, shouldWarn );
}
}
diff --git a/maven-compiler-plugin/src/test/java/org/apache/maven/plugin/compiler/stubs/CompilerStub.java b/maven-compiler-plugin/src/test/java/org/apache/maven/plugin/compiler/stubs/CompilerStub.java
index 94710c6c7f..aba419082e 100644
--- a/maven-compiler-plugin/src/test/java/org/apache/maven/plugin/compiler/stubs/CompilerStub.java
+++ b/maven-compiler-plugin/src/test/java/org/apache/maven/plugin/compiler/stubs/CompilerStub.java
@@ -39,14 +39,17 @@ public class CompilerStub
{
private boolean shouldFail;
+ private boolean shouldWarn;
+
public CompilerStub()
{
- this( false );
+ this( false, false );
}
- public CompilerStub( boolean shouldFail )
+ public CompilerStub( boolean shouldFail, boolean shouldWarn )
{
this.shouldFail = shouldFail;
+ this.shouldWarn = shouldWarn;
}
public CompilerOutputStyle getCompilerOutputStyle()
@@ -120,9 +123,23 @@ public CompilerResult performCompile( CompilerConfiguration compilerConfiguratio
{
throw new CompilerException( "An exception occurred while creating output file", e );
}
-
- return new CompilerResult( !shouldFail,
- Collections.singletonList( new CompilerMessage( "message 1", CompilerMessage.Kind.OTHER ) ) );
+
+ List compilerMessages;
+
+ if ( shouldFail )
+ {
+ compilerMessages = Collections.singletonList(new CompilerMessage( "message 1", CompilerMessage.Kind.ERROR ));
+ }
+ else if ( shouldWarn )
+ {
+ compilerMessages = Collections.singletonList(new CompilerMessage( "message 1", CompilerMessage.Kind.WARNING ));
+ }
+ else
+ {
+ compilerMessages = Collections.singletonList(new CompilerMessage( "message 1", CompilerMessage.Kind.OTHER ));
+ }
+
+ return new CompilerResult( !shouldFail, compilerMessages );
}
public String[] createCommandLine( CompilerConfiguration compilerConfiguration )
diff --git a/maven-compiler-plugin/src/test/resources/unit/compiler-failonwarning-test/plugin-config.xml b/maven-compiler-plugin/src/test/resources/unit/compiler-failonwarning-test/plugin-config.xml
new file mode 100644
index 0000000000..3b19cecc51
--- /dev/null
+++ b/maven-compiler-plugin/src/test/resources/unit/compiler-failonwarning-test/plugin-config.xml
@@ -0,0 +1,38 @@
+
+
+
+
+
+
+ maven-compiler-plugin
+
+ true
+
+ ${basedir}/target/test-classes/unit/compiler-basic-test/src/main/java
+
+ javac
+ true
+ ${basedir}/target/test/unit/compiler-basic-test/target/classes
+ ${basedir}/target/test/unit/compiler-basic-test/target
+
+
+
+
+
diff --git a/maven-compiler-plugin/src/test/resources/unit/compiler-failonwarning-test/src/main/java/TestCompile0.java b/maven-compiler-plugin/src/test/resources/unit/compiler-failonwarning-test/src/main/java/TestCompile0.java
new file mode 100644
index 0000000000..ad427c4c54
--- /dev/null
+++ b/maven-compiler-plugin/src/test/resources/unit/compiler-failonwarning-test/src/main/java/TestCompile0.java
@@ -0,0 +1,32 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class TestCompile0
+{
+
+ public TestCompile0()
+ {
+ List xxx = new ArrayList();
+ System.out.println( "Woo Hoo!" );
+ }
+
+}
diff --git a/maven-compiler-plugin/src/test/resources/unit/compiler-failonwarning-test/src/test/java/TestCompile0Test.java b/maven-compiler-plugin/src/test/resources/unit/compiler-failonwarning-test/src/test/java/TestCompile0Test.java
new file mode 100644
index 0000000000..5df6d4f5fe
--- /dev/null
+++ b/maven-compiler-plugin/src/test/resources/unit/compiler-failonwarning-test/src/test/java/TestCompile0Test.java
@@ -0,0 +1,29 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import junit.framework.TestCase;
+
+public class TestCompile0Test
+ extends TestCase
+{
+ public void testCompile0Test()
+ {
+ TestCompile0 test = new TestCompile0();
+ }
+}
\ No newline at end of file