How to determine what artifacts are built from a m

2019-04-10 21:36发布

问题:

(Note: this question was originally posed by Dan Allen on Google+ here: https://plus.google.com/114112334290393746697/posts/G6BLNgjyqeQ)

If I ran 'mvn install', what artifacts would it install? Or, if I ran 'mvn deploy', what artifacts would it deploy?

This would likely be a fairly straightforward plugin to write, but I don't want to re-invent this if it's already available somewhere programmatically. It seems like this should be readily available somewhere.

回答1:

As Andrew Logvinov already commented, any maven plugin could attach additional artifacts. So I don't think this would be possible without actually building the project and executing all plugins bound to lifecycle phases upto package.

I don't know of any preexisting plugins doing this, the closes would probably be to run an actual deploy into a temporary directory and then list the contained files. To avoid modifying your local repository while doing this, you would want to avoid the install phase. The verify phase happens directly before install, the deploy mojo can then be invoked explicitly.

The deploy plugin allows to specify an alternative repository using a file url like this:

mvn verify deploy:deploy -DaltDeploymentRepository=snapshots::default::file:///home/jh/Temp/repository

The simplest implementation of a maven plugin listing all attached artifacts could look like this:

/**
 * @goal list-artifacts
 * @phase verify
 */
public class ListArtifactsMojo extends AbstractMojo {

    /**
     * @parameter default-value="${project}"
     * @required
     * @readonly
     */
    MavenProject project;

    public void execute() throws MojoExecutionException, MojoFailureException {
        Collection<Artifact> artifacts = new ArrayList<Artifact>();
        artifacts.add(project.getArtifact());
        artifacts.addAll(project.getAttachedArtifacts());

        for (Artifact artifact : artifacts) {
            System.out.println("Artifact: " + artifact);
        }
    }
}