java 仅从 POM.xml - pom 解析器获取 MavenProject?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4381460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get MavenProject from just the POM.xml - pom parser?
提问by javamonkey79
Is it possible to get an instance of org.apache.maven.project.MavenProject
or some other object form of the POM from just the pom.xml file?
是否可以org.apache.maven.project.MavenProject
仅从 pom.xml 文件中获取 POM的实例或其他一些对象形式?
Thanks in advance.
提前致谢。
回答by rxx
yes you can . This is the code. You need maven-model-3.0.4.jar and plexus-utils-2.0.6.jar and maven-core-3.0.4.jar
是的你可以 。这是代码。你需要 maven-model-3.0.4.jar 和 plexus-utils-2.0.6.jar 和 maven-core-3.0.4.jar
Model model = null;
FileReader reader = null;
MavenXpp3Reader mavenreader = new MavenXpp3Reader();
try {
reader = new FileReader(pomfile);
model = mavenreader.read(reader);
model.setPomFile(pomfile);
}catch(Exception ex){}
MavenProject project = new MavenProject(model);
回答by Robert Reiz
If you want to get the MavenProject based on groupid, artifactId and version you have to run inside of a maven plugin. And this code will do the job:
如果您想根据 groupid、artifactId 和 version 获取 MavenProject,您必须在 Maven 插件中运行。这段代码将完成这项工作:
ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
configuration.setProcessPlugins( false );
configuration.setRepositorySession( session );
org.apache.maven.artifact.Artifact artifact = new org.apache.maven.artifact.DefaultArtifact(groupId, artifactId, version, "compile", "", "", new DefaultArtifactHandler());
MavenProject project = projectBuilder.build(artifact, configuration).getProject();
The missing properties can be injected into the maven plugin.
缺失的属性可以注入到 maven 插件中。