2009-09-17 47 views
18

我有一個Maven插件,它在其配置文件中使用了groupId,artifactId和版本。如何在插件中下載Maven工件?

我希望能夠從遠程存儲庫下載該工件並將該文件複製到項目中。我無法弄清楚如何下載工件。

我明白我可以使用依賴插件來解決依賴關係,但我需要它在我的插件中發生。我怎樣才能做到這一點?

回答

24

您的插件需要使用ArtifactFactory以及要引導的artifact的groupId,artifactId和版本創建Artifact,然後將該Artifact傳遞給ArtifactResolver以處理髮現/下載。

解析器需要訪問本地存儲庫和遠程存儲庫。好消息是,所有這些都是plexus組件,您可以在您的Mojo中聲明爲依賴項,並讓Plexus爲您配置它們。

another answer我演示瞭如何做到這一點。在你的情況下,你需要一個具有略微不同參數的精簡版來讀取groupId,artifactId和version。在下面的插件中,各種組件被聲明爲plexus組件,以及聲明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> 
+0

哇,感謝。我會試試 – 2009-09-17 17:34:33

+0

再次感謝,它很好地工作。獲取下載文件的Maven項目的好方法是什麼? – 2009-09-17 18:02:26

+0

那真的是一個單獨的問題: -/ – 2009-09-17 18:04:44