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