如何给出groupId,artifactId,version等以编程方式构建MavenProject实例(不是当前项目)?
更新:我正在尝试为http://jira.codehaus.org/browse/MDEP-322创建补丁。我相信maven-dependency-plugin依赖于Maven 2.x所以我不能使用任何Maven 3.x API。
答案 0 :(得分:1)
如何执行此操作取决于您是要从本地存储库中的工件还是从硬盘驱动器上的pom文件构建项目。无论哪种方式,你都需要得到一个ProjectBuilder
,你可以在Mojo中做到这一点:
/** @component role = "org.apache.maven.project.ProjectBuilder" */
protected ProjectBuilder m_projectBuilder;
如果您想从本地仓库中的工件构建,您还需要:
/** @parameter expression="${localRepository}" */
protected ArtifactRepository m_localRepository;
完成后,您可以从本地存储库中的工件构造MavenProject
:
//Construct the artifact representation
Artifact artifact =
new DefaultArtifact(groupId,artifactId,version,scope,type,classifier,new DefaultArtifactHandler());
//Resolve it against the local repository
artifact = m_localRepository.find(artifact);
//Create a project building request
ProjectBuildingRequest request = new DefaultProjectBuildingRequest();
//Build the project and get the result
MavenProject project = m_projectBuilder.build(artifact,request).getProject();
或者来自pom文件:
File pomFile = new File("path/to/pom.xml");
ProjectBuildingRequest request = new DefaultProjectBuildingRequest();
MavenProject project = m_projectBuilder.build(pomFile,request).getProject();