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 import java.nio.file.Files;
25
26 import org.apache.maven.artifact.Artifact;
27 import org.apache.maven.execution.MavenSession;
28 import org.apache.maven.model.building.ModelBuildingRequest;
29 import org.apache.maven.plugin.AbstractMojo;
30 import org.apache.maven.plugin.MojoExecutionException;
31 import org.apache.maven.plugins.annotations.Component;
32 import org.apache.maven.plugins.annotations.Parameter;
33 import org.apache.maven.project.DefaultProjectBuildingRequest;
34 import org.apache.maven.project.MavenProject;
35 import org.apache.maven.project.ProjectBuilder;
36 import org.apache.maven.project.ProjectBuildingRequest;
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
89 @Parameter(property = "output")
90 protected File output;
91
92
93
94
95
96
97
98
99
100 protected static void writeFile(File output, StringBuilder content) throws IOException {
101 writeFile(output, content.toString());
102 }
103
104
105
106
107
108
109
110
111 protected static void writeFile(File output, String content) throws IOException {
112 if (output == null) {
113 return;
114 }
115
116 output.getParentFile().mkdirs();
117 try (Writer out = Files.newBufferedWriter(output.toPath())) {
118 out.write(content);
119 }
120 }
121
122
123
124
125
126
127
128
129
130 protected org.eclipse.aether.artifact.Artifact getAetherArtifact(String artifactString, String type)
131 throws MojoExecutionException {
132 if (artifactString == null || artifactString.isEmpty()) {
133 throw new IllegalArgumentException("artifact parameter could not be empty");
134 }
135
136 String groupId;
137 String artifactId;
138 String version;
139
140 String[] artifactParts = artifactString.split(":");
141 switch (artifactParts.length) {
142 case 2:
143 groupId = artifactParts[0];
144 artifactId = artifactParts[1];
145 version = Artifact.LATEST_VERSION;
146 break;
147 case 3:
148 groupId = artifactParts[0];
149 artifactId = artifactParts[1];
150 version = artifactParts[2];
151 break;
152 default:
153 throw new MojoExecutionException("The artifact parameter '" + artifactString
154 + "' should be conform to: " + "'groupId:artifactId[:version]'.");
155 }
156
157 return new DefaultArtifact(groupId, artifactId, type, version);
158 }
159
160
161
162
163
164
165
166
167
168
169 protected MavenProject getMavenProject(String artifactString) throws MojoExecutionException {
170 try {
171 ProjectBuildingRequest pbr = new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
172 pbr.setRemoteRepositories(project.getRemoteArtifactRepositories());
173 pbr.setPluginArtifactRepositories(project.getPluginArtifactRepositories());
174 pbr.setProject(null);
175 pbr.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL);
176 pbr.setResolveDependencies(true);
177
178 org.eclipse.aether.artifact.Artifact artifact =
179 resolveArtifact(getAetherArtifact(artifactString, "pom")).getArtifact();
180
181 return projectBuilder.build(artifact.getFile(), pbr).getProject();
182 } catch (Exception e) {
183 throw new MojoExecutionException(
184 "Unable to get the POM for the artifact '" + artifactString + "'. Verify the artifact parameter.",
185 e);
186 }
187 }
188
189 protected org.eclipse.aether.resolution.ArtifactResult resolveArtifact(
190 org.eclipse.aether.artifact.Artifact artifact) throws RepositoryException {
191 RepositorySystemSession repositorySession = session.getRepositorySession();
192
193
194 ArtifactDescriptorResult artifactDescriptor = repositorySystem.readArtifactDescriptor(
195 repositorySession,
196 new ArtifactDescriptorRequest(artifact, project.getRemoteProjectRepositories(), null));
197
198 return repositorySystem.resolveArtifact(
199 repositorySession,
200 new ArtifactRequest(artifactDescriptor.getArtifact(), project.getRemoteProjectRepositories(), null));
201 }
202 }