这里有我的Maven Mojo:
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.shared.invoker.*;
import java.util.Collections;
@Mojo(name = "run")
public class RunMojo extends AbstractMojo {
@Override
public void execute() throws MojoExecutionException {
InvocationRequest request = new DefaultInvocationRequest();
request.setGoals(Collections.singletonList("myplugin:mygoal"));
// need to set parameters to pass to the goal
Invoker invoker = new DefaultInvoker();
try {
invoker.execute(request);
} catch (MavenInvocationException e) {
e.printStackTrace();
}
}
}
而且我需要调用第二个Mojo传递一些参数,就像在pom.xml中定义插件时一样,如下。
<build>
<plugins>
<plugin>
<artifactId>myPlugin</artifactId>
<groupId>myGroupId</groupId>
<version>myVersion</version>
<configuration>
<param1>value1</param1>
<param2>value2</param2>
<param3>value3</param3>
</configuration>
</plugin>
</plugins>
</build>
任何解决方案?预先谢谢你。
这可能是一个比较老的问题,但是最近发现自己处于这种情况,并告诉我要教我如何创建一个新的插件,该插件将另一个插件包裹起来。
我最终使用了mojo-executor插件(https://github.com/TimMoore/mojo-executor)。即使没有积极维护它,它也可以很好地完成工作。这是我如何通过自己的maven插件执行html2pdf-maven-plugin(https://bitbucket.org/prunge/html2pdf-maven-plugin/wiki/Home)的示例。
generatePdf(...)方法是从Mojo类的execute()方法调用的。我在getMvnPlugin(...)方法中指定了插件groupId和ArtifactId以及执行细节。使用getPluginConfiguration(...)方法进行插件配置,并指定html2pdf-maven-plugin的参数。
private void generatePdf(File inputFilePath, String outputFile) {
String inputDirectory = inputFilePath.getParent();
String inputFileName = inputFilePath.getName();
try {
Plugin html2pdfPlugin = getMvnPlugin(outputFile, inputFile, inputFileName);
MojoExecutor.ExecutionEnvironment executionEnvironment = MojoExecutor.executionEnvironment(mavenProject, mavenSession, pluginManager);
MojoExecutor.executeMojo(html2pdfPlugin, "html2pdf", getPluginConfiguration(outputFile, inputDirectory, inputFileName), executionEnvironment);
} catch (Exception ex) {
String errorMessage = String.format("Failed to generate PDF file [%s] from html file [%s].", outputFile, inputFile);
getLog().error(errorMessage, ex);
}
}
private Plugin getMvnPlugin(String outputFile, String inputDirectory, String inputFileName) {
Plugin plugin = new Plugin();
plugin.setGroupId("au.net.causal.maven.plugins");
plugin.setArtifactId("html2pdf-maven-plugin");
plugin.setVersion("2.0");
PluginExecution pluginExecution = new PluginExecution();
pluginExecution.setGoals(Collections.singletonList("html2pdf"));
pluginExecution.setId("generate-pdf");
pluginExecution.setPhase("generate-resources");
plugin.setExecutions(Collections.singletonList(pluginExecution));
plugin.setConfiguration(getPluginConfiguration(outputFile, inputDirectory, inputFileName));
return plugin;
}
private Xpp3Dom getPluginConfiguration(String outputFile, String inputDirectory, String inputFileName) {
MojoExecutor.Element outputFileElement = new MojoExecutor.Element("outputFile", outputFile);
MojoExecutor.Element includeElement = new MojoExecutor.Element("include", "**/" + inputFileName);
MojoExecutor.Element includesElement = new MojoExecutor.Element("includes", includeElement);
MojoExecutor.Element directoryElement = new MojoExecutor.Element("directory", inputDirectory);
MojoExecutor.Element htmlFileSetElement = new MojoExecutor.Element("htmlFileSet", directoryElement, includesElement);
MojoExecutor.Element htmlFileSetsElement = new MojoExecutor.Element("htmlFileSets", htmlFileSetElement);
return MojoExecutor.configuration(outputFileElement, htmlFileSetsElement);
}
[mavenProject,mavenSession和pluginManager只需使用org.apache.maven.plugins.annotations.Component注入Mojo类中。
@Component
private MavenProject mavenProject;
@Component
private MavenSession mavenSession;
@Component
private BuildPluginManager pluginManager;