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

前端 未结 6 882
情歌与酒
情歌与酒 2020-12-04 17:03

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. Goo

相关标签:
6条回答
  • 2020-12-04 17:32

    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>
    
    0 讨论(0)
  • 2020-12-04 17:36

    The dependency matrix information for provided scopes and dynamically acquired components can be a bit confusing. It was for me, since it appeared to me that I got all the required items by direct or transitive dependency, but then remote resolution didn't work.

    I wanted to jump to Maven 3.3.3 (latest as of 2015-05-25). I got it working without the sisu errors that presented when I tried to optimistically update to current versions of things specified here (and elsewhere). This is a project with a tag that worked with the example specified as of today using JDK8.

    https://github.com/mykelalvis/test-maven-embedder/tree/20150525-working

    Relevant deps (SLF4J is just so I can see the logs)

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-embedder</artifactId>
            <version>3.3.3</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.aether</groupId>
            <artifactId>aether-connector-basic</artifactId>
            <version>1.0.2.v20150114</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.aether</groupId>
            <artifactId>aether-transport-wagon</artifactId>
            <version>1.0.2.v20150114</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.wagon</groupId>
            <artifactId>wagon-http</artifactId>
            <version>2.9</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.wagon</groupId>
            <artifactId>wagon-provider-api</artifactId>
            <version>2.9</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.wagon</groupId>
            <artifactId>wagon-http-lightweight</artifactId>
            <version>2.9</version>
        </dependency>   
    

    Running this is:

    rm -r ~/.m2/repository/org/apache/maven/plugins/maven-clean-plugin/
    mvn exec:java
    

    Probably should have made it a unit test of some sort.

    If someone has a superior solution for embedded Maven 3.3.3 (i.e. came up with a smaller or more range-oriented set of required dependencies), please post them.

    0 讨论(0)
  • 2020-12-04 17:39

    to build on the comment from @StevePerkins, and using maven version 3.1.0,

    I had to exclude the transitive dependency from aether-connector-wagon to wagon-provider-api to get it working.

    pom.xml:

    (...)
    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-embedder</artifactId>
        <version>3.1.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.eclipse.aether</groupId>
        <artifactId>aether-connector-wagon</artifactId>
        <version>0.9.0.M2</version>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.apache.maven.wagon</groupId>
                <artifactId>wagon-provider-api</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.maven.wagon</groupId>
        <artifactId>wagon-http</artifactId>
        <version>2.5</version>
        <scope>test</scope>
    </dependency>
    (...)
    

    and here is a java example:

    (...)
    MavenCli cli = new MavenCli();
    
    ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
    ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
    
    PrintStream out = new PrintStream(baosOut, true);
    PrintStream err = new PrintStream(baosErr, true);
    
    cli.doMain( new String[] { "clean" }, new File("."), out, err );
    
    String stdout = baosOut.toString("UTF-8");
    String stderr = baosErr.toString("UTF-8");
    (...)
    

    full example here

    0 讨论(0)
  • 2020-12-04 17:39

    There is a dependency matrix for each version of maven-embedder, e.g. for 3.2.5: http://maven.apache.org/ref/3.2.5/maven-embedder/dependencies.html

    Based on that I had to use org.apache.maven:maven-embedder:jar:3.2.5, org.apache.maven:maven-aether-provider:jar:3.2.5, and org.apache.maven.wagon:wagon-provider-api:jar:2.8.

    It also fixes dependency on very old Guava library, since this version uses 18.0.

    0 讨论(0)
  • 2020-12-04 17:41

    Dependency list for Maven Embedded 3.6.3 version that works in my Spring Boot 2.3 project (JDK8 or JDK 11 runtime):

        <!-- Maven Embedder -->
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-embedder</artifactId>
            <version>3.6.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-compat</artifactId>
            <version>3.6.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.wagon</groupId>
            <artifactId>wagon-http</artifactId>
            <version>3.3.4</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.aether</groupId>
            <artifactId>aether-connector-basic</artifactId>
            <version>1.1.0</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.eclipse.aether</groupId>
            <artifactId>aether-transport-wagon</artifactId>
            <version>1.1.0</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.30</version>
        </dependency>
        <dependency>
            <groupId>org.usefultoys</groupId>
            <artifactId>slf4j-toys</artifactId>
            <version>1.6.3</version>
        </dependency>
    

    The Maven CLI command looks like to:

        // Maven CLI to execute Maven Commands
        MavenCli cli = new MavenCli();
        int result = cli.doMain(args, workingDirectory,
                        org.usefultoys.slf4j.LoggerFactory.getInfoPrintStream(LOGGER),
                        org.usefultoys.slf4j.LoggerFactory.getErrorPrintStream(LOGGER));
    

    HTH

    0 讨论(0)
  • 2020-12-04 17:49

    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...

    0 讨论(0)
提交回复
热议问题