Skip to content

Commit

Permalink
Support mojos using the new API
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Oct 12, 2022
1 parent 0343ad3 commit 4a4b1f0
Show file tree
Hide file tree
Showing 32 changed files with 262 additions and 150 deletions.
4 changes: 2 additions & 2 deletions maven-plugin-annotations/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
<modelVersion>4.0.0</modelVersion>

<parent>
<artifactId>maven-plugin-tools</artifactId>
<groupId>org.apache.maven.plugin-tools</groupId>
<version>3.6.5-SNAPSHOT</version>
<artifactId>maven-plugin-tools</artifactId>
<version>4.0.0-SNAPSHOT</version>
</parent>

<artifactId>maven-plugin-annotations</artifactId>
Expand Down
7 changes: 4 additions & 3 deletions maven-plugin-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
<modelVersion>4.0.0</modelVersion>

<parent>
<artifactId>maven-plugin-tools</artifactId>
<groupId>org.apache.maven.plugin-tools</groupId>
<version>3.6.5-SNAPSHOT</version>
<artifactId>maven-plugin-tools</artifactId>
<version>4.0.0-SNAPSHOT</version>
</parent>

<groupId>org.apache.maven.plugins</groupId>
Expand All @@ -38,13 +38,14 @@
</description>

<prerequisites>
<maven>${mavenVersion}</maven>
<maven>3.2.5</maven>
</prerequisites>

<properties>
<doxiaVersion>1.11.1</doxiaVersion>
<doxia-sitetoolsVersion>1.11.1</doxia-sitetoolsVersion>
<it.debug>true</it.debug>
<javaVersion>8</javaVersion>
</properties>

<dependencies>
Expand Down
11 changes: 8 additions & 3 deletions maven-plugin-plugin/src/it/fix-maven-since-3.x/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
<module>javasample-maven-plugin</module>
</modules>

<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>

<dependencies>
<dependency>
<groupId>org.codehaus.plexus</groupId>
Expand All @@ -46,7 +51,7 @@
<plugins>
<plugin>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.1</version>
<version>@project.version@</version>
<executions>
<execution>
<id>generated-helpmojo</id>
Expand All @@ -59,7 +64,7 @@
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-tools-ant</artifactId>
<version>3.1</version>
<version>@project.version@</version>
</dependency>
</dependencies>
</plugin>
Expand Down Expand Up @@ -87,7 +92,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.1</version>
<version>@project.version@</version>
</plugin>
</plugins>
</reporting>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;

Expand All @@ -31,7 +30,7 @@
@Mojo(name="test-plugin",defaultPhase = LifecyclePhase.GENERATE_SOURCES)
public class MyMojo extends AbstractMojo {
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
public void execute() throws MojoExecutionException {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,10 @@ static class PluginOverviewRenderer
private final Locale locale;

/**
* @param project not null
* @param sink not null
* @param locale not null
*/
public PluginOverviewRenderer( Sink sink, Locale locale )
PluginOverviewRenderer( Sink sink, Locale locale )
{
super( sink );

Expand Down
14 changes: 14 additions & 0 deletions maven-plugin-plugin/src/it/plugin-report/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ under the License.

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>

<dependencies>
Expand Down Expand Up @@ -75,6 +77,18 @@ under the License.

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.0.0</version>
<dependencies>
<dependency>
<groupId>org.codehaus.mojo</groupId>
<artifactId>extra-enforcer-rules</artifactId>
<version>1.5.1</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ protected void executeReport( Locale locale )
{
return;
}
if (skip || skipReport)
if ( skip || skipReport )
{
getLog().info( "Maven Plugin Plugin Report generation skipped." );
return;
Expand Down Expand Up @@ -181,12 +181,10 @@ static class PluginOverviewRenderer
private final Locale locale;

/**
* @param project not null
* @param sink not null
* @param locale not null
*/
public PluginOverviewRenderer( Sink sink,
Locale locale )
PluginOverviewRenderer( Sink sink, Locale locale )
{
super( sink );

Expand Down
4 changes: 4 additions & 0 deletions maven-plugin-plugin/src/it/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ under the License.
<url>@localRepositoryUrl@</url>
<releases>
<enabled>true</enabled>
<checksumPolicy>ignore</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<checksumPolicy>ignore</checksumPolicy>
</snapshots>
</repository>
</repositories>
Expand All @@ -51,9 +53,11 @@ under the License.
<url>@localRepositoryUrl@</url>
<releases>
<enabled>true</enabled>
<checksumPolicy>ignore</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<checksumPolicy>ignore</checksumPolicy>
</snapshots>
</pluginRepository>
</pluginRepositories>
Expand Down
6 changes: 5 additions & 1 deletion maven-plugin-tools-annotations/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-tools</artifactId>
<version>3.6.5-SNAPSHOT</version>
<version>4.0.0-SNAPSHOT</version>
</parent>

<artifactId>maven-plugin-tools-annotations</artifactId>
Expand Down Expand Up @@ -131,6 +131,10 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,22 @@ private DocletTag findInClassHierarchy( JavaClass javaClass, String tagName )
}
catch ( NoClassDefFoundError e )
{
getLogger().warn( "Failed extracting tag '" + tagName + "' from class " + javaClass );
throw e;
if ( e.getMessage().replace( '/', '.' )
.contains( MojoAnnotationsScanner.V4_API_PLUGIN_PACKAGE ) )
{
return null;
}
String str;
try
{
str = javaClass.getFullyQualifiedName();
}
catch ( Throwable t )
{
str = javaClass.getValue();
}
getLogger().warn( "Failed extracting tag '" + tagName + "' from class " + str );
throw (NoClassDefFoundError) new NoClassDefFoundError( e.getMessage() ).initCause( e );
}
}

Expand Down Expand Up @@ -438,8 +452,22 @@ private Map<String, JavaField> extractFieldParameterTags( JavaClass javaClass,
}
catch ( NoClassDefFoundError e )
{
getLogger().warn( "Failed extracting parameters from " + javaClass );
throw e;
if ( e.getMessage().replace( '/', '.' )
.contains( MojoAnnotationsScanner.V4_API_PLUGIN_PACKAGE ) )
{
return new TreeMap<>();
}
String str;
try
{
str = javaClass.getFullyQualifiedName();
}
catch ( Throwable t )
{
str = javaClass.getValue();
}
getLogger().warn( "Failed extracting parameters from " + str );
throw (NoClassDefFoundError) new NoClassDefFoundError( e.getMessage() ).initCause( e );
}
}

Expand Down Expand Up @@ -531,6 +559,8 @@ private List<MojoDescriptor> toMojoDescriptors( Map<String, MojoAnnotatedClass>
mojoDescriptor.setImplementation( mojoAnnotatedClass.getClassName() );
mojoDescriptor.setLanguage( "java" );

mojoDescriptor.setV4Api( mojoAnnotatedClass.isV4Api() );

MojoAnnotationContent mojo = mojoAnnotatedClass.getMojo();

mojoDescriptor.setDescription( mojo.getDescription() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
Expand All @@ -65,6 +67,17 @@ public class DefaultMojoAnnotationsScanner
extends AbstractLogEnabled
implements MojoAnnotationsScanner
{
public static final String MVN4_API = "org.apache.maven.api.plugin.annotations.";
public static final String MOJO_V4 = MVN4_API + "Mojo";
public static final String EXECUTE_V4 = MVN4_API + "Execute";
public static final String PARAMETER_V4 = MVN4_API + "Parameter";
public static final String COMPONENT_V4 = MVN4_API + "Component";

public static final String MOJO_V3 = Mojo.class.getName();
public static final String EXECUTE_V3 = Execute.class.getName();
public static final String PARAMETER_V3 = Parameter.class.getName();
public static final String COMPONENT_V3 = Component.class.getName();

// classes with a dash must be ignored
private static final Pattern SCANNABLE_CLASS = Pattern.compile( "[^-]+\\.class" );
private static final String EMPTY = "";
Expand Down Expand Up @@ -271,7 +284,11 @@ protected void analyzeVisitors( MojoClassVisitor mojoClassVisitor )
try
{
// @Mojo annotation
MojoAnnotationVisitor mojoAnnotationVisitor = mojoClassVisitor.getAnnotationVisitor( Mojo.class );
MojoAnnotationVisitor mojoAnnotationVisitor = mojoClassVisitor.getAnnotationVisitor( MOJO_V3 );
if ( mojoAnnotationVisitor == null )
{
mojoAnnotationVisitor = mojoClassVisitor.getAnnotationVisitor( MOJO_V4 );
}
if ( mojoAnnotationVisitor != null )
{
MojoAnnotationContent mojoAnnotationContent = new MojoAnnotationContent();
Expand All @@ -286,7 +303,11 @@ protected void analyzeVisitors( MojoClassVisitor mojoClassVisitor )
}

// @Execute annotation
mojoAnnotationVisitor = mojoClassVisitor.getAnnotationVisitor( Execute.class );
mojoAnnotationVisitor = mojoClassVisitor.getAnnotationVisitor( EXECUTE_V3 );
if ( mojoAnnotationVisitor == null )
{
mojoAnnotationVisitor = mojoClassVisitor.getAnnotationVisitor( EXECUTE_V4 );
}
if ( mojoAnnotationVisitor != null )
{
ExecuteAnnotationContent executeAnnotationContent = new ExecuteAnnotationContent();
Expand All @@ -295,16 +316,24 @@ protected void analyzeVisitors( MojoClassVisitor mojoClassVisitor )
}

// @Parameter annotations
List<MojoFieldVisitor> mojoFieldVisitors = mojoClassVisitor.findFieldWithAnnotation( Parameter.class );
List<MojoFieldVisitor> mojoFieldVisitors = mojoClassVisitor.findFieldWithAnnotation(
new HashSet<>( Arrays.asList( PARAMETER_V3, PARAMETER_V4 ) ) );
for ( MojoFieldVisitor mojoFieldVisitor : mojoFieldVisitors )
{
ParameterAnnotationContent parameterAnnotationContent =
new ParameterAnnotationContent( mojoFieldVisitor.getFieldName(), mojoFieldVisitor.getClassName() );

Map<String, MojoAnnotationVisitor> annotationVisitorMap = mojoFieldVisitor.getAnnotationVisitorMap();
MojoAnnotationVisitor fieldAnnotationVisitor = annotationVisitorMap.get( Parameter.class.getName() );
MojoAnnotationVisitor fieldAnnotationVisitor = annotationVisitorMap.get( PARAMETER_V3 );
if ( fieldAnnotationVisitor == null )
{
fieldAnnotationVisitor = annotationVisitorMap.get( PARAMETER_V4 );
}

populateAnnotationContent( parameterAnnotationContent, fieldAnnotationVisitor );
if ( fieldAnnotationVisitor != null )
{
populateAnnotationContent( parameterAnnotationContent, fieldAnnotationVisitor );
}

if ( annotationVisitorMap.containsKey( Deprecated.class.getName() ) )
{
Expand All @@ -316,14 +345,19 @@ protected void analyzeVisitors( MojoClassVisitor mojoClassVisitor )
}

// @Component annotations
mojoFieldVisitors = mojoClassVisitor.findFieldWithAnnotation( Component.class );
mojoFieldVisitors = mojoClassVisitor.findFieldWithAnnotation(
new HashSet<>( Arrays.asList( COMPONENT_V3, COMPONENT_V4 ) ) );
for ( MojoFieldVisitor mojoFieldVisitor : mojoFieldVisitors )
{
ComponentAnnotationContent componentAnnotationContent =
new ComponentAnnotationContent( mojoFieldVisitor.getFieldName() );

Map<String, MojoAnnotationVisitor> annotationVisitorMap = mojoFieldVisitor.getAnnotationVisitorMap();
MojoAnnotationVisitor annotationVisitor = annotationVisitorMap.get( Component.class.getName() );
MojoAnnotationVisitor annotationVisitor = annotationVisitorMap.get( COMPONENT_V3 );
if ( annotationVisitor == null )
{
annotationVisitor = annotationVisitorMap.get( COMPONENT_V4 );
}

if ( annotationVisitor != null )
{
Expand Down
Loading

0 comments on commit 4a4b1f0

Please sign in to comment.