Perform multiple mojo from one "meta mojo" and sharing options

I created a maven plugin with some mojos, each of which has a special purpose. But for the end user, it would be nice to execute some of them right away (order is critical).

So how to execute other mojos from mojos execute? Completed mojos have @Parameter fields. Therefore, I can’t just new MyMojo().execute.

My second question is: is there a way to share some @Parameters between Mojos, or should I declare "@Parameter" in every Mojo that uses them? My idea is to somehow deliver all the common parameters through a utility class that provides getters parameters.

I think that the answer to both questions somehow lies in understanding the DI mechanism behind maven-mojos ?! I have some experience with Guice, but not with Plexus. So can someone please give me some advice?

+4
source share
1 answer

I don’t know exactly what your second question means. Maybe they really are not connected. But I am trying to answer both questions, starting with the first.

Question 1: How to call a target from another target?

You can use the Apache Maven Invoker for this .

  • Add the Maven dependency to your plugin.
    For example:

    <dependency>
        <groupId>org.apache.maven.shared</groupId>
        <artifactId>maven-invoker</artifactId>
        <version>2.2</version>
    </dependency>
    
  • And then you can call up another target as follows:

    // 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);
    

Question 2: How can I share options between mojos?

You meant:

  • ?
    (. " 2.1" )
  • "meta mojo" " "?
    (. " 2.2" )

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());
    }
}

maven. , Apache Maven Plugin.

2.2:

1. "-", properties.

+2

All Articles