Calling Maven goals from Java

后端 未结 3 1612
臣服心动
臣服心动 2021-02-05 07:37

Is it possible to call Maven goals from Java, for instance, can I do the equivalent of:

mvn clean package

from a Java class?

thanks, Ni

3条回答
  •  天涯浪人
    2021-02-05 08:00

    This is easy :)

    Java code

    MavenCli cli = new MavenCli();
    cli.doMain(new String[]{"clean", "package"}, "project_dir", System.out, System.out);
    

    Project configuration:

    
        
            org.apache.maven
            maven-embedder
            3.1.1
        
        
            org.eclipse.aether
            aether-connector-wagon
            0.9.0.M2
        
        
            org.apache.maven.wagon
            wagon-http-lightweight
            2.5
        
    
    

    Fully working example: https://github.com/mariuszs/maven-cli-example

提交回复
热议问题