1 package org.apache.maven.plugins.help;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.io.Writer;
25 import java.util.List;
26
27 import org.apache.maven.artifact.Artifact;
28 import org.apache.maven.artifact.repository.ArtifactRepository;
29 import org.apache.maven.execution.MavenSession;
30 import org.apache.maven.model.building.ModelBuildingRequest;
31 import org.apache.maven.plugin.AbstractMojo;
32 import org.apache.maven.plugin.MojoExecutionException;
33 import org.apache.maven.plugins.annotations.Component;
34 import org.apache.maven.plugins.annotations.Parameter;
35 import org.apache.maven.project.DefaultProjectBuildingRequest;
36 import org.apache.maven.project.MavenProject;
37 import org.apache.maven.project.ProjectBuilder;
38 import org.apache.maven.project.ProjectBuildingRequest;
39 import org.apache.maven.shared.artifact.ArtifactCoordinate;
40 import org.apache.maven.shared.artifact.DefaultArtifactCoordinate;
41 import org.apache.maven.shared.artifact.resolve.ArtifactResolver;
42 import org.codehaus.plexus.util.IOUtil;
43 import org.codehaus.plexus.util.StringUtils;
44 import org.codehaus.plexus.util.WriterFactory;
45
46
47
48
49
50
51
52
53 public abstract class AbstractHelpMojo
54 extends AbstractMojo
55 {
56
57 protected static final int LINE_LENGTH = 79;
58
59
60 protected static final String LS = System.getProperty( "line.separator" );
61
62
63
64
65 @Component
66 protected ProjectBuilder projectBuilder;
67
68
69
70
71 @Component
72 protected ArtifactResolver artifactResolver;
73
74
75
76
77 @Parameter( defaultValue = "${project.remoteArtifactRepositories}", required = true, readonly = true )
78 protected List<ArtifactRepository> remoteRepositories;
79
80
81
82
83 @Parameter( defaultValue = "${localRepository}", required = true, readonly = true )
84 protected ArtifactRepository localRepository;
85
86
87
88
89
90 @Parameter( defaultValue = "${session}", readonly = true, required = true )
91 protected MavenSession session;
92
93
94
95
96
97
98 @Parameter( property = "output" )
99 protected File output;
100
101
102
103
104
105
106
107
108
109 protected static void writeFile( File output, StringBuilder content )
110 throws IOException
111 {
112 writeFile( output, content.toString() );
113 }
114
115
116
117
118
119
120
121
122 protected static void writeFile( File output, String content )
123 throws IOException
124 {
125 if ( output == null )
126 {
127 return;
128 }
129
130 Writer out = null;
131 try
132 {
133 output.getParentFile().mkdirs();
134
135 out = WriterFactory.newPlatformWriter( output );
136
137 out.write( content );
138
139 out.close();
140 out = null;
141 }
142 finally
143 {
144 IOUtil.close( out );
145 }
146 }
147
148
149
150
151
152
153
154
155
156 protected ArtifactCoordinate getArtifactCoordinate( String artifactString, String type )
157 throws MojoExecutionException
158 {
159 if ( StringUtils.isEmpty( artifactString ) )
160 {
161 throw new IllegalArgumentException( "artifact parameter could not be empty" );
162 }
163
164 String groupId;
165 String artifactId;
166 String version;
167
168 String[] artifactParts = artifactString.split( ":" );
169 switch ( artifactParts.length )
170 {
171 case 2:
172 groupId = artifactParts[0];
173 artifactId = artifactParts[1];
174 version = Artifact.LATEST_VERSION;
175 break;
176 case 3:
177 groupId = artifactParts[0];
178 artifactId = artifactParts[1];
179 version = artifactParts[2];
180 break;
181 default:
182 throw new MojoExecutionException( "The artifact parameter '" + artifactString
183 + "' should be conform to: " + "'groupId:artifactId[:version]'." );
184 }
185 return getArtifactCoordinate( groupId, artifactId, version, type );
186 }
187
188 protected ArtifactCoordinate getArtifactCoordinate( String groupId, String artifactId, String version, String type )
189 {
190 DefaultArtifactCoordinate coordinate = new DefaultArtifactCoordinate();
191 coordinate.setGroupId( groupId );
192 coordinate.setArtifactId( artifactId );
193 coordinate.setVersion( version );
194 coordinate.setExtension( type );
195 return coordinate;
196 }
197
198
199
200
201
202
203
204
205
206
207 protected MavenProject getMavenProject( String artifactString )
208 throws MojoExecutionException
209 {
210 ArtifactCoordinate coordinate = getArtifactCoordinate( artifactString, "pom" );
211 try
212 {
213 ProjectBuildingRequest pbr = new DefaultProjectBuildingRequest( session.getProjectBuildingRequest() );
214 pbr.setRemoteRepositories( remoteRepositories );
215 pbr.setProject( null );
216 pbr.setValidationLevel( ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL );
217 pbr.setResolveDependencies( true );
218 Artifact artifact = artifactResolver.resolveArtifact( pbr, coordinate ).getArtifact();
219 return projectBuilder.build( artifact.getFile(), pbr ).getProject();
220 }
221 catch ( Exception e )
222 {
223 throw new MojoExecutionException( "Unable to get the POM for the artifact '" + artifactString
224 + "'. Verify the artifact parameter.", e );
225 }
226 }
227
228 }