[英]Calling Maven goals from Java
Is it possible to call Maven goals from Java, for instance, can I do the equivalent of: 是否可以从Java调用Maven目标,例如,我是否可以执行以下操作:
mvn clean package
from a Java class? 来自Java类?
thanks, Nick 谢谢,尼克
absolutely, you need to use the Maven Embedder API . 绝对,您需要使用Maven Embedder API 。
Updated link is http://maven.apache.org/ref/3-LATEST/maven-embedder/index.html 更新的链接是http://maven.apache.org/ref/3-LATEST/maven-embedder/index.html
This is easy :) 这很简单 :)
Java code Java代码
MavenCli cli = new MavenCli();
cli.doMain(new String[]{"clean", "package"}, "project_dir", System.out, System.out);
Project configuration: 项目配置:
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-embedder</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-connector-wagon</artifactId>
<version>0.9.0.M2</version>
</dependency>
<dependency>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-http-lightweight</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
Fully working example: https://github.com/mariuszs/maven-cli-example 完整的工作示例: https : //github.com/mariuszs/maven-cli-example
I would not recommend to use MAVEN CLI
because it works differently comparing to command line alternative. 我不建议使用
MAVEN CLI
因为它与命令行替代方法的工作方式不同。 For example: 例如:
With CLI, I want to reproduce "mvn dependency:resolve validate"
使用CLI,我想重现
"mvn dependency:resolve validate"
cli.doMain(
new String[]{
"dependency:resolve", // download deps if needed
"validate"}, // just validates, no need even to compile
projectPath ...
But actually it will go all over the folders (recursively) and will try to validate all projects there even if I don't want it. 但实际上它将遍布文件夹(递归)并将尝试验证那里的所有项目,即使我不想要它。 If if something wrong - it fails .
如果出现问题 - 它会失败 。
If, though, you try to do the same just with command line - it will invoke only pom.xm and will finish successfully (even if some project inside 'projectPath' is not resolvable. 但是,如果您尝试仅使用命令行执行相同操作 - 它将仅调用pom.xm并将成功完成 (即使'projectPath'中的某些项目无法解析。
With CLI
I could NOT manage to use "-f " flag to specify particular pom.xml
! 使用
CLI
我无法使用“-f”标志来指定特定的pom.xml
!
This is quite good for me: 这对我来说非常好:
private int resolveAsCommandLine() {
try {
String command = "mvn " +
"-f " + projectPath + "\\pom.xml " +
"-Dmaven.repo.local=" + localRepoPath + "\\repository " +
"-Dmaven.test.skip=true " + // ignore tests
"dependency:resolve " + // download deps if needed
"validate";
System.out.println("%> Executing command: '" + command + "'...");
Process p = Runtime.getRuntime().exec( // "cmd - for windows only"
"cmd /c " + command
);
BufferedReader in = new BufferedReader(
new InputStreamReader(p.getInputStream()) );
String line = "";
while ((line = in.readLine()) != null) {
System.out.println(line);
if(line.contains("[ERROR]")) return ERROR_STATUS;
if(line.contains("BUILD FAILURE")) return ERROR_STATUS;
if(line.contains("BUILD SUCCESS")) return OK_STATUS;
}
in.close();
} catch (IOException e) {
e.printStackTrace();
return ERROR_STATUS;
}
return ERROR_STATUS;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.