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