der*_*itz 5 dependency-injection maven-plugin
我创建了一个带有一些mojos的maven-plugin,每个都有一个非常特殊的目的.但对于最终用户来说,一次执行其中一些会很好(顺序是至关重要的).
那么如何从mojos执行其他mojos执行?正在执行的mojos有一些@Parameter字段.所以我不能简单new MyMojo().execute
.
我的第二个问题是:有没有办法在Mojos之间共享一些@Parameters,还是我必须在每个使用它们的Mojo中声明"@Parameter"?我的想法是以某种方式通过实用程序类提供所有共享参数,该实用程序类为getter提供参数.
我认为这两个问题的答案都在于理解maven-mojos背后的DI机制?!我对Guice有一些经验,但对Plexus没有经验.那么有人可以给我一些建议吗?
我不太清楚你的第二个问题是什么意思。也许他们并没有真正的关系。但我尝试从第一个开始回答这两个问题。
问题1:如何从一个Goal调用另一个Goal?
为此,您可以使用Apache Maven Invoker。
将 Maven 依赖项添加到您的插件中。
例如:
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-invoker</artifactId>
<version>2.2</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)然后你可以这样调用另一个目标:
// parameters:
final Properties properties = new Properties();
properties.setProperty("example.param.one", exampleValueOne);
// prepare the execution:
final InvocationRequest invocationRequest = new DefaultInvocationRequest();
invocationRequest.setPomFile(new File(pom)); // pom could be an injected field annotated with '@Parameter(defaultValue = "${basedir}/pom.xml")' if you want to use the same pom for the second goal
invocationRequest.setGoals(Collections.singletonList("second-plugin:example-goal"));
invocationRequest.setProperties(properties);
// configure logging:
final Invoker invoker = new DefaultInvoker();
invoker.setOutputHandler(new LogOutputHandler(getLog())); // using getLog() here redirects all log output directly to the current console
// execute:
final InvocationResult invocationResult = invoker.execute(invocationRequest);
Run Code Online (Sandbox Code Playgroud)问题2:如何在mojos之间共享参数?
你的意思:
对问题2.1的回答:
您可以创建一个包含参数字段的抽象父类。
例子:
abstract class AbstractMyPluginMojo extends Abstract Mojo {
@Parameter(required = true)
private String someParam;
protected String getSomeParam() {
return someParam;
}
}
@Mojo(name = "first-mojo")
public class MyFirstMojo extends AbstractMyPluginMojo {
public final void execute() {
getLog().info("someParam: " + getSomeParam());
}
}
@Mojo(name = "second-mojo")
public class MySecondMojo extends AbstractMyPluginMojo {
public final void execute() {
getLog().info("someParam: " + getSomeParam());
}
}
Run Code Online (Sandbox Code Playgroud)
您几乎可以在任何更大的 Maven 插件中找到这种技术。例如,查看Apache Maven 插件源。
对问题2.2的回答:
您可以在我对问题 1 的回答中找到解决方案。如果您想在“元魔力”中执行多个目标,您可以重复使用该properties
变量。
归档时间: |
|
查看次数: |
212 次 |
最近记录: |