Hi, I have two changes for the animal sniffer maven plugin. Apologies for mixing them, but while fixing one the other seemed obvious.
A. Create a "test-check" goal for checking test classes. I recently broke another project with a commit of a test class containing a JDK7 API call; the project should compile with JDK5. This was my motivation for looking at the animal sniffer plugin. B. Update the source directory for checking to include locations other than ${basedir}/src/*/java. When using other maven plugins, e.g. the build helper plugin, or generated code there may be additional source roots. I do not know the procedure for contributing, so I append output from "svn diff src" below. It would be helpful for me if these changes could appear in version 1.10; for now I'm commenting out "test-check" in my project pom. Please do not hesitate to contact me with any questions or concerns. I am happy to sign copyright documents, if required. Regards, --binkley Index: src/main/java/org/codehaus/mojo/animal_sniffer/maven/CheckSignatureMojo.java =================================================================== --- src/main/java/org/codehaus/mojo/animal_sniffer/maven/CheckSignatureMojo.java (revision 19274) +++ src/main/java/org/codehaus/mojo/animal_sniffer/maven/CheckSignatureMojo.java (working copy) @@ -25,25 +25,8 @@ * */ -import org.apache.maven.artifact.factory.ArtifactFactory; -import org.apache.maven.artifact.repository.ArtifactRepository; -import org.apache.maven.artifact.resolver.AbstractArtifactResolutionException; -import org.apache.maven.artifact.resolver.ArtifactResolver; -import org.apache.maven.plugin.AbstractMojo; -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.MojoFailureException; -import org.apache.maven.project.MavenProject; -import org.codehaus.mojo.animal_sniffer.ClassFileVisitor; -import org.codehaus.mojo.animal_sniffer.ClassListBuilder; -import org.codehaus.mojo.animal_sniffer.SignatureChecker; - import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.util.Collections; -import java.util.Iterator; import java.util.List; -import java.util.Set; /** * Checks the classes compiled by this module. @@ -54,9 +37,16 @@ * @goal check * @threadSafe */ -public class CheckSignatureMojo - extends AbstractMojo +public class CheckSignatureMojo extends AbstractCheckSignatureMojo { + /** + * The directories for source files (including added by other plugins). + * + * @parameter expression="${project.compileSourceRoots}" + * @required + * @readonly + */ + protected List sourceRoots; /** * Project classpath. @@ -76,143 +66,15 @@ */ protected File outputDirectory; - /** - * Signature module to use. - * - * @required - * @parameter - */ - protected Signature signature; - - /** - * Class names to ignore signatures for (wildcards accepted). - * - * @parameter - */ - protected String[] ignores; - - /** - * Should dependencies be ignored. - * - * @parameter default-value="true" - */ - protected boolean ignoreDependencies; - - /** - * Should signature checking be skipped? - * - * @parameter default-value="false" expression="${animal.sniffer.skip}" - */ - protected boolean skip; - - /** - * @component - * @readonly - */ - protected ArtifactResolver resolver; - - /** - * @parameter expression="${project}" - * @readonly - */ - protected MavenProject project; - - /** - * @parameter expression="${localRepository}" - * @readonly - */ - protected ArtifactRepository localRepository; - - /** - * @component - * @readonly - */ - protected ArtifactFactory artifactFactory; - - public void execute() - throws MojoExecutionException, MojoFailureException - { - if ( skip ) - { - getLog().info( "Signature checking is skipped." ); - return; - } - - try - { - getLog().info( "Checking unresolved references to " + signature ); - - org.apache.maven.artifact.Artifact a = signature.createArtifact( artifactFactory ); - - resolver.resolve( a, project.getRemoteArtifactRepositories(), localRepository ); - // just check code from this module - final Set ignoredPackages = buildPackageList(); - - if ( ignores != null ) - { - for ( int i = 0; i < ignores.length; i++ ) - { - String ignore = ignores[i]; - if ( ignore == null ) - { - continue; - } - ignoredPackages.add( ignore.replace( '.', '/' ) ); - } - } - - if ( getLog().isDebugEnabled() ) - { - getLog().debug( ignoredPackages.toString() ); - } - - final SignatureChecker signatureChecker = - new SignatureChecker( new FileInputStream( a.getFile() ), ignoredPackages, - new MavenLogger( getLog() ) ); - signatureChecker.setCheckJars( false ); // don't want to decend into jar files that have been copied to - // the output directory as resources. - signatureChecker.setSourcePath( Collections.singletonList( new File( project.getBuild().getSourceDirectory() ) ) ); - signatureChecker.process( outputDirectory ); - - if ( signatureChecker.isSignatureBroken() ) - { - throw new MojoFailureException( - "Signature errors found. Verify them and put @IgnoreJRERequirement on them." ); - } - } - catch ( IOException e ) - { - throw new MojoExecutionException( "Failed to check signatures", e ); - } - catch ( AbstractArtifactResolutionException e ) - { - throw new MojoExecutionException( "Failed to obtain signature: " + signature, e ); - } + public List getSourceRoots() { + return sourceRoots; } - /** - * List of packages defined in the application. - */ - private Set buildPackageList() - throws IOException - { - ClassListBuilder plb = new ClassListBuilder( new MavenLogger( getLog() ) ); - apply( plb ); - return plb.getPackages(); + protected List getClasspathElements() { + return classpathElements; } - private void apply( ClassFileVisitor v ) - throws IOException - { - v.process( outputDirectory ); - if ( ignoreDependencies ) - { - Iterator itr = classpathElements.iterator(); - while ( itr.hasNext() ) - { - String path = (String) itr.next(); - v.process( new File( path ) ); - } - } + protected File getOutputDirectory() { + return outputDirectory; } } Index: src/main/java/org/codehaus/mojo/animal_sniffer/maven/TestCheckSignatureMojo.java =================================================================== --- src/main/java/org/codehaus/mojo/animal_sniffer/maven/TestCheckSignatureMojo.java (revision 0) +++ src/main/java/org/codehaus/mojo/animal_sniffer/maven/TestCheckSignatureMojo.java (working copy) @@ -0,0 +1,107 @@ +/* + * This is free and unencumbered software released into the public domain. + * + * Anyone is free to copy, modify, publish, use, compile, sell, or + * distribute this software, either in source code form or as a compiled + * binary, for any purpose, commercial or non-commercial, and by any + * means. + * + * In jurisdictions that recognize copyright laws, the author or authors + * of this software dedicate any and all copyright interest in the + * software to the public domain. We make this dedication for the benefit + * of the public at large and to the detriment of our heirs and + * successors. We intend this dedication to be an overt act of + * relinquishment in perpetuity of all present and future rights to this + * software under copyright law. + * + * 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 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. + * + * For more information, please refer to <http://unlicense.org/>. + */ + +package org.codehaus.mojo.animal_sniffer.maven; + +/* + * The MIT License + * + * Copyright (c) 2008 Kohsuke Kawaguchi and 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. + * + */ + +import java.io.File; +import java.util.List; + +/** + * Checks the classes compiled by this module. + * + * @author Kohsuke Kawaguchi + * @phase process-test-classes + * @requiresDependencyResolution test + * @goal test-check + * @threadSafe + */ +public class TestCheckSignatureMojo extends AbstractCheckSignatureMojo +{ + /** + * The directories for source files (including added by other plugins). + * + * @parameter expression="${project.testSourceRoots}" + * @required + * @readonly + */ + protected List sourceRoots; + + /** + * Project classpath. + * + * @parameter expression="${project.testClasspathElements}" + * @required + * @readonly + */ + protected List classpathElements; + + /** + * The directory for compiled classes. + * + * @parameter expression="${project.build.testOutputDirectory}" + * @required + * @readonly + */ + protected File outputDirectory; + + public List getSourceRoots() { + return sourceRoots; + } + + protected List getClasspathElements() { + return classpathElements; + } + + protected File getOutputDirectory() { + return outputDirectory; + } +} Property changes on: src/main/java/org/codehaus/mojo/animal_sniffer/maven/TestCheckSignatureMojo.java ___________________________________________________________________ Added: svn:executable ## -0,0 +1 ## +* \ No newline at end of property ---------- Forwarded message ---------- From: Kohsuke Kawaguchi <k...@kohsuke.org> Date: Wed, Jan 15, 2014 at 2:21 AM Subject: Re: animal sniffer for maven test classes To: "B. K. Oxley (binkley)" <bink...@alumni.rice.edu> Thank you! Yes, we'd need to move most of the logic into an abstract base class, then create another mojo that extends from it that binds itself to the correct lifecycle phase and the classpath. This is a common pattern that can be seen in many Maven plugins, such as jar plugin and javadoc plugin. The project is maintained in the codehaus mojo project now, so you'd want to forward this e-mail there. What you are proposing sounds sensible to me, so I don't expect a problem integrating it. On 01/13/2014 02:27 PM, B. K. Oxley (binkley) wrote: > Howdy! > > Apologies if you are the wrong guy for this question; your name was on the > 1.9 > release of animal sniffer, and I trust your code. :) > > I've had difficulty getting the maven animal sniffer maven plugin to check > test > classes. I looked through the check mojo and could point the output > directory > at target/test-classes with configuration, but then couldn't change the > classpath elements to use test scope. Result is animal sniffer doesn't > know > what to do about test-only dependencies (e.g., junit & friends). > > I'm willing to contribute a patch to animal sniffer for a test-check mojo. > > Some questions: > > 1. Who's the right person to approach on this? I don't want to bug you if > you > don't work on animal sniffer any more. > 2. What's the right way to check test classes in a maven project? I'm > obviously > doing it the wrong way. > 3. If a new mojo is needed, or a change to the existing mojo, how do I > contribute it? (Answer to question #1 relates.) > > > Cheers, > --binkley > > -- Kohsuke Kawaguchi http://kohsuke.org/