Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Add new option "failOnWarning". #60

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions maven-compiler-plugin/src/it/fail-on-warning/invoker.properties
Original file line number Diff line number Diff line change
@@ -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+
66 changes: 66 additions & 0 deletions maven-compiler-plugin/src/it/fail-on-warning/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?xml version='1.0'?>
<!--
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.
-->

<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.maven.plugins.compiler.it</groupId>
<artifactId>fail-on-warning</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Werror warnings build</name>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.4</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>@project.version@</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<verbose>true</verbose>
<debug>true</debug>
<optimize>true</optimize>
<showDeprecation>true</showDeprecation>
<showWarnings>true</showWarnings>
<failOnWarning>true</failOnWarning>
<compilerArgs>
<arg>-Xlint:all,-options,-path</arg>
</compilerArgs>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -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");
}
}
27 changes: 27 additions & 0 deletions maven-compiler-plugin/src/it/fail-on-warning/verify.groovy
Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
Expand Up @@ -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 <code>true</code> to include debugging information in the compiled class files.
*/
Expand Down Expand Up @@ -235,7 +243,7 @@ public abstract class AbstractCompilerMojo
* </pre>
*
* @since 2.0.1
* @deprecated use {@link #compilerArgs} instead.
* @deprecated use {@link #compilerArgs} instead.
*/
@Parameter
@Deprecated
Expand All @@ -257,7 +265,7 @@ public abstract class AbstractCompilerMojo
*/
@Parameter
protected List<String> compilerArgs;

/**
* <p>
* Sets the unformatted single argument string to be passed to the compiler if {@link #fork} is set to
Expand Down Expand Up @@ -550,7 +558,7 @@ public void execute()
String effectiveCompilerArgument = getCompilerArgument();

if ( ( effectiveCompilerArguments != null ) || ( effectiveCompilerArgument != null )
|| ( compilerArgs != null ) )
|| ( compilerArgs != null ) || failOnWarning )
{
LinkedHashMap<String, String> cplrArgsCopy = new LinkedHashMap<String, String>();
if ( effectiveCompilerArguments != null )
Expand Down Expand Up @@ -585,6 +593,10 @@ public void execute()
cplrArgsCopy.put( arg, null );
}
}
if ( failOnWarning )
{
cplrArgsCopy.put( "-Werror", null );
}
compilerConfiguration.setCustomCompilerArguments( cplrArgsCopy );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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 );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -120,14 +127,21 @@ 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 ) ) );
}

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];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!--
~ 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.
-->

<project>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<failOnWarning>true</failOnWarning>
<compileSourceRoots>
<compileSourceRoot>${basedir}/target/test-classes/unit/compiler-failonwarning-test/src/main/java</compileSourceRoot>
</compileSourceRoots>
<compilerId>javac</compilerId>
<debug>true</debug>
<outputDirectory>${basedir}/target/test/unit/compiler-failonwarning-test/target/classes</outputDirectory>
<buildDirectory>${basedir}/target/test/unit/compiler-failonwarning-test/target</buildDirectory>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -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!" );
}

}
Loading