栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在插件中下载Maven工件?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在插件中下载Maven工件?

您的插件需要使用ArtifactFactory以及要引导的工件的groupId,artifactId和版本创建一个Artifact,然后将该工件传递给ArtifactResolver来处理发现/下载。

解析程序需要访问本地存储库和远程存储库。好消息是所有这些都是plexus组件,您可以在Mojo中将它们声明为依赖项,并让Plexus为您连接它们。

在另一个答案中,我展示了如何执行此操作。在您的情况下,您需要一个参数略有不同的简化版本,以读取groupId,artifactId和版本。在下面的插件中,各种组件都声明为丛组件,而属性则声明为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;public class BootstrapAppMojo extends AbstractMojo {        protected ArtifactFactory factory;        protected ArtifactResolver artifactResolver;        protected List remoteRepositories;        protected ArtifactRepository localRepository;        private String bootstrapArtifactId;        private String bootstrapGroupId;        private String bootstrapType;        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);        }    }}

这是配置为使用插件的pom的示例(并下载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>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/508318.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号