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 org.apache.maven.RepositoryUtils;
23 import org.apache.maven.artifact.Artifact;
24 import org.apache.maven.artifact.repository.ArtifactRepository;
25 import org.apache.maven.execution.MavenSession;
26 import org.apache.maven.model.building.ModelBuildingRequest;
27 import org.apache.maven.plugin.AbstractMojo;
28 import org.apache.maven.plugin.MojoExecutionException;
29 import org.apache.maven.plugins.annotations.Component;
30 import org.apache.maven.plugins.annotations.Parameter;
31 import org.apache.maven.project.DefaultProjectBuildingRequest;
32 import org.apache.maven.project.MavenProject;
33 import org.apache.maven.project.ProjectBuilder;
34 import org.apache.maven.project.ProjectBuildingRequest;
35 import org.codehaus.plexus.util.StringUtils;
36 import org.codehaus.plexus.util.WriterFactory;
37 import org.eclipse.aether.RepositoryException;
38 import org.eclipse.aether.RepositorySystem;
39 import org.eclipse.aether.RepositorySystemSession;
40 import org.eclipse.aether.artifact.DefaultArtifact;
41 import org.eclipse.aether.repository.RemoteRepository;
42 import org.eclipse.aether.resolution.ArtifactDescriptorRequest;
43 import org.eclipse.aether.resolution.ArtifactDescriptorResult;
44 import org.eclipse.aether.resolution.ArtifactRequest;
45
46 import java.io.File;
47 import java.io.IOException;
48 import java.io.Writer;
49 import java.util.List;
50
51
52
53
54
55
56
57 public abstract class AbstractHelpMojo
58 extends AbstractMojo
59 {
60
61 protected static final int LINE_LENGTH = 79;
62
63
64 protected static final String LS = System.getProperty( "line.separator" );
65
66
67
68
69 @Component
70 protected ProjectBuilder projectBuilder;
71
72
73
74
75 @Component
76 protected RepositorySystem repositorySystem;
77
78
79
80
81 @Parameter( defaultValue = "${project.remoteArtifactRepositories}", required = true, readonly = true )
82 protected List<ArtifactRepository> remoteRepositories;
83
84
85
86
87 @Parameter( defaultValue = "${project.pluginArtifactRepositories}", required = true, readonly = true )
88 protected List<ArtifactRepository> pluginArtifactRepositories;
89
90
91
92
93 @Parameter( defaultValue = "${localRepository}", required = true, readonly = true )
94 protected ArtifactRepository localRepository;
95
96
97
98
99
100 @Parameter( defaultValue = "${session}", readonly = true, required = true )
101 protected MavenSession session;
102
103
104
105
106
107
108 @Parameter( property = "output" )
109 protected File output;
110
111
112
113
114
115
116
117
118
119 protected static void writeFile( File output, StringBuilder content )
120 throws IOException
121 {
122 writeFile( output, content.toString() );
123 }
124
125
126
127
128
129
130
131
132 protected static void writeFile( File output, String content )
133 throws IOException
134 {
135 if ( output == null )
136 {
137 return;
138 }
139
140 output.getParentFile().mkdirs();
141 try ( Writer out = WriterFactory.newPlatformWriter( output ) )
142 {
143 out.write( content );
144 }
145 }
146
147
148
149
150
151
152
153
154
155 protected org.eclipse.aether.artifact.Artifact getAetherArtifact( 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
185 return new DefaultArtifact( groupId, artifactId, type, version );
186 }
187
188
189
190
191
192
193
194
195
196
197 protected MavenProject getMavenProject( String artifactString )
198 throws MojoExecutionException
199 {
200 try
201 {
202 ProjectBuildingRequest pbr = new DefaultProjectBuildingRequest( session.getProjectBuildingRequest() );
203 pbr.setRemoteRepositories( remoteRepositories );
204 pbr.setLocalRepository( localRepository );
205 pbr.setPluginArtifactRepositories( pluginArtifactRepositories );
206 pbr.setProject( null );
207 pbr.setValidationLevel( ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL );
208 pbr.setResolveDependencies( true );
209
210 org.eclipse.aether.artifact.Artifact artifact = resolveArtifact(
211 getAetherArtifact( artifactString, "pom" ) ).getArtifact();
212
213 return projectBuilder.build( artifact.getFile(), pbr ).getProject();
214 }
215 catch ( Exception e )
216 {
217 throw new MojoExecutionException( "Unable to get the POM for the artifact '" + artifactString
218 + "'. Verify the artifact parameter.", e );
219 }
220 }
221
222 protected org.eclipse.aether.resolution.ArtifactResult resolveArtifact(
223 org.eclipse.aether.artifact.Artifact artifact ) throws RepositoryException
224 {
225 List<RemoteRepository> repositories = RepositoryUtils.toRepos( remoteRepositories );
226 RepositorySystemSession repositorySession = session.getProjectBuildingRequest().getRepositorySession();
227
228
229 ArtifactDescriptorResult artifactDescriptor = repositorySystem.readArtifactDescriptor(
230 repositorySession, new ArtifactDescriptorRequest( artifact, repositories, null ) );
231
232 return repositorySystem.resolveArtifact( repositorySession,
233 new ArtifactRequest( artifactDescriptor.getArtifact(), repositories, null ) );
234 }
235
236 }