Question

Dans une question précédente, j'ai obtenu un answer pour télécharger un artefact à partir du référentiel Maven. Cela fonctionne bien pour moi, mais je dois lire le MavenProject pour l'artefact téléchargé.

Quel est le meilleur moyen pour moi de lire le MavenProject de l'artefact téléchargé dans mon plugin?

Était-ce utile?

La solution

Vous pouvez utiliser MavenProjectBuilder pour résoudre l'artefact et lire le pom téléchargé dans un MavenProject. La méthode buildFromRepository () obtiendra l'artefact (si nécessaire) des référentiels distants, il n'est donc pas nécessaire de le télécharger avant la lecture.

Ce sont les modifications nécessaires à la réponse précédente qui résolvent le projet maven:

//other imports same as previous answer
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectBuilder;
import org.apache.maven.project.ProjectBuildingException;

/**
 * Obtain the artifact defined by the groupId, artifactId, and version from the
 * remote repository.
 * 
 * @goal bootstrap
 */
public class BootstrapAppMojo extends AbstractMojo {

    /**
     * Used to resolve the maven project.
     * 
     * @parameter expression=
     *            "${component.org.apache.maven.project.MavenProjectBuilder}"
     * @required
     * @readonly
     */
    private MavenProjectBuilder mavenProjectBuilder;

    //rest of properties same as before.

    /**
     * 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);

            MavenProject project = mavenProjectBuilder.buildFromRepository(
                pomArtifact, this.remoteRepositories, this.localRepository);

            //do something with the project...
        } catch (ProjectBuildingException e) {
            getLog().error("can't build bootstrapped pom", e);
        }
    }
}

Autres conseils

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top