[英]Maven not recognizing parameters when running custom mojo from CLI
[英]Pass parameters to a maven mojo invoked programmatically
在这里,我有我的 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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.