
com.atlassian.maven.plugins.DependencyInspectingMojo Maven / Gradle / Ivy
The newest version!
/*
* Copyright © 2022 Atlassian Pty Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.atlassian.maven.plugins;
import org.apache.maven.artifact.Artifact;
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.ContextEnabled;
import org.apache.maven.plugin.Mojo;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectHelper;
import java.util.*;
import java.util.stream.Collectors;
public abstract class DependencyInspectingMojo implements Mojo, ContextEnabled {
protected Log log;
@Component
protected ArtifactResolver artifactResolver;
/**
* Artifacts which are not included in the archive or list.
*/
@Parameter(defaultValue = "")
private String excludeArtifactIds;
/**
* The scope to consider when calculating the set of dependencies to archive.
*/
@Parameter(defaultValue = "runtime")
private String includeScope;
/**
* The local Maven repository.
*/
@Parameter(defaultValue = "${localRepository}", required = true, readonly = true)
protected ArtifactRepository localRepository;
private Map pluginContext;
@Parameter(defaultValue = "${project}", readonly = true, required = true)
protected MavenProject project;
/**
* The Maven project helper.
*/
@Component
protected MavenProjectHelper projectHelper;
/**
* The remote Maven repositories.
*/
@Parameter(defaultValue = "${project.remoteArtifactRepositories}")
protected List remoteRepositories;
public DependencyInspectingMojo() {
//used by Maven and Plexus
}
DependencyInspectingMojo(ArtifactResolver artifactResolver, String excludeArtifactIds, String includeScope,
ArtifactRepository localRepository, MavenProject project, MavenProjectHelper projectHelper,
List remoteRepositories) {
//used by tests
this.artifactResolver = artifactResolver;
this.excludeArtifactIds = excludeArtifactIds;
this.includeScope = includeScope;
this.localRepository = localRepository;
this.project = project;
this.projectHelper = projectHelper;
this.remoteRepositories = remoteRepositories;
}
@Override
public Log getLog() {
return log;
}
@Override
public void setLog(Log log) {
this.log = log;
}
@Override
public Map getPluginContext() {
return pluginContext;
}
@Override
public void setPluginContext(Map value) {
pluginContext = value;
}
/**
* Creates the archive containing the artifacts of this project's dependencies.
* Call from superclass and put your code in closure.
*/
List resolveAllArtifacts() {
Set exclusions = new HashSet<>(Arrays.asList(excludeArtifactIds == null ? new String[0] : excludeArtifactIds.split(",")));
for (String exl : exclusions) {
log.debug("Excluding artifacts with artifact id " + exl);
}
List allArtifacts = project.getDependencyArtifacts().stream().filter(it ->
(it.getScope().equals(includeScope)) && !exclusions.contains(it.getArtifactId())).collect(Collectors.toList());
log.debug("Considering all dependencies with '" + includeScope + "' scope");
// make sure the artifacts are resolved
allArtifacts.forEach(artifact -> {
if (artifact.getFile() == null) {
//must resolve, set the resolved flag to false to force resolution
artifact.setResolved(false);
try {
artifactResolver.resolve(artifact, remoteRepositories, localRepository);
} catch (ArtifactResolutionException | ArtifactNotFoundException e) {
throw new RuntimeException(e);
}
}
});
return allArtifacts;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy