2009-09-17 10 views
18

Tengo un plugin Maven que toma groupId, artifactId y versión en su confiugration.¿Cómo puedo descargar artefactos de Maven dentro de un complemento?

Quiero ser capaz de descargar ese artefacto de los repositorios remotos y copiar el archivo en el proyecto. Sin embargo, no puedo descifrar cómo descargar el artefacto.

Entiendo que puedo resolver dependencias usando el complemento de dependencia, pero necesito que ocurra dentro de mi complemento. ¿Cómo puedo hacer esto?

Respuesta

24

Su complemento necesita crear un artefacto usando ArtifactFactory y groupId, artifactId y versión del artefacto que se va a arrancar, luego pasar ese artefacto a ArtifactResolver para manejar el descubrimiento/descarga.

La resolución necesita acceso al repositorio local y a los repositorios remotos. La buena noticia es que todos esos son componentes del plexo que puede declarar como dependencias en su Mojo y hacer que Plexus los conecte por usted.

En another answer Mostré cómo hacer esto. En su caso, necesita una versión reducida con parámetros ligeramente diferentes para leer groupId, artifactId y version. En el complemento siguiente, los diversos componentes se declaran como componentes de plexo y las propiedades para declarar groupId, artifactId, versión y tipo de paquete.

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

Este es un ejemplo de un pom configurado para utilizar el plug-in (y descargar el pom aspectjrt 1.6.4):

<?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, gracias. Voy a intentarlo –

+0

gracias de nuevo, funciona muy bien. ¿Cuál es una buena manera de obtener un proyecto de maven para el archivo descargado? –

+0

Bueno, eso es realmente una pregunta separada: -/ –

Cuestiones relacionadas