1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.tools.plugin.generator;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.InputStreamReader;
25 import java.io.OutputStreamWriter;
26 import java.io.StringWriter;
27 import java.io.Writer;
28
29 import org.apache.maven.project.MavenProject;
30 import org.apache.velocity.VelocityContext;
31 import org.codehaus.plexus.util.io.CachingOutputStream;
32 import org.codehaus.plexus.velocity.VelocityComponent;
33
34 import static java.nio.charset.StandardCharsets.UTF_8;
35
36
37
38
39
40
41
42
43
44 public class PluginHelpGenerator {
45
46
47
48 private static final String HELP_MOJO_CLASS_NAME = "HelpMojo";
49
50 private String helpPackageName;
51 private String goalPrefix;
52 private MavenProject mavenProject;
53 private boolean useMaven4Api;
54 private VelocityComponent velocityComponent;
55
56
57
58
59 public PluginHelpGenerator() {
60
61 }
62
63
64
65
66
67 public void execute(File destinationDirectory) throws GeneratorException {
68 String helpImplementation = getImplementation();
69
70 useMaven4Api = mavenProject.getDependencies().stream()
71 .anyMatch(dep ->
72 "org.apache.maven".equals(dep.getGroupId()) && "maven-api-core".equals(dep.getArtifactId()));
73
74 try {
75 String sourcePath = helpImplementation.replace('.', File.separatorChar) + ".java";
76
77 File helpClass = new File(destinationDirectory, sourcePath);
78 helpClass.getParentFile().mkdirs();
79
80 String helpClassSources = getHelpClassSources(getPluginHelpPath(mavenProject));
81
82 try (Writer w = new OutputStreamWriter(new CachingOutputStream(helpClass), UTF_8)) {
83 w.write(helpClassSources);
84 }
85 } catch (IOException e) {
86 throw new GeneratorException(e.getMessage(), e);
87 }
88 }
89
90 public PluginHelpGenerator setHelpPackageName(String helpPackageName) {
91 this.helpPackageName = helpPackageName;
92 return this;
93 }
94
95 public PluginHelpGenerator setVelocityComponent(VelocityComponent velocityComponent) {
96 this.velocityComponent = velocityComponent;
97 return this;
98 }
99
100 public PluginHelpGenerator setGoalPrefix(String goalPrefix) {
101 this.goalPrefix = goalPrefix;
102 return this;
103 }
104
105 public PluginHelpGenerator setMavenProject(MavenProject mavenProject) {
106 this.mavenProject = mavenProject;
107 return this;
108 }
109
110
111
112
113
114 private String getHelpClassSources(String pluginHelpPath) throws IOException {
115 VelocityContext context = new VelocityContext();
116 boolean useAnnotations =
117 mavenProject.getArtifactMap().containsKey("org.apache.maven.plugin-tools:maven-plugin-annotations");
118
119 context.put("helpPackageName", helpPackageName);
120 context.put("pluginHelpPath", pluginHelpPath);
121 context.put("artifactId", mavenProject.getArtifactId());
122
123 context.put("goalPrefix", goalPrefix);
124 context.put("useAnnotations", useAnnotations);
125
126 StringWriter stringWriter = new StringWriter();
127
128
129 try (InputStream is = Thread.currentThread()
130 .getContextClassLoader()
131 .getResourceAsStream(useMaven4Api ? "help-class-source-v4.vm" : "help-class-source.vm");
132 InputStreamReader isReader = new InputStreamReader(is, UTF_8)) {
133
134 velocityComponent.getEngine().evaluate(context, stringWriter, "", isReader);
135 }
136
137
138 return stringWriter.toString().replaceAll("(\r\n|\n|\r)", System.lineSeparator());
139 }
140
141
142
143
144 private String getImplementation() {
145 return (helpPackageName == null || helpPackageName.isEmpty())
146 ? HELP_MOJO_CLASS_NAME
147 : helpPackageName + '.' + HELP_MOJO_CLASS_NAME;
148 }
149
150 static String getPluginHelpPath(MavenProject mavenProject) {
151 return mavenProject.getGroupId() + "/" + mavenProject.getArtifactId() + "/plugin-help.xml";
152 }
153 }