2009-09-17 10 views
18

Mam wtyczkę Maven, która przyjmuje identyfikator groupId, artifactId i wersję w konfigurze.Jak mogę pobierać artefakty Maven z wtyczki?

Chcę móc pobrać ten artefakt ze zdalnych repozytoriów i skopiować plik do projektu. Nie mogę jednak dowiedzieć się, jak pobrać artefakt.

Rozumiem, że mogę rozwiązać zależności za pomocą wtyczki zależności, ale muszę to zrobić w mojej wtyczce. Jak mogę to zrobić?

Odpowiedz

24

Twoja wtyczka musi utworzyć artefakt za pomocą ArtifactFactory i groupId, artifactId i wersji artefaktu, które mają być bootstrapped, a następnie przekazać ten artefakt do ArtifactResolver, aby obsłużyć wykrywanie/pobieranie.

Konwerter wymaga dostępu do lokalnego repozytorium i zdalnych repozytoriów. Dobrą wiadomością jest to, że wszystkie te elementy są elementami splotu, które możesz zadeklarować jako zależności w swoim Mojo i mają splot Plexus dla ciebie.

W another answer pokazałem, jak to zrobić. W twoim przypadku potrzebujesz wersji cut-down z nieco innymi parametrami, aby odczytać groupId, artifactId i wersję. W poniższej wtyczce różne komponenty są deklarowane jako komponenty splotów, a właściwości deklarują identyfikator groupId, artifactId, wersję i typ opakowania.

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); 
     } 
    } 
} 

Jest przykładem pom skonfigurowany do korzystania z wtyczki (i pobrać 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

wow, dzięki. Dam to spróbować –

+0

dzięki jeszcze raz, to działa ładnie. Jaki jest dobry sposób na uzyskanie projektu dla pobranego pliku? –

+0

cóż, to naprawdę osobne pytanie: -/ –

Powiązane problemy