diff --git a/animal-sniffer-enforcer-rule/src/main/java/org/codehaus/mojo/animal_sniffer/enforcer/CheckSignatureRule.java b/animal-sniffer-enforcer-rule/src/main/java/org/codehaus/mojo/animal_sniffer/enforcer/CheckSignatureRule.java index 66705a3f..07b93fd5 100644 --- a/animal-sniffer-enforcer-rule/src/main/java/org/codehaus/mojo/animal_sniffer/enforcer/CheckSignatureRule.java +++ b/animal-sniffer-enforcer-rule/src/main/java/org/codehaus/mojo/animal_sniffer/enforcer/CheckSignatureRule.java @@ -130,6 +130,14 @@ public class CheckSignatureRule */ private String[] excludeDependencies = null; + /** + * Should test classes be checked. + * + * @parameter default-value="true" + * @since 1.19 + */ + private boolean checkTestClasses = true; + public void execute( EnforcerRuleHelper helper ) throws EnforcerRuleException { @@ -137,6 +145,8 @@ public void execute( EnforcerRuleHelper helper ) { File outputDirectory = new File( (String) helper.evaluate( "${project.build.outputDirectory}" ) ); + File testOutputDirectory = new File( (String) helper.evaluate( "${project.build.testOutputDirectory}" ) ); + ArtifactResolver resolver = (ArtifactResolver) helper.getComponent( ArtifactResolver.class ); MavenProject project = (MavenProject) helper.evaluate( "${project}" ); @@ -194,19 +204,22 @@ public void execute( EnforcerRuleHelper helper ) new SignatureChecker( new FileInputStream( a.getFile() ), ignoredPackages, logger ); signatureChecker.setCheckJars( false ); // don't want to descend into jar files that have been copied to // the output directory as resources. - List sourcePaths = new ArrayList(); - Iterator iterator = project.getCompileSourceRoots().iterator(); - while ( iterator.hasNext() ) - { - String path = (String) iterator.next(); - sourcePaths.add( new File( path ) ); - } - signatureChecker.setSourcePath( sourcePaths ); + + signatureChecker.setSourcePath( buildSourcePathList( project ) ); + if ( annotations != null ) { signatureChecker.setAnnotationTypes( Arrays.asList( annotations ) ); } - signatureChecker.process( outputDirectory ); + + if ( checkTestClasses ) + { + signatureChecker.process( new File[] { outputDirectory, testOutputDirectory } ); + } + else + { + signatureChecker.process( outputDirectory ); + } if ( signatureChecker.isSignatureBroken() ) { @@ -365,4 +378,20 @@ private static String artifactId( Artifact artifact ) artifact.getClassifier() != null ? ":" + artifact.getClassifier() : "" ) + ":" + artifact.getBaseVersion(); } + + @SuppressWarnings("unchecked") + private List buildSourcePathList( MavenProject project ) + { + List compileSourceRoots = new ArrayList<>( project.getCompileSourceRoots() ); + if ( checkTestClasses ) + { + compileSourceRoots.addAll( project.getTestCompileSourceRoots() ); + } + List sourcePathList = new ArrayList<>( compileSourceRoots.size() ); + for ( String compileSourceRoot : compileSourceRoots) + { + sourcePathList.add( new File( compileSourceRoot ) ); + } + return sourcePathList; + } } diff --git a/animal-sniffer-maven-plugin/src/it/github-5-test-classes/invoker.properties b/animal-sniffer-maven-plugin/src/it/github-5-test-classes/invoker.properties new file mode 100644 index 00000000..1c2b2875 --- /dev/null +++ b/animal-sniffer-maven-plugin/src/it/github-5-test-classes/invoker.properties @@ -0,0 +1,27 @@ +# +# The MIT License +# +# Copyright (c) 2009 codehaus.org. +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. +# +invoker.goals.1=verify -Danimal.sniffer.checkTestClasses=false + +invoker.goals.2=verify +invoker.buildResult.2=failure \ No newline at end of file diff --git a/animal-sniffer-maven-plugin/src/it/github-5-test-classes/pom.xml b/animal-sniffer-maven-plugin/src/it/github-5-test-classes/pom.xml new file mode 100644 index 00000000..d101471f --- /dev/null +++ b/animal-sniffer-maven-plugin/src/it/github-5-test-classes/pom.xml @@ -0,0 +1,85 @@ + + + + 4.0.0 + + localdomain.localhost + test-classes-test + 1.0-SNAPSHOT + jar + + Real Test + + + Tests that verifies that test classes are checked. + + + + + + + maven-compiler-plugin + + @mojo.java.target@ + @mojo.java.target@ + + + + + + + ${pluginGroupId} + ${pluginArtifactId} + ${pluginVersion} + + + test + + check + + + + org.codehaus.mojo.signature + java14 + 1.0 + + + + + + + + + + UTF-8 + UTF-8 + UTF-8 + @project.groupId@ + @project.artifactId@ + @project.version@ + + + diff --git a/animal-sniffer-maven-plugin/src/it/github-5-test-classes/src/test/java/localhost/Main.java b/animal-sniffer-maven-plugin/src/it/github-5-test-classes/src/test/java/localhost/Main.java new file mode 100644 index 00000000..9a40d6c5 --- /dev/null +++ b/animal-sniffer-maven-plugin/src/it/github-5-test-classes/src/test/java/localhost/Main.java @@ -0,0 +1,37 @@ +package localhost; + +/* + * The MIT License + * + * Copyright (c) 2009, codehaus.org + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ + +public class Main +{ + public static void main( String[] args ) + { + if ( new java.util.concurrent.ConcurrentHashMap().isEmpty() ) + { + System.out.println( "All is good" ); + } + } +} diff --git a/animal-sniffer-maven-plugin/src/main/java/org/codehaus/mojo/animal_sniffer/maven/CheckSignatureMojo.java b/animal-sniffer-maven-plugin/src/main/java/org/codehaus/mojo/animal_sniffer/maven/CheckSignatureMojo.java index 3c754225..3aa07b1f 100644 --- a/animal-sniffer-maven-plugin/src/main/java/org/codehaus/mojo/animal_sniffer/maven/CheckSignatureMojo.java +++ b/animal-sniffer-maven-plugin/src/main/java/org/codehaus/mojo/animal_sniffer/maven/CheckSignatureMojo.java @@ -72,10 +72,26 @@ public class CheckSignatureMojo @Parameter( defaultValue = "${project.build.outputDirectory}", required = true, readonly = true ) protected File outputDirectory; + /** + * The directory for compiled test classes. + * + * @since 1.19 + */ + @Parameter( defaultValue = "${project.build.testOutputDirectory}", required = true, readonly = true ) + protected File testOutputDirectory; + + /** + * Should test classes be checked. + * + * @since 1.19 + */ + @Parameter( property = "animal.sniffer.checkTestClasses", defaultValue = "true" ) + protected boolean checkTestClasses; + /** * Signature module to use. */ - @Parameter( required = true, property="animal.sniffer.signature" ) + @Parameter( required = true, property = "animal.sniffer.signature" ) protected Signature signature; /** @@ -187,6 +203,7 @@ public void setSignature( String signatureId ) { @Component protected ArtifactFactory artifactFactory; + @Override public void execute() throws MojoExecutionException, MojoFailureException { @@ -250,21 +267,22 @@ public void execute() new MavenLogger( getLog() ) ); signatureChecker.setCheckJars( false ); // don't want to decend into jar files that have been copied to // the output directory as resources. - List sourcePaths = new ArrayList<>(); - Iterator iterator = project.getCompileSourceRoots().iterator(); - while ( iterator.hasNext() ) - { - String path = iterator.next(); - sourcePaths.add( new File( path ) ); - } - signatureChecker.setSourcePath( sourcePaths ); + + signatureChecker.setSourcePath( buildSourcePathList() ); if ( annotations != null ) { signatureChecker.setAnnotationTypes( Arrays.asList( annotations ) ); } - signatureChecker.process( outputDirectory ); + if ( checkTestClasses ) + { + signatureChecker.process( new File[] { outputDirectory, testOutputDirectory } ); + } + else + { + signatureChecker.process( outputDirectory ); + } if ( signatureChecker.isSignatureBroken() ) { @@ -394,4 +412,20 @@ private void apply( ClassFileVisitor v ) } } } + + @SuppressWarnings("unchecked") + private List buildSourcePathList( ) + { + List compileSourceRoots = new ArrayList<>( project.getCompileSourceRoots() ); + if ( checkTestClasses ) + { + compileSourceRoots.addAll( project.getTestCompileSourceRoots() ); + } + List sourcePathList = new ArrayList<>( compileSourceRoots.size() ); + for ( String compileSourceRoot : compileSourceRoots) + { + sourcePathList.add( new File( compileSourceRoot ) ); + } + return sourcePathList; + } }