List of usage examples for org.apache.maven.artifact Artifact getDependencyFilter
ArtifactFilter getDependencyFilter();
From source file:com.github.zhve.ideaplugin.ArtifactDependencyResolver.java
License:Apache License
private static Artifact toDependencyArtifact(ArtifactFactory artifactFactory, Artifact dependency, String inheritedScope) {/*from w w w .j av a2s . c om*/ Artifact dependencyArtifact = artifactFactory.createDependencyArtifact(dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersionRange(), dependency.getType(), dependency.getClassifier(), dependency.getScope(), inheritedScope, dependency.isOptional()); if (dependencyArtifact != null) dependencyArtifact.setDependencyFilter(dependency.getDependencyFilter()); return dependencyArtifact; }
From source file:org.codehaus.mojo.natives.plugin.NativeLinkMojo.java
License:Open Source License
/** * /*from ww w. j a v a 2 s .c om*/ */ private void attachPrimaryArtifact() { Artifact artifact = this.project.getArtifact(); if (null == this.classifier) { artifact.setFile(new File(this.linkerOutputDirectory + "/" + this.project.getBuild().getFinalName() + "." + this.project.getArtifact().getArtifactHandler().getExtension())); } else { //install primary artifact as a classifier DefaultArtifact clone = new DefaultArtifact(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersionRange().cloneOf(), artifact.getScope(), artifact.getType(), classifier, artifact.getArtifactHandler(), artifact.isOptional()); clone.setRelease(artifact.isRelease()); clone.setResolvedVersion(artifact.getVersion()); clone.setResolved(artifact.isResolved()); clone.setFile(artifact.getFile()); if (artifact.getAvailableVersions() != null) { clone.setAvailableVersions(new ArrayList(artifact.getAvailableVersions())); } clone.setBaseVersion(artifact.getBaseVersion()); clone.setDependencyFilter(artifact.getDependencyFilter()); if (artifact.getDependencyTrail() != null) { clone.setDependencyTrail(new ArrayList(artifact.getDependencyTrail())); } clone.setDownloadUrl(artifact.getDownloadUrl()); clone.setRepository(artifact.getRepository()); clone.setFile(new File(this.linkerOutputDirectory + "/" + this.project.getBuild().getFinalName() + "." + this.project.getArtifact().getArtifactHandler().getExtension())); project.setArtifact(clone); } }
From source file:org.codehaus.mojo.pomtools.helpers.MetadataHelper.java
License:Apache License
/** Resolves all transitive dependencies for the current project and returns a list * of {@link TransitiveDependencyInfo} objects. Each object represents a distinct * groupId:artifactId:type dependency. The {@link TransitiveDependencyInfo#getResolutionNodes()} * represent all of the possible ResolutionNodes which resolve to this groupId:artifactId. * /*from w w w .j a v a 2s .co m*/ * @return * @throws PomToolsException */ public List getTransitiveDependencies() throws PomToolsException, ProjectBuildingException { // Certain things like groupId or versions for dependencies may be declared in a parent // pom so we need to have maven fully resolve the model before walking the tree. MavenProject project = PomToolsPluginContext.getInstance().getActiveProject().getTemporaryResolvedProject(); try { project.setDependencyArtifacts(project.createArtifacts(artifactFactory, null, null)); } catch (InvalidDependencyVersionException e) { throw new PomToolsVersionException( "Unable to build project due to an invalid dependency version: " + e.getMessage(), e); } Artifact projectArtifact = project.getArtifact(); Set artifacts = project.getDependencyArtifacts(); try { List dependencies = new ArrayList(); ArtifactResolutionResult result; result = artifactResolver.resolveTransitively(artifacts, projectArtifact, Collections.EMPTY_MAP, localRepository, remoteRepositories, metadataSource, projectArtifact.getDependencyFilter()); Map dependencyMap = new HashMap(); Set seen = new HashSet(); // First build our map of distinct groupId:artifactIds for (Iterator iter = result.getArtifactResolutionNodes().iterator(); iter.hasNext();) { ResolutionNode node = (ResolutionNode) iter.next(); TransitiveDependencyInfo info = new TransitiveDependencyInfo(node); dependencyMap.put(info.getKey(), info); dependencies.add(info); } // Now populate the map with all children recurseNode(dependencyMap, seen, result.getArtifactResolutionNodes().iterator(), 0); return dependencies; } catch (ArtifactNotFoundException e) { throw new PomToolsException(e); } catch (ArtifactResolutionException e) { throw new PomToolsException(e); } }
From source file:org.universAAL.maven.treebuilder.DependencyTreeBuilder.java
License:Apache License
/** * Method resolves provided node with the use of provided * ArtifactMetadataSource and taking into account ManagedVersionMap. Output * is passed to listeners, passed as argument, which are notified about all * events related to dependencies detected in the tree. * //from ww w . j a v a 2 s .c o m * @param parentNode * Parent node * @param child * Child node * @param filter * Filter for filtering artifacts for the resolving process. * @param managedVersions * Map of managed versions. * @param listener * Listener to be notified about events related to resolution * process. * @param source * ArtifactMetadataSource object passed by maven. * @param parentArtifact * Parent artifact * @return returns true if the child should be recursively resolved. * @throws OverConstrainedVersionException * Occurs when ranges exclude each other and no valid value * remains. * @throws ArtifactMetadataRetrievalException * Error while retrieving repository metadata from the * repository */ private boolean resolveChildNode(final ResolutionNode parentNode, final ResolutionNode child, final ArtifactFilter filter, final ManagedVersionMap managedVersions, final DependencyTreeResolutionListener listener, final ArtifactMetadataSource source, final Artifact parentArtifact) throws OverConstrainedVersionException, ArtifactMetadataRetrievalException { // We leave in optional ones, but don't pick up its dependencies if (!child.isResolved() && (!child.getArtifact().isOptional() || child.isChildOfRootNode())) { Artifact artifact = child.getArtifact(); artifact.setDependencyTrail(parentNode.getDependencyTrail()); List childRemoteRepositories = child.getRemoteRepositories(); try { Object childKey; do { childKey = child.getKey(); if (managedVersions.containsKey(childKey)) { // If this child node is a managed dependency, // ensure // we are using the dependency management // version // of this child if applicable b/c we want to // use the // managed version's POM, *not* any other // version's POM. // We retrieve the POM below in the retrieval // step. manageArtifact(child, managedVersions); // Also, we need to ensure that any exclusions // it presents are // added to the artifact before we retrieve the // metadata // for the artifact; otherwise we may end up // with unwanted // dependencies. Artifact ma = (Artifact) managedVersions.get(childKey); ArtifactFilter managedExclusionFilter = ma.getDependencyFilter(); if (null != managedExclusionFilter) { if (null != artifact.getDependencyFilter()) { AndArtifactFilter aaf = new AndArtifactFilter(); aaf.add(artifact.getDependencyFilter()); aaf.add(managedExclusionFilter); artifact.setDependencyFilter(aaf); } else { artifact.setDependencyFilter(managedExclusionFilter); } } } if (artifact.getVersion() == null) { // set the recommended version // TODO: maybe its better to just pass the range // through to retrieval and use a // transformation? ArtifactVersion version; if (artifact.isSelectedVersionKnown()) { version = artifact.getSelectedVersion(); } else { // go find the version List versions = artifact.getAvailableVersions(); if (versions == null) { versions = source.retrieveAvailableVersions(artifact, localRepository, childRemoteRepositories); artifact.setAvailableVersions(versions); } Collections.sort(versions); VersionRange versionRange = artifact.getVersionRange(); version = versionRange.matchVersion(versions); if (version == null) { if (versions.isEmpty()) { throw new OverConstrainedVersionException( "No versions are present in the repository for the artifact with a range " + versionRange, artifact, childRemoteRepositories); } throw new OverConstrainedVersionException("Couldn't find a version in " + versions + " to match range " + versionRange, artifact, childRemoteRepositories); } } // this is dangerous because // artifact.getSelectedVersion() can // return null. However it is ok here because we // first check if the // selected version is known. As currently coded // we can't get a null here. artifact.selectVersion(version.toString()); fireEvent(ResolutionListener.SELECT_VERSION_FROM_RANGE, listener, child); } // rotgier: it is not compatible with maven 3 // Artifact relocated = source.retrieveRelocatedArtifact( // artifact, localRepository, childRemoteRepositories); // if (relocated != null && !artifact.equals(relocated)) { // relocated.setDependencyFilter(artifact // .getDependencyFilter()); // artifact = relocated; // child.setArtifact(artifact); // } } while (!childKey.equals(child.getKey())); if (parentArtifact != null && parentArtifact.getDependencyFilter() != null && !parentArtifact.getDependencyFilter().include(artifact)) { // MNG-3769: the [probably relocated] artifact is // excluded. // We could process exclusions on relocated artifact // details in the // MavenMetadataSource.createArtifacts(..) step, BUT // that would // require resolving the POM from the repository // very early on in // the build. return true; } ResolutionGroup rGroup = source.retrieve(artifact, localRepository, childRemoteRepositories); // TODO might be better to have source.retrieve() throw // a specific exception for this situation // and catch here rather than have it return null if (rGroup == null) { // relocated dependency artifact is declared // excluded, no need to add and recurse further return true; } child.addDependencies(rGroup.getArtifacts(), rGroup.getResolutionRepositories(), filter); } catch (CyclicDependencyException e) { // would like to throw this, but we have crappy stuff in // the repo fireEvent(ResolutionListener.OMIT_FOR_CYCLE, listener, new ResolutionNode(e.getArtifact(), childRemoteRepositories, child)); } catch (ArtifactMetadataRetrievalException e) { artifact.setDependencyTrail(parentNode.getDependencyTrail()); throw e; } } else { return true; } return false; }