Domanda

Ho un plugin Maven che accetta un groupId, artefactId e versione nella sua configurazione.

Voglio poter scaricare quell'artefatto dai repository remoti e copiare il file nel progetto. Non riesco a capire come scaricare l'artefatto però.

Comprendo che posso risolvere le dipendenze usando il plugin delle dipendenze, ma ho bisogno che accada all'interno del mio plugin. Come posso farlo?

È stato utile?

Soluzione

Il tuo plugin deve creare un Artefatto usando ArtifactFactory e il groupId, artefactId e la versione dell'artefatto da avviare, quindi passare quell'artefatto a un ArtefattoResolver per gestire il rilevamento / download.

Il resolver deve accedere al repository locale e ai repository remoti. La buona notizia è che tutti questi sono componenti del plesso che puoi dichiarare come dipendenze nel tuo Mojo e che Plexus li collega a te.

In un'altra risposta ho mostrato come fare . Nel tuo caso hai bisogno di una versione ridotta con parametri leggermente diversi per leggere groupId, artefactId e versione. Nel plugin seguente, i vari componenti sono dichiarati come componenti del plesso e le proprietà per dichiarare il groupId, artefactId, versione e tipo di packaging.

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

Questo è un esempio di un pom configurato per usare il plugin (e scaricare il 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>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top