Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can anyone give a good example of using org.apache.maven.cli.MavenCli programmatically?

Tags:

maven-3

api

I'm trying to create an intelliJ plugin that needs to execute maven targets on the current project. All the talk in the intertubes recommends using the MavenEmbedder. Good luck with that. The 2.0.4 version isn't well supported and there are no references for how to use it.

I gave it a whirl and ran into a wall where the embedder had not been initialized with all the fields it needs. Reflective private member injection? Awesome! Why would anyone need an obvious way to initialize an object?

It seems a few people are using a 2.1 version with some success. I have been unable to find that in a jar or even sources.

I went and checked out the 3.0 version of the embedder project: http://maven.apache.org/ref/3.0-beta-3/maven-embedder/ It does away with the MavenEmbedder object all together and seems to only support access through the main or doMain methods on MavenCli. Has anyone used these methods and can give some advice?

like image 405
Nick Orton Avatar asked Nov 17 '10 16:11

Nick Orton


2 Answers

Yeah, the's not much in the way of documentation of MavenCli. The API is significatly simpler but i'd still like some examples. Here's one that works...

MavenCli cli = new MavenCli();
int result = cli.doMain(new String[]{"compile"},
        "/home/aioffe/workspace/MiscMaven",
        System.out, System.out);
System.out.println("result: " + result);

It takes a dir and runs the 'compile' phase...

like image 173
alexi Avatar answered Oct 03 '22 16:10

alexi


Working maven configuration for maven 3.6.3

Code

MavenCli cli = new MavenCli();
System.setProperty("maven.multiModuleProjectDirectory", workingDirectory);
cli.doMain(new String[]{"compile"}, workingDirectory, System.out, System.err);

Dependencies

<dependencies>
    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-embedder</artifactId>
        <version>3.6.3</version>
    </dependency>
    <!-- https://issues.apache.org/jira/browse/MNG-5995 -->
    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-compat</artifactId>
        <version>3.6.3</version>
    </dependency>

    <!-- enable logging -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.7.30</version>
    </dependency>
</dependencies>
like image 20
MariuszS Avatar answered Oct 03 '22 16:10

MariuszS