在上一个问题中,我得到了 answer用于从 Maven 存储库下载工件。这对我来说效果很好,但我需要阅读 MavenProject 以获取下载的工件。
在我的插件中读取下载工件的 MavenProject 的最佳方式是什么?
最佳答案
您可以使用 MavenProjectBuilder 解析工件并将下载的 pom 读入 MavenProject。 buildFromRepository() 方法将从远程存储库获取工件(如果需要),因此在阅读之前无需下载它。
这些是先前答案解决 Maven 项目所需的更改:
//other imports same as previous answer
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectBuilder;
import org.apache.maven.project.ProjectBuildingException;
/**
* Obtain the artifact defined by the groupId, artifactId, and version from the
* remote repository.
*
* @goal bootstrap
*/
public class BootstrapAppMojo extends AbstractMojo {
/**
* Used to resolve the maven project.
*
* @parameter expression=
* "${component.org.apache.maven.project.MavenProjectBuilder}"
* @required
* @readonly
*/
private MavenProjectBuilder mavenProjectBuilder;
//rest of properties same as before.
/**
* The target pom's version
*
* @parameter expression="${bootstrapVersion}"
* @required
*/
private String bootstrapVersion;
public void execute() throws MojoExecutionException, MojoFailureException {
try {
Artifact pomArtifact = this.factory.createArtifact(
bootstrapGroupId, bootstrapArtifactId, bootstrapVersion,
"", bootstrapType);
MavenProject project = mavenProjectBuilder.buildFromRepository(
pomArtifact, this.remoteRepositories, this.localRepository);
//do something with the project...
} catch (ProjectBuildingException e) {
getLog().error("can't build bootstrapped pom", e);
}
}
}
关于java - 如何在插件中解析 Maven 存储库中的工件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1440486/