是否可以从pom.xml文件中获取org.apache.maven.project.MavenProject
的实例或POM的其他对象形式?
提前致谢。
答案 0 :(得分:29)
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);
答案 1 :(得分:2)
如果你想基于groupid,artifactId和版本获得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();
可以将缺少的属性注入maven插件。