List of usage examples for org.apache.maven.artifact.versioning VersionRange matchVersion
public ArtifactVersion matchVersion(List<ArtifactVersion> versions)
From source file:com.adviser.maven.GraphArtifactCollector.java
License:Apache License
private ArtifactVersion getArtifactVersion(ArtifactRepository localRepository, List remoteRepositories, ArtifactMetadataSource source, Artifact artifact) throws OverConstrainedVersionException, ArtifactMetadataRetrievalException { ArtifactVersion version;//from www .ja v a2 s . c om if (!artifact.isSelectedVersionKnown()) { List versions = artifact.getAvailableVersions(); if (versions == null) { versions = source.retrieveAvailableVersions(artifact, localRepository, remoteRepositories); artifact.setAvailableVersions(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, remoteRepositories); } else { throw new OverConstrainedVersionException( "Couldn't find a version in " + versions + " to match range " + versionRange, artifact, remoteRepositories); } } } else { version = artifact.getSelectedVersion(); } return version; }
From source file:org.apache.felix.bundleplugin.baseline.AbstractBaselinePlugin.java
License:Apache License
private Artifact getPreviousArtifact() throws MojoFailureException, MojoExecutionException { // Find the previous version JAR and resolve it, and it's dependencies final VersionRange range; try {//w w w .j av a 2 s. c o m range = VersionRange.createFromVersionSpec(comparisonVersion); } catch (InvalidVersionSpecificationException e) { throw new MojoFailureException("Invalid comparison version: " + e.getMessage()); } final Artifact previousArtifact; try { previousArtifact = factory.createDependencyArtifact(comparisonGroupId, comparisonArtifactId, range, comparisonPackaging, comparisonClassifier, Artifact.SCOPE_COMPILE); if (!previousArtifact.getVersionRange().isSelectedVersionKnown(previousArtifact)) { getLog().debug("Searching for versions in range: " + previousArtifact.getVersionRange()); @SuppressWarnings("unchecked") // type is konwn List<ArtifactVersion> availableVersions = metadataSource.retrieveAvailableVersions(previousArtifact, session.getLocalRepository(), project.getRemoteArtifactRepositories()); filterSnapshots(availableVersions); ArtifactVersion version = range.matchVersion(availableVersions); if (version != null) { previousArtifact.selectVersion(version.toString()); } } } catch (OverConstrainedVersionException ocve) { throw new MojoFailureException("Invalid comparison version: " + ocve.getMessage()); } catch (ArtifactMetadataRetrievalException amre) { throw new MojoExecutionException("Error determining previous version: " + amre.getMessage(), amre); } if (previousArtifact.getVersion() == null) { getLog().info("Unable to find a previous version of the project in the repository"); return null; } try { resolver.resolve(previousArtifact, project.getRemoteArtifactRepositories(), session.getLocalRepository()); } catch (ArtifactResolutionException are) { throw new MojoExecutionException( "Artifact " + previousArtifact + " cannot be resolved : " + are.getMessage(), are); } catch (ArtifactNotFoundException anfe) { throw new MojoExecutionException( "Artifact " + previousArtifact + " does not exist on local/remote repositories", anfe); } return previousArtifact; }
From source file:org.apache.sling.maven.projectsupport.AbstractBundleListMojo.java
License:Apache License
/** * Get a resolved Artifact from the coordinates provided * * @return the artifact, which has been resolved. * @throws MojoExecutionException//from w w w . j a v a 2 s.c o m */ protected Artifact getArtifact(String groupId, String artifactId, String version, String type, String classifier) throws MojoExecutionException { Artifact artifact; VersionRange vr; try { vr = VersionRange.createFromVersionSpec(version); } catch (InvalidVersionSpecificationException e) { vr = VersionRange.createFromVersion(version); } if (StringUtils.isEmpty(classifier)) { artifact = factory.createDependencyArtifact(groupId, artifactId, vr, type, null, Artifact.SCOPE_COMPILE); } else { artifact = factory.createDependencyArtifact(groupId, artifactId, vr, type, classifier, Artifact.SCOPE_COMPILE); } // This code kicks in when the version specifier is a range. if (vr.getRecommendedVersion() == null) { try { List<ArtifactVersion> availVersions = metadataSource.retrieveAvailableVersions(artifact, local, remoteRepos); ArtifactVersion resolvedVersion = vr.matchVersion(availVersions); artifact.setVersion(resolvedVersion.toString()); } catch (ArtifactMetadataRetrievalException e) { throw new MojoExecutionException("Unable to find version for artifact", e); } } try { resolver.resolve(artifact, remoteRepos, local); } catch (ArtifactResolutionException e) { throw new MojoExecutionException("Unable to resolve artifact.", e); } catch (ArtifactNotFoundException e) { throw new MojoExecutionException("Unable to find artifact.", e); } return artifact; }
From source file:org.codehaus.mojo.clirr.AbstractClirrMojo.java
License:Apache License
private Artifact getComparisonArtifact() throws MojoFailureException, MojoExecutionException { // Find the previous version JAR and resolve it, and it's dependencies VersionRange range; try {/*from w w w .j a va 2 s . c o m*/ range = VersionRange.createFromVersionSpec(comparisonVersion); } catch (InvalidVersionSpecificationException e) { throw new MojoFailureException("Invalid comparison version: " + e.getMessage()); } Artifact previousArtifact; try { previousArtifact = factory.createDependencyArtifact(project.getGroupId(), project.getArtifactId(), range, project.getPackaging(), null, Artifact.SCOPE_COMPILE); if (!previousArtifact.getVersionRange().isSelectedVersionKnown(previousArtifact)) { getLog().debug("Searching for versions in range: " + previousArtifact.getVersionRange()); List availableVersions = metadataSource.retrieveAvailableVersions(previousArtifact, localRepository, project.getRemoteArtifactRepositories()); filterSnapshots(availableVersions); ArtifactVersion version = range.matchVersion(availableVersions); if (version != null) { previousArtifact.selectVersion(version.toString()); } } } catch (OverConstrainedVersionException e1) { throw new MojoFailureException("Invalid comparison version: " + e1.getMessage()); } catch (ArtifactMetadataRetrievalException e11) { throw new MojoExecutionException("Error determining previous version: " + e11.getMessage(), e11); } if (previousArtifact.getVersion() == null) { getLog().info("Unable to find a previous version of the project in the repository"); } return previousArtifact; }
From source file:org.eclipse.tycho.nexus.internal.plugin.cache.ParsedRequest.java
License:Open Source License
@SuppressWarnings("deprecation") String selectVersion(final Versioning versioning, final VersionRange versionRange, final boolean findSnapshots) throws ItemNotFoundException { // do not rely on LATEST and RELEASE tag, because they not necessarily correspond to highest version number String selectedVersion = getLatestVersion(versioning, findSnapshots); if (versionRange != null && !versionRange.containsVersion(new DefaultArtifactVersion(selectedVersion))) { final List<ArtifactVersion> versions = new ArrayList<ArtifactVersion>(); for (final String artifactVersion : versioning.getVersions()) { if (findSnapshots || !artifactVersion.endsWith("-SNAPSHOT")) { versions.add(new DefaultArtifactVersion(artifactVersion)); }//from www. j av a2s . co m } final ArtifactVersion matchedVersion = versionRange.matchVersion(versions); if (matchedVersion != null) { selectedVersion = matchedVersion.toString(); } else { throw new ItemNotFoundException("No version found within range"); } } return selectedVersion; }
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 2s . co 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; }