From eef07569ef010a939bd7796f16f4ad73b5ba1c19 Mon Sep 17 00:00:00 2001 From: Henning Schmiedehausen Date: Tue, 15 Sep 2015 12:58:56 -0700 Subject: [PATCH] Add new option "failOnWarning". This option causes the maven-compiler-plugin to treat warnings as errors and fail accordingly. Simply adds "-Werror" to the compiler command line. It may be nice to add this to the plexus-compiler-api proper but as the sonatype repo only has tags to 2.4 and the current plugin references 2.6 (and I have no idea where that comes from), I went the easy route. Happy to refactor it if wanted. --- .../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 | 27 ++++++++ .../plugin/compiler/AbstractCompilerMojo.java | 18 ++++- .../plugin/compiler/CompilerMojoTestCase.java | 21 +++++- .../compiler/stubs/CompilerManagerStub.java | 11 +++- .../plugin/compiler/stubs/CompilerStub.java | 18 ++++- .../plugin-config.xml | 38 +++++++++++ .../src/main/java/TestCompile0.java | 32 +++++++++ .../src/test/java/TestCompile0Test.java | 33 ++++++++++ 11 files changed, 311 insertions(+), 8 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..86f9130c00 --- /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..695e93d1a7 --- /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..c04959f6c1 --- /dev/null +++ b/maven-compiler-plugin/src/it/fail-on-warning/verify.groovy @@ -0,0 +1,27 @@ + +/* + * 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 614a7de6b7..3bea06da58 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 @@ -86,6 +86,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.4 + */ + @Parameter( property = "maven.compiler.failOnWarning", defaultValue = "false" ) + private boolean failOnWarning = false; + /** * Set to true to include debugging information in the compiled class files. */ @@ -235,7 +243,7 @@ public abstract class AbstractCompilerMojo * * * @since 2.0.1 - * @deprecated use {@link #compilerArgs} instead. + * @deprecated use {@link #compilerArgs} instead. */ @Parameter @Deprecated @@ -257,7 +265,7 @@ public abstract class AbstractCompilerMojo */ @Parameter protected List compilerArgs; - + /** *

* Sets the unformatted single argument string to be passed to the compiler if {@link #fork} is set to @@ -550,7 +558,7 @@ public void execute() String effectiveCompilerArgument = getCompilerArgument(); if ( ( effectiveCompilerArguments != null ) || ( effectiveCompilerArgument != null ) - || ( compilerArgs != null ) ) + || ( compilerArgs != null ) || failOnWarning ) { LinkedHashMap cplrArgsCopy = new LinkedHashMap(); if ( effectiveCompilerArguments != null ) @@ -585,6 +593,10 @@ public void execute() cplrArgsCopy.put( arg, null ); } } + if ( failOnWarning ) + { + cplrArgsCopy.put( "-Werror", null ); + } compilerConfiguration.setCustomCompilerArguments( cplrArgsCopy ); } 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 1b1e46948a..71198828b9 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 @@ -273,6 +273,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( true, true ) ); + + try + { + compileMojo.execute(); + + fail( "Should throw an exception" ); + } + catch ( CompilationFailureException e ) + { + //expected + } + } + public void testCompileFailOnError() throws Exception { @@ -338,7 +357,7 @@ private TestCompilerMojo getTestCompilerMojo( CompilerMojo compilerMojo, String junitURI = junitURI.substring( "jar:".length(), junitURI.indexOf( '!' ) ); testClasspathList.add( new File( URI.create( junitURI ) ).getAbsolutePath() ); } - + testClasspathList.add( compilerMojo.getOutputDirectory().getPath() ); setVariableValueToObject( mojo, "classpathElements", testClasspathList ); 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..b95ea7728b 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 @@ -29,20 +29,27 @@ public class CompilerManagerStub implements CompilerManager { 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..6e45b2f113 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 @@ -38,15 +38,22 @@ public class CompilerStub implements org.codehaus.plexus.compiler.Compiler { private boolean shouldFail; + private boolean shouldWarn; public CompilerStub() { - this( false ); + this( false, false ); } public CompilerStub( boolean shouldFail ) + { + this( shouldFail, false ); + } + + public CompilerStub( boolean shouldFail, boolean shouldWarn ) { this.shouldFail = shouldFail; + this.shouldWarn = shouldWarn; } public CompilerOutputStyle getCompilerOutputStyle() @@ -120,7 +127,7 @@ 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 ) ) ); } @@ -128,6 +135,13 @@ public CompilerResult performCompile( CompilerConfiguration compilerConfiguratio public String[] createCommandLine( CompilerConfiguration compilerConfiguration ) throws CompilerException { + if ( shouldWarn ) + { + if ( ! compilerConfiguration.getCustomCompilerArguments().containsKey("-Werror") ) + { + throw new CompilerException( "Compiler should error on warnings but no Werror flag found" ); + } + } return new String[0]; } } 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..267a994395 --- /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-failonwarning-test/src/main/java + + javac + true + ${basedir}/target/test/unit/compiler-failonwarning-test/target/classes + ${basedir}/target/test/unit/compiler-failonwarning-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..54f26aa28e --- /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!" ); + } + +} \ No newline at end of file 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..6eca7eac0b --- /dev/null +++ b/maven-compiler-plugin/src/test/resources/unit/compiler-failonwarning-test/src/test/java/TestCompile0Test.java @@ -0,0 +1,33 @@ +/* + * 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; + +import junit.framework.TestCase; + +public class TestCompile0Test + extends TestCase +{ + public void testCompile0Test() + { + List foo = new ArrayList(); + TestCompile0 test = new TestCompile0(); + } +} \ No newline at end of file