1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.help;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.io.Writer;
24
25 import org.apache.maven.artifact.Artifact;
26 import org.apache.maven.execution.MavenSession;
27 import org.apache.maven.model.building.ModelBuildingRequest;
28 import org.apache.maven.plugin.AbstractMojo;
29 import org.apache.maven.plugin.MojoExecutionException;
30 import org.apache.maven.plugins.annotations.Component;
31 import org.apache.maven.plugins.annotations.Parameter;
32 import org.apache.maven.project.DefaultProjectBuildingRequest;
33 import org.apache.maven.project.MavenProject;
34 import org.apache.maven.project.ProjectBuilder;
35 import org.apache.maven.project.ProjectBuildingRequest;
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.resolution.ArtifactDescriptorRequest;
42 import org.eclipse.aether.resolution.ArtifactDescriptorResult;
43 import org.eclipse.aether.resolution.ArtifactRequest;
44
45
46
47
48
49
50
51 public abstract class AbstractHelpMojo extends AbstractMojo {
52
53 protected static final int LINE_LENGTH = 79;
54
55
56 protected static final String LS = System.getProperty("line.separator");
57
58
59
60
61 @Component
62 protected ProjectBuilder projectBuilder;
63
64
65
66
67 @Component
68 protected RepositorySystem repositorySystem;
69
70
71
72
73 @Parameter(defaultValue = "${project}", readonly = true, required = true)
74 protected MavenProject project;
75
76
77
78
79
80 @Parameter(defaultValue = "${session}", readonly = true, required = true)
81 protected MavenSession session;
82
83
84
85
86
87
88 @Parameter(property = "output")
89 protected File output;
90
91
92
93
94
95
96
97
98
99 protected static void writeFile(File output, StringBuilder content) throws IOException {
100 writeFile(output, content.toString());
101 }
102
103
104
105
106
107
108
109
110 protected static void writeFile(File output, String content) throws IOException {
111 if (output == null) {
112 return;
113 }
114
115 output.getParentFile().mkdirs();
116 try (Writer out = WriterFactory.newPlatformWriter(output)) {
117 out.write(content);
118 }
119 }
120
121
122
123
124
125
126
127
128
129 protected org.eclipse.aether.artifact.Artifact getAetherArtifact(String artifactString, String type)
130 throws MojoExecutionException {
131 if (artifactString == null || artifactString.isEmpty()) {
132 throw new IllegalArgumentException("artifact parameter could not be empty");
133 }
134
135 String groupId;
136 String artifactId;
137 String version;
138
139 String[] artifactParts = artifactString.split(":");
140 switch (artifactParts.length) {
141 case 2:
142 groupId = artifactParts[0];
143 artifactId = artifactParts[1];
144 version = Artifact.LATEST_VERSION;
145 break;
146 case 3:
147 groupId = artifactParts[0];
148 artifactId = artifactParts[1];
149 version = artifactParts[2];
150 break;
151 default:
152 throw new MojoExecutionException("The artifact parameter '" + artifactString
153 + "' should be conform to: " + "'groupId:artifactId[:version]'.");
154 }
155
156 return new DefaultArtifact(groupId, artifactId, type, version);
157 }
158
159
160
161
162
163
164
165
166
167
168 protected MavenProject getMavenProject(String artifactString) throws MojoExecutionException {
169 try {
170 ProjectBuildingRequest pbr = new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
171 pbr.setRemoteRepositories(project.getRemoteArtifactRepositories());
172 pbr.setPluginArtifactRepositories(project.getPluginArtifactRepositories());
173 pbr.setProject(null);
174 pbr.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL);
175 pbr.setResolveDependencies(true);
176
177 org.eclipse.aether.artifact.Artifact artifact =
178 resolveArtifact(getAetherArtifact(artifactString, "pom")).getArtifact();
179
180 return projectBuilder.build(artifact.getFile(), pbr).getProject();
181 } catch (Exception e) {
182 throw new MojoExecutionException(
183 "Unable to get the POM for the artifact '" + artifactString + "'. Verify the artifact parameter.",
184 e);
185 }
186 }
187
188 protected org.eclipse.aether.resolution.ArtifactResult resolveArtifact(
189 org.eclipse.aether.artifact.Artifact artifact) throws RepositoryException {
190 RepositorySystemSession repositorySession = session.getRepositorySession();
191
192
193 ArtifactDescriptorResult artifactDescriptor = repositorySystem.readArtifactDescriptor(
194 repositorySession,
195 new ArtifactDescriptorRequest(artifact, project.getRemoteProjectRepositories(), null));
196
197 return repositorySystem.resolveArtifact(
198 repositorySession,
199 new ArtifactRequest(artifactDescriptor.getArtifact(), project.getRemoteProjectRepositories(), null));
200 }
201 }