use of org.apache.maven.artifact.versioning.VersionRange in project maven-plugins by apache.
the class TestDependencyUtil method testFileNameClassifierWithFile.
public void testFileNameClassifierWithFile() throws MojoExecutionException {
// specifically testing the default operation that getFormattedFileName
// returns
// the actual name of the file if available unless remove version is
// set.
ArtifactHandler ah = new DefaultArtifactHandlerStub("war", "sources");
VersionRange vr = VersionRange.createFromVersion("1.1-SNAPSHOT");
Artifact artifact = new DefaultArtifact("test", "two", vr, Artifact.SCOPE_PROVIDED, "war", "sources", ah, false);
File file = new File("/target", "test-file-name.jar");
artifact.setFile(file);
String name = DependencyUtil.getFormattedFileName(artifact, false);
String expectedResult = "two-1.1-SNAPSHOT-sources.war";
assertEquals(expectedResult, name);
name = DependencyUtil.getFormattedFileName(artifact, false, false, false, true);
expectedResult = "two-1.1-SNAPSHOT.war";
assertEquals(expectedResult, name);
name = DependencyUtil.getFormattedFileName(artifact, true);
expectedResult = "two-sources.war";
assertEquals(expectedResult, name);
artifact = new DefaultArtifact("test", "two", vr, Artifact.SCOPE_PROVIDED, "war", "", ah, false);
name = DependencyUtil.getFormattedFileName(artifact, true);
expectedResult = "two.war";
assertEquals(expectedResult, name);
// test that we pickup the correct extension in the file name if set.
ah = new DefaultArtifactHandlerStub("jar", null);
artifact = new DefaultArtifact("test", "two", vr, Artifact.SCOPE_PROVIDED, "war", "", ah, false);
name = DependencyUtil.getFormattedFileName(artifact, true);
expectedResult = "two.jar";
assertEquals(expectedResult, name);
}
use of org.apache.maven.artifact.versioning.VersionRange in project maven-plugins by apache.
the class TestDependencyUtil method testFileNameClassifier.
public void testFileNameClassifier() throws MojoExecutionException {
ArtifactHandler ah = new DefaultArtifactHandlerStub("jar", "sources");
VersionRange vr = VersionRange.createFromVersion("1.1-SNAPSHOT");
Artifact artifact = new DefaultArtifact("test", "two", vr, Artifact.SCOPE_PROVIDED, "jar", "sources", ah, false);
String name = DependencyUtil.getFormattedFileName(artifact, false);
String expectedResult = "two-1.1-SNAPSHOT-sources.jar";
assertEquals(expectedResult, name);
name = DependencyUtil.getFormattedFileName(artifact, true);
expectedResult = "two-sources.jar";
assertEquals(expectedResult, name);
name = DependencyUtil.getFormattedFileName(artifact, false, false, false, true);
expectedResult = "two-1.1-SNAPSHOT.jar";
assertEquals(expectedResult, name);
ah = new DefaultArtifactHandlerStub("war", null);
artifact = new DefaultArtifact("test", "two", vr, Artifact.SCOPE_PROVIDED, "war", "", ah, false);
name = DependencyUtil.getFormattedFileName(artifact, true);
expectedResult = "two.war";
assertEquals(expectedResult, name);
}
use of org.apache.maven.artifact.versioning.VersionRange in project maven-plugins by apache.
the class TestSourcesMarkerFileHandler method setUp.
protected void setUp() throws Exception {
super.setUp();
ArtifactHandler ah = new DefaultArtifactHandler();
VersionRange vr = VersionRange.createFromVersion("1.1");
Artifact artifact = new DefaultArtifact("test", "1", vr, Artifact.SCOPE_COMPILE, "jar", "", ah, false);
artifacts.add(artifact);
artifact = new DefaultArtifact("test", "2", vr, Artifact.SCOPE_PROVIDED, "war", "", ah, false);
artifacts.add(artifact);
artifact = new DefaultArtifact("test", "3", vr, Artifact.SCOPE_TEST, "sources", "", ah, false);
artifacts.add(artifact);
artifact = new DefaultArtifact("test", "4", vr, Artifact.SCOPE_RUNTIME, "zip", "", ah, false);
artifacts.add(artifact);
// pick random output location
Random a = new Random();
outputFolder = new File("target/markers" + a.nextLong() + "/");
outputFolder.delete();
assertFalse(outputFolder.exists());
}
use of org.apache.maven.artifact.versioning.VersionRange in project maven-plugins by apache.
the class DependencyManagementRenderer method getDependencyRow.
@SuppressWarnings("unchecked")
private String[] getDependencyRow(Dependency dependency, boolean hasClassifier) {
Artifact artifact = artifactFactory.createProjectArtifact(dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion());
StringBuilder licensesBuffer = new StringBuilder();
String url = null;
try {
VersionRange range = VersionRange.createFromVersionSpec(dependency.getVersion());
if (range.getRecommendedVersion() == null) {
// MPIR-216: no direct version but version range: need to choose one precise version
log.debug("Resolving range for DependencyManagement on " + artifact.getId());
List<ArtifactVersion> versions = artifactMetadataSource.retrieveAvailableVersions(artifact, localRepository, remoteRepositories);
// only use versions from range
for (Iterator<ArtifactVersion> iter = versions.iterator(); iter.hasNext(); ) {
if (!range.containsVersion(iter.next())) {
iter.remove();
}
}
// select latest, assuming pom information will be the most accurate
if (versions.size() > 0) {
ArtifactVersion maxArtifactVersion = Collections.max(versions);
artifact.setVersion(maxArtifactVersion.toString());
log.debug("DependencyManagement resolved: " + artifact.getId());
}
}
url = ProjectInfoReportUtils.getArtifactUrl(artifactFactory, artifact, mavenProjectBuilder, remoteRepositories, localRepository);
MavenProject artifactProject = repoUtils.getMavenProjectFromRepository(artifact);
List<License> licenses = artifactProject.getLicenses();
for (License license : licenses) {
String licenseCell = ProjectInfoReportUtils.getArtifactIdCell(license.getName(), license.getUrl());
if (licensesBuffer.length() > 0) {
licensesBuffer.append(", ");
}
licensesBuffer.append(licenseCell);
}
} catch (InvalidVersionSpecificationException e) {
log.warn("Unable to parse version for " + artifact.getId(), e);
} catch (ArtifactMetadataRetrievalException e) {
log.warn("Unable to retrieve versions for " + artifact.getId() + " from repository.", e);
} catch (ProjectBuildingException e) {
log.warn("Unable to create Maven project for " + artifact.getId() + " from repository.", e);
}
String artifactIdCell = ProjectInfoReportUtils.getArtifactIdCell(artifact.getArtifactId(), url);
if (hasClassifier) {
return new String[] { dependency.getGroupId(), artifactIdCell, dependency.getVersion(), dependency.getClassifier(), dependency.getType(), licensesBuffer.toString() };
}
return new String[] { dependency.getGroupId(), artifactIdCell, dependency.getVersion(), dependency.getType(), licensesBuffer.toString() };
}
use of org.apache.maven.artifact.versioning.VersionRange in project sling by apache.
the class AbstractBundleListMojo method getArtifact.
/**
* Get a resolved Artifact from the coordinates provided
*
* @return the artifact, which has been resolved.
* @throws MojoExecutionException
*/
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;
}
Aggregations