通过java执行maven命令

2023-08-04  本文已影响0人  追风还是少年

引入依赖

        <dependency>
            <groupId>org.apache.maven.shared</groupId>
            <artifactId>maven-invoker</artifactId>
            <version>3.0.1</version>
        </dependency>
public static main(){
        // compile、deploy
        String cmd = "compile";

        InvocationRequest request = new DefaultInvocationRequest();

        request.setUserSettingsFile(new File(sourcePath, "settings.xml"));

        request.setPomFile(new File(sourcePath, "pom.xml"));
        request.setGoals(cmd);

        request.setBaseDirectory(new File(sourcePath));

        List<String> outList = new ArrayList<>();

        InvocationOutputHandler handler = outList::add;

        request.setOutputHandler(handler);
//        request.setErrorHandler(handler);

        Invoker invoker = new DefaultInvoker();
        //maven的位置
        invoker.setMavenHome(new File(mavenPath));

        if(execute.getExitCode() != 0){
             
        } else {

        }
}
上一篇下一篇

猜你喜欢

热点阅读