从POM.xml获取MavenProject - pom解析器?

时间:2010-12-07 20:53:12

标签: java maven pom.xml

是否可以从pom.xml文件中获取org.apache.maven.project.MavenProject的实例或POM的其他对象形式?

提前致谢。

2 个答案:

答案 0 :(得分:29)

是的,你可以。这是代码。你需要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);

答案 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插件。