use of org.apache.maven.artifact.resolver.filter.ArtifactFilter in project maven-plugins by apache.
the class PurgeLocalRepositoryMojo method getFilteredResolvedArtifacts.
private Set<Artifact> getFilteredResolvedArtifacts(MavenProject project, Set<Artifact> artifacts, TransformableFilter filter) {
try {
Iterable<ArtifactResult> results = dependencyResolver.resolveDependencies(session.getProjectBuildingRequest(), project.getModel(), filter);
Set<Artifact> resolvedArtifacts = new LinkedHashSet<Artifact>();
for (ArtifactResult artResult : results) {
resolvedArtifacts.add(artResult.getArtifact());
}
return resolvedArtifacts;
} catch (DependencyResolverException e) {
getLog().info("Unable to resolve all dependencies for : " + project.getGroupId() + ":" + project.getArtifactId() + ":" + project.getVersion() + ". Falling back to non-transitive mode for initial artifact resolution.");
}
Set<Artifact> resolvedArtifacts = new LinkedHashSet<Artifact>();
ArtifactFilter artifactFilter = filter.transform(new ArtifactIncludeFilterTransformer());
// purge anyway
for (Artifact artifact : artifacts) {
if (artifactFilter.include(artifact)) {
try {
ArtifactResult artifactResult = artifactResolver.resolveArtifact(session.getProjectBuildingRequest(), artifact);
resolvedArtifacts.add(artifactResult.getArtifact());
} catch (ArtifactResolverException e) {
getLog().debug("Unable to resolve artifact: " + artifact);
}
}
}
return resolvedArtifacts;
}
use of org.apache.maven.artifact.resolver.filter.ArtifactFilter in project felix by apache.
the class AbstractDependencyFilter method filteredDependencies.
private void filteredDependencies(final ArtifactFilter artifactFilter, Collection<Artifact> filteredDependencies) {
CollectingDependencyNodeVisitor collectingDependencyNodeVisitor = new CollectingDependencyNodeVisitor();
final Artifact rootArtifact = m_dependencyGraph.getArtifact();
ArtifactFilter filter = new ArtifactFilter() {
public boolean include(Artifact artifact) {
return artifact.equals(rootArtifact) || artifactFilter.include(artifact);
}
};
DependencyNodeFilter dependencyNodeFilter = new ArtifactDependencyNodeFilter(filter);
dependencyNodeFilter = new TrimmingDependencyNodeFilter(dependencyNodeFilter);
DependencyNodeVisitor dependencyNodeVisitor = new FilteringDependencyNodeVisitor(collectingDependencyNodeVisitor, dependencyNodeFilter);
dependencyNodeVisitor = new BuildingDependencyNodeVisitor(dependencyNodeVisitor);
m_dependencyGraph.accept(dependencyNodeVisitor);
List<DependencyNode> dependencyNodes = collectingDependencyNodeVisitor.getNodes();
Set<String> ids = new LinkedHashSet<String>(dependencyNodes.size());
for (DependencyNode dependencyNode : dependencyNodes) {
Artifact artifact = dependencyNode.getArtifact();
String id = artifact.getId();
ids.add(id);
}
for (Iterator<Artifact> iterator = filteredDependencies.iterator(); iterator.hasNext(); ) {
Artifact artifact = iterator.next();
String id = artifact.getId();
if (!ids.contains(id)) {
iterator.remove();
}
}
}
use of org.apache.maven.artifact.resolver.filter.ArtifactFilter in project felix by apache.
the class AbstractDependencyFilter method processInstructions.
protected final void processInstructions(String header) throws MojoExecutionException {
Map<String, Attrs> instructions = OSGiHeader.parseHeader(MISSING_KEY_PATTERN.matcher(header).replaceAll("$1$2*;$3"));
Collection<Artifact> availableDependencies = new LinkedHashSet<Artifact>(m_dependencyArtifacts);
for (Iterator<Map.Entry<String, Attrs>> clauseIterator = instructions.entrySet().iterator(); clauseIterator.hasNext(); ) {
String inline = "false";
// always start with a fresh *modifiable* collection for each unique clause
Collection<Artifact> filteredDependencies = new LinkedHashSet<Artifact>(availableDependencies);
// CLAUSE: REGEXP --> { ATTRIBUTE MAP }
Map.Entry<String, Attrs> clause = clauseIterator.next();
String primaryKey = clause.getKey().replaceFirst("~+$", "");
boolean isNegative = primaryKey.startsWith("!");
if (isNegative) {
primaryKey = primaryKey.substring(1);
}
final AndArtifactFilter andArtifactFilter = new AndArtifactFilter();
if (!"*".equals(primaryKey)) {
ArtifactFilter filter = new DependencyFilter(primaryKey) {
@Override
public boolean include(Artifact dependency) {
return super.matches(dependency.getArtifactId());
}
};
// FILTER ON MAIN CLAUSE
andArtifactFilter.add(filter);
}
for (Iterator<Map.Entry<String, String>> attrIterator = clause.getValue().entrySet().iterator(); attrIterator.hasNext(); ) {
final ArtifactFilter filter;
// ATTRIBUTE: KEY --> REGEXP
Map.Entry<String, String> attr = attrIterator.next();
if ("groupId".equals(attr.getKey())) {
filter = new DependencyFilter(attr.getValue()) {
@Override
public boolean include(Artifact dependency) {
return super.matches(dependency.getGroupId());
}
};
} else if ("artifactId".equals(attr.getKey())) {
filter = new DependencyFilter(attr.getValue()) {
@Override
public boolean include(Artifact dependency) {
return super.matches(dependency.getArtifactId());
}
};
} else if ("version".equals(attr.getKey())) {
filter = new DependencyFilter(attr.getValue()) {
@Override
public boolean include(Artifact dependency) {
try {
// use the symbolic version if available (ie. 1.0.0-SNAPSHOT)
return super.matches(dependency.getSelectedVersion().toString());
} catch (Exception e) {
return super.matches(dependency.getVersion());
}
}
};
} else if ("scope".equals(attr.getKey())) {
filter = new DependencyFilter(attr.getValue(), "compile") {
@Override
public boolean include(Artifact dependency) {
return super.matches(dependency.getScope());
}
};
} else if ("type".equals(attr.getKey())) {
filter = new DependencyFilter(attr.getValue(), "jar") {
@Override
public boolean include(Artifact dependency) {
return super.matches(dependency.getType());
}
};
} else if ("classifier".equals(attr.getKey())) {
filter = new DependencyFilter(attr.getValue()) {
@Override
public boolean include(Artifact dependency) {
return super.matches(dependency.getClassifier());
}
};
} else if ("optional".equals(attr.getKey())) {
filter = new DependencyFilter(attr.getValue(), "false") {
@Override
public boolean include(Artifact dependency) {
return super.matches("" + dependency.isOptional());
}
};
} else if ("inline".equals(attr.getKey())) {
inline = attr.getValue();
continue;
} else {
throw new MojoExecutionException("Unexpected attribute " + attr.getKey());
}
// FILTER ON EACH ATTRIBUTE
andArtifactFilter.add(filter);
}
filteredDependencies(andArtifactFilter, filteredDependencies);
if (isNegative) {
// negative clauses reduce the set of available artifacts
availableDependencies.removeAll(filteredDependencies);
if (!clauseIterator.hasNext()) {
// assume there's an implicit * missing at the end
processDependencies(availableDependencies, inline);
}
} else {
// positive clause; doesn't alter the available artifacts
processDependencies(filteredDependencies, inline);
}
}
}
use of org.apache.maven.artifact.resolver.filter.ArtifactFilter in project maven-dependency-plugin by apache.
the class TreeMojo method createResolvingArtifactFilter.
// private methods --------------------------------------------------------
/**
* Gets the artifact filter to use when resolving the dependency tree.
*
* @return the artifact filter
*/
private ArtifactFilter createResolvingArtifactFilter() {
ArtifactFilter filter;
// filter scope
if (scope != null) {
getLog().debug("+ Resolving dependency tree for scope '" + scope + "'");
filter = new ScopeArtifactFilter(scope);
} else {
filter = null;
}
return filter;
}
use of org.apache.maven.artifact.resolver.filter.ArtifactFilter in project maven-dependency-plugin by apache.
the class PurgeLocalRepositoryMojo method purgeLocalRepository.
/**
* Purges the local repository for the dependencies in the given Maven project.
*
* @param theProject Maven project.
* @param resolvedArtifactsToPurge The artifacts that were already purged.
* @throws MojoFailureException in case of errors during the purge.
*/
private void purgeLocalRepository(MavenProject theProject, Set<Artifact> purgedArtifacts) throws MojoFailureException {
List<Dependency> dependencies = theProject.getDependencies();
TransformableFilter dependencyFilter = createPurgeArtifactsFilter(theProject, dependencies, purgedArtifacts);
Set<Artifact> resolvedArtifactsToPurge = getFilteredResolvedArtifacts(theProject, dependencies, dependencyFilter);
if (resolvedArtifactsToPurge.isEmpty()) {
getLog().info("No artifacts included for purge for project: " + theProject.getId());
return;
}
verbose("Purging dependencies for project: " + theProject.getId());
purgeArtifacts(resolvedArtifactsToPurge);
purgedArtifacts.addAll(resolvedArtifactsToPurge);
if (reResolve) {
ArtifactFilter artifactFilter = dependencyFilter.transform(new ArtifactIncludeFilterTransformer());
try {
reResolveArtifacts(theProject, resolvedArtifactsToPurge, artifactFilter);
} catch (ArtifactResolutionException e) {
String failureMessage = "Failed to refresh project dependencies for: " + theProject.getId();
MojoFailureException failure = new MojoFailureException(failureMessage);
failure.initCause(e);
throw failure;
} catch (ArtifactNotFoundException e) {
String failureMessage = "Failed to refresh project dependencies for: " + theProject.getId();
MojoFailureException failure = new MojoFailureException(failureMessage);
failure.initCause(e);
throw failure;
}
}
}
Aggregations