在先前的問題,我從Maven倉庫下載神器有一個answer。這適用於我,但我需要閱讀下載的工件的MavenProject。如何從插件中的Maven存儲庫中解析工件?
什麼是我讀MavenProject在我的插件下載神器的最佳方式?
在先前的問題,我從Maven倉庫下載神器有一個answer。這適用於我,但我需要閱讀下載的工件的MavenProject。如何從插件中的Maven存儲庫中解析工件?
什麼是我讀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);
}
}
}
+1好參考 – 2009-09-18 13:25:26