我有一个Maven插件,在其confiugration需要的groupId,artifactId和版本。
我希望能够从远程仓库下载神器然后将该文件复制到项目中。 我无法弄清楚如何下载虽然神器。
我知道我可以解决您在使用的依赖插件的依赖关系,但我需要我的插件内发生。 我怎样才能做到这一点?
我有一个Maven插件,在其confiugration需要的groupId,artifactId和版本。
我希望能够从远程仓库下载神器然后将该文件复制到项目中。 我无法弄清楚如何下载虽然神器。
我知道我可以解决您在使用的依赖插件的依赖关系,但我需要我的插件内发生。 我怎样才能做到这一点?
你的插件需要创建使用ArtifactFactory和的groupId,artifactId的和人为的版本中自举的神器,那神器传递到ArtifactResolver处理发现/下载。
解析器需要访问本地存储库和远程存储库。 好消息是,所有这些都是你可以声明在你的Mojo的依赖,并丛它们连接为您丛组件。
在另一个答案我展示了如何做到这一点。 在你的情况,你需要稍微不同参数的简化版本读取的groupId,artifactId和version。 在下面的插件,各种部件被声明为丛部件,并申报的groupId,artifactId的,版本和包装类型的属性。
package name.seller.rich.maven.plugins.bootstrap;
import java.util.List;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
import org.apache.maven.artifact.resolver.ArtifactResolver;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
/**
* Obtain the artifact defined by the groupId, artifactId, and version
* from the remote repository.
*
* @goal bootstrap
*/
public class BootstrapAppMojo extends AbstractMojo {
/**
* Used to look up Artifacts in the remote repository.
*
* @parameter expression=
* "${component.org.apache.maven.artifact.factory.ArtifactFactory}"
* @required
* @readonly
*/
protected ArtifactFactory factory;
/**
* Used to look up Artifacts in the remote repository.
*
* @parameter expression=
* "${component.org.apache.maven.artifact.resolver.ArtifactResolver}"
* @required
* @readonly
*/
protected ArtifactResolver artifactResolver;
/**
* List of Remote Repositories used by the resolver
*
* @parameter expression="${project.remoteArtifactRepositories}"
* @readonly
* @required
*/
protected List remoteRepositories;
/**
* Location of the local repository.
*
* @parameter expression="${localRepository}"
* @readonly
* @required
*/
protected ArtifactRepository localRepository;
/**
* The target pom's artifactId
*
* @parameter expression="${bootstrapArtifactId}"
* @required
*/
private String bootstrapArtifactId;
/**
* The target pom's groupId
*
* @parameter expression="${bootstrapGroupId}"
* @required
*/
private String bootstrapGroupId;
/**
* The target pom's type
*
* @parameter expression="${bootstrapType}"
* @required
*/
private String bootstrapType;
/**
* 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);
artifactResolver.resolve(pomArtifact, this.remoteRepositories,
this.localRepository);
} catch (ArtifactResolutionException e) {
getLog().error("can't resolve parent pom", e);
} catch (ArtifactNotFoundException e) {
getLog().error("can't resolve parent pom", e);
}
}
}
这是被配置为使用该插件(并下载aspectjrt 1.6.4 POM)聚甲醛的例子:
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>name.seller.rich</groupId>
<artifactId>bootstrap-test</artifactId>
<version>1.0.0</version>
<build>
<plugins>
<plugin>
<groupId>name.seller.rich</groupId>
<artifactId>maven-bootstrap-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>bootstrap</goal>
</goals>
<configuration>
<bootstrapGroupId>org.aspectj</bootstrapGroupId>
<bootstrapArtifactId>aspectjrt</bootstrapArtifactId>
<bootstrapVersion>1.6.4</bootstrapVersion>
<bootstrapType>pom</bootstrapType>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>