1 package org.apache.maven.plugin.plugin;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.Collections;
23 import java.util.List;
24
25 import org.apache.maven.plugin.AbstractMojo;
26 import org.apache.maven.plugin.MojoExecutionException;
27 import org.apache.maven.plugin.descriptor.PluginDescriptor;
28 import org.apache.maven.plugins.annotations.Parameter;
29 import org.apache.maven.project.MavenProject;
30
31
32
33
34
35
36
37 public abstract class AbstractGeneratorMojo
38 extends AbstractMojo
39 {
40
41
42
43 @Parameter( defaultValue = "${project}", readonly = true )
44 protected MavenProject project;
45
46
47
48
49 @Parameter
50 protected String goalPrefix;
51
52
53
54
55
56
57 @Parameter( defaultValue = "false", property = "maven.plugin.skip" )
58 private boolean skip;
59
60
61
62
63
64
65 @Parameter
66 private List<String> packagingTypes = Collections.singletonList( "maven-plugin" );
67
68
69
70
71 protected static final String LS = System.lineSeparator();
72
73 protected abstract void generate() throws MojoExecutionException;
74
75 @Override
76 public void execute()
77 throws MojoExecutionException
78 {
79 if ( !packagingTypes.contains( project.getPackaging() ) )
80 {
81 getLog().info( "Unsupported packaging type " + project.getPackaging() + ", execution skipped" );
82 return;
83 }
84
85 if ( skip )
86 {
87 getLog().warn( "Execution skipped" );
88 return;
89 }
90
91 String defaultGoalPrefix = getDefaultGoalPrefix( project );
92
93 if ( goalPrefix == null )
94 {
95 goalPrefix = defaultGoalPrefix;
96 }
97 else if ( !goalPrefix.equals( defaultGoalPrefix ) )
98 {
99 getLog().warn(
100 LS + LS + "Goal prefix is specified as: '" + goalPrefix + "'. " + "Maven currently expects it to be '"
101 + defaultGoalPrefix + "'." + LS );
102 }
103
104 generate();
105 }
106
107 static String getDefaultGoalPrefix( MavenProject project )
108 {
109 String defaultGoalPrefix;
110 if ( "maven-plugin-report-plugin".equalsIgnoreCase( project.getArtifactId() ) )
111 {
112 defaultGoalPrefix = "plugin-report";
113 }
114 else if ( "maven-plugin".equalsIgnoreCase( project.getArtifactId() ) )
115 {
116 defaultGoalPrefix = project.getGroupId().substring( project.getGroupId().lastIndexOf( '.' ) + 1 );
117 }
118 else
119 {
120 defaultGoalPrefix = PluginDescriptor.getGoalPrefixFromArtifactId( project.getArtifactId() );
121 }
122 return defaultGoalPrefix;
123 }
124 }