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