Skip to content

Commit 6363af4

Browse files
authored
Fix #5: check test classes (#80)
* Bump enforcer-plugin version to support mvn -f or -pl * Fix #5: Check test classes * Fix #5: align phase and dep resolution to test class check
1 parent 71db56b commit 6363af4

File tree

8 files changed

+238
-26
lines changed

8 files changed

+238
-26
lines changed

animal-sniffer-enforcer-rule/src/main/java/org/codehaus/mojo/animal_sniffer/enforcer/CheckSignatureRule.java

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,23 @@ public class CheckSignatureRule
130130
*/
131131
private String[] excludeDependencies = null;
132132

133+
/**
134+
* Should test classes be checked.
135+
*
136+
* @parameter default-value="true"
137+
* @since 1.19
138+
*/
139+
private boolean checkTestClasses = true;
140+
133141
public void execute( EnforcerRuleHelper helper )
134142
throws EnforcerRuleException
135143
{
136144
try
137145
{
138146
File outputDirectory = new File( (String) helper.evaluate( "${project.build.outputDirectory}" ) );
139147

148+
File testOutputDirectory = new File( (String) helper.evaluate( "${project.build.testOutputDirectory}" ) );
149+
140150
ArtifactResolver resolver = (ArtifactResolver) helper.getComponent( ArtifactResolver.class );
141151

142152
MavenProject project = (MavenProject) helper.evaluate( "${project}" );
@@ -194,19 +204,22 @@ public void execute( EnforcerRuleHelper helper )
194204
new SignatureChecker( new FileInputStream( a.getFile() ), ignoredPackages, logger );
195205
signatureChecker.setCheckJars( false ); // don't want to descend into jar files that have been copied to
196206
// the output directory as resources.
197-
List<File> sourcePaths = new ArrayList<File>();
198-
Iterator iterator = project.getCompileSourceRoots().iterator();
199-
while ( iterator.hasNext() )
200-
{
201-
String path = (String) iterator.next();
202-
sourcePaths.add( new File( path ) );
203-
}
204-
signatureChecker.setSourcePath( sourcePaths );
207+
208+
signatureChecker.setSourcePath( buildSourcePathList( project ) );
209+
205210
if ( annotations != null )
206211
{
207212
signatureChecker.setAnnotationTypes( Arrays.asList( annotations ) );
208213
}
209-
signatureChecker.process( outputDirectory );
214+
215+
if ( checkTestClasses )
216+
{
217+
signatureChecker.process( new File[] { outputDirectory, testOutputDirectory } );
218+
}
219+
else
220+
{
221+
signatureChecker.process( outputDirectory );
222+
}
210223

211224
if ( signatureChecker.isSignatureBroken() )
212225
{
@@ -365,4 +378,20 @@ private static String artifactId( Artifact artifact )
365378
artifact.getClassifier() != null ? ":" + artifact.getClassifier() : "" ) + ":" + artifact.getBaseVersion();
366379

367380
}
381+
382+
@SuppressWarnings("unchecked")
383+
private List<File> buildSourcePathList( MavenProject project )
384+
{
385+
List<String> compileSourceRoots = new ArrayList<>( project.getCompileSourceRoots() );
386+
if ( checkTestClasses )
387+
{
388+
compileSourceRoots.addAll( project.getTestCompileSourceRoots() );
389+
}
390+
List<File> sourcePathList = new ArrayList<>( compileSourceRoots.size() );
391+
for ( String compileSourceRoot : compileSourceRoots)
392+
{
393+
sourcePathList.add( new File( compileSourceRoot ) );
394+
}
395+
return sourcePathList;
396+
}
368397
}

animal-sniffer-enforcer-rule/src/site/apt/examples/checking-signatures.apt.vm

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ Checking a project against API signatures
6161
....
6262
<execution>
6363
<id>check-signatures</id>
64-
<phase>test</phase>
64+
<phase>process-test-classes</phase>
6565
<goals>
6666
<goal>enforce</goal>
6767
</goals>
@@ -158,7 +158,7 @@ public final class MapFactory {
158158
....
159159
<execution>
160160
<id>check-signatures</id>
161-
<phase>test</phase>
161+
<phase>process-test-classes</phase>
162162
<goals>
163163
<goal>enforce</goal>
164164
</goals>
@@ -217,7 +217,7 @@ public final class MapFactory {
217217
....
218218
<execution>
219219
<id>check-signatures</id>
220-
<phase>test</phase>
220+
<phase>process-test-classes</phase>
221221
<goals>
222222
<goal>enforce</goal>
223223
</goals>
@@ -359,7 +359,7 @@ public final class Someclass {
359359
....
360360
<execution>
361361
<id>check-signatures</id>
362-
<phase>test</phase>
362+
<phase>process-test-classes</phase>
363363
<goals>
364364
<goal>enforce</goal>
365365
</goals>

animal-sniffer-enforcer-rule/src/site/apt/usage.apt.vm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ Usage
6565
....
6666
<execution>
6767
<id>check-signatures</id>
68-
<phase>test</phase>
68+
<phase>process-test-classes</phase>
6969
<goals>
7070
<goal>enforce</goal>
7171
</goals>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#
2+
# The MIT License
3+
#
4+
# Copyright (c) 2009 codehaus.org.
5+
#
6+
# Permission is hereby granted, free of charge, to any person obtaining a copy
7+
# of this software and associated documentation files (the "Software"), to deal
8+
# in the Software without restriction, including without limitation the rights
9+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
# copies of the Software, and to permit persons to whom the Software is
11+
# furnished to do so, subject to the following conditions:
12+
#
13+
# The above copyright notice and this permission notice shall be included in
14+
# all copies or substantial portions of the Software.
15+
#
16+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
# THE SOFTWARE.
23+
#
24+
invoker.goals.1=verify -Danimal.sniffer.checkTestClasses=false
25+
26+
invoker.goals.2=verify
27+
invoker.buildResult.2=failure
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
The MIT License
4+
5+
Copyright (c) 2009 codehaus.org.
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
-->
25+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
26+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
27+
<modelVersion>4.0.0</modelVersion>
28+
29+
<groupId>localdomain.localhost</groupId>
30+
<artifactId>test-classes-test</artifactId>
31+
<version>1.0-SNAPSHOT</version>
32+
<packaging>jar</packaging>
33+
34+
<name>Real Test</name>
35+
36+
<description>
37+
Tests that verifies that test classes are checked.
38+
</description>
39+
40+
<build>
41+
<pluginManagement>
42+
<plugins>
43+
<plugin>
44+
<artifactId>maven-compiler-plugin</artifactId>
45+
<configuration>
46+
<source>@mojo.java.target@</source>
47+
<target>@mojo.java.target@</target>
48+
</configuration>
49+
</plugin>
50+
</plugins>
51+
</pluginManagement>
52+
<plugins>
53+
<plugin>
54+
<groupId>${pluginGroupId}</groupId>
55+
<artifactId>${pluginArtifactId}</artifactId>
56+
<version>${pluginVersion}</version>
57+
<executions>
58+
<execution>
59+
<phase>test</phase>
60+
<goals>
61+
<goal>check</goal>
62+
</goals>
63+
<configuration>
64+
<signature>
65+
<groupId>org.codehaus.mojo.signature</groupId>
66+
<artifactId>java14</artifactId>
67+
<version>1.0</version>
68+
</signature>
69+
</configuration>
70+
</execution>
71+
</executions>
72+
</plugin>
73+
</plugins>
74+
</build>
75+
76+
<properties>
77+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
78+
<project.build.outputEncoding>UTF-8</project.build.outputEncoding>
79+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
80+
<pluginGroupId>@project.groupId@</pluginGroupId>
81+
<pluginArtifactId>@project.artifactId@</pluginArtifactId>
82+
<pluginVersion>@project.version@</pluginVersion>
83+
</properties>
84+
85+
</project>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package localhost;
2+
3+
/*
4+
* The MIT License
5+
*
6+
* Copyright (c) 2009, codehaus.org
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*
26+
*/
27+
28+
public class Main
29+
{
30+
public static void main( String[] args )
31+
{
32+
if ( new java.util.concurrent.ConcurrentHashMap().isEmpty() )
33+
{
34+
System.out.println( "All is good" );
35+
}
36+
}
37+
}

animal-sniffer-maven-plugin/src/main/java/org/codehaus/mojo/animal_sniffer/maven/CheckSignatureMojo.java

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
*
6262
* @author Kohsuke Kawaguchi
6363
*/
64-
@Mojo( name = "check", defaultPhase = LifecyclePhase.PROCESS_CLASSES, requiresDependencyResolution = ResolutionScope.COMPILE, threadSafe = true )
64+
@Mojo( name = "check", defaultPhase = LifecyclePhase.PROCESS_TEST_CLASSES, requiresDependencyResolution = ResolutionScope.TEST, threadSafe = true )
6565
public class CheckSignatureMojo
6666
extends AbstractMojo
6767
{
@@ -72,10 +72,26 @@ public class CheckSignatureMojo
7272
@Parameter( defaultValue = "${project.build.outputDirectory}", required = true, readonly = true )
7373
protected File outputDirectory;
7474

75+
/**
76+
* The directory for compiled test classes.
77+
*
78+
* @since 1.19
79+
*/
80+
@Parameter( defaultValue = "${project.build.testOutputDirectory}", required = true, readonly = true )
81+
protected File testOutputDirectory;
82+
83+
/**
84+
* Should test classes be checked.
85+
*
86+
* @since 1.19
87+
*/
88+
@Parameter( property = "animal.sniffer.checkTestClasses", defaultValue = "true" )
89+
protected boolean checkTestClasses;
90+
7591
/**
7692
* Signature module to use.
7793
*/
78-
@Parameter( required = true, property="animal.sniffer.signature" )
94+
@Parameter( required = true, property = "animal.sniffer.signature" )
7995
protected Signature signature;
8096

8197
/**
@@ -187,6 +203,7 @@ public void setSignature( String signatureId ) {
187203
@Component
188204
protected ArtifactFactory artifactFactory;
189205

206+
@Override
190207
public void execute()
191208
throws MojoExecutionException, MojoFailureException
192209
{
@@ -250,21 +267,22 @@ public void execute()
250267
new MavenLogger( getLog() ) );
251268
signatureChecker.setCheckJars( false ); // don't want to decend into jar files that have been copied to
252269
// the output directory as resources.
253-
List<File> sourcePaths = new ArrayList<>();
254-
Iterator<String> iterator = project.getCompileSourceRoots().iterator();
255-
while ( iterator.hasNext() )
256-
{
257-
String path = iterator.next();
258-
sourcePaths.add( new File( path ) );
259-
}
260-
signatureChecker.setSourcePath( sourcePaths );
270+
271+
signatureChecker.setSourcePath( buildSourcePathList() );
261272

262273
if ( annotations != null )
263274
{
264275
signatureChecker.setAnnotationTypes( Arrays.asList( annotations ) );
265276
}
266277

267-
signatureChecker.process( outputDirectory );
278+
if ( checkTestClasses )
279+
{
280+
signatureChecker.process( new File[] { outputDirectory, testOutputDirectory } );
281+
}
282+
else
283+
{
284+
signatureChecker.process( outputDirectory );
285+
}
268286

269287
if ( signatureChecker.isSignatureBroken() )
270288
{
@@ -394,4 +412,20 @@ private void apply( ClassFileVisitor v )
394412
}
395413
}
396414
}
415+
416+
@SuppressWarnings("unchecked")
417+
private List<File> buildSourcePathList( )
418+
{
419+
List<String> compileSourceRoots = new ArrayList<>( project.getCompileSourceRoots() );
420+
if ( checkTestClasses )
421+
{
422+
compileSourceRoots.addAll( project.getTestCompileSourceRoots() );
423+
}
424+
List<File> sourcePathList = new ArrayList<>( compileSourceRoots.size() );
425+
for ( String compileSourceRoot : compileSourceRoots)
426+
{
427+
sourcePathList.add( new File( compileSourceRoot ) );
428+
}
429+
return sourcePathList;
430+
}
397431
}

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@
171171
<plugin>
172172
<groupId>org.apache.maven.plugins</groupId>
173173
<artifactId>maven-enforcer-plugin</artifactId>
174-
<version>3.0.0-M2</version>
174+
<version>3.0.0-M3</version>
175175
</plugin>
176176
<plugin>
177177
<groupId>org.apache.maven.plugins</groupId>

0 commit comments

Comments
 (0)