Java Code Examples for org.apache.maven.artifact.versioning.VersionRange#containsVersion()

The following examples show how to use org.apache.maven.artifact.versioning.VersionRange#containsVersion() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ResolvableDependency.java    From java-specialagent with Apache License 2.0 6 votes vote down vote up
List<Dependency> resolveVersions(final MavenProject project, final Log log) throws InvalidVersionSpecificationException, MojoExecutionException {
  if (resolvedVersions != null)
    return resolvedVersions;

  if (isSingleVersion())
    return resolvedVersions = Collections.singletonList(MavenUtil.newDependency(getGroupId(), getArtifactId(), getVersion()));

  final ResolvableDependency versionDependency = isOwnVersion() ? this : ResolvableDependency.parse(getVersion());
  resolvedVersions = new ArrayList<>();

  final SortedSet<DefaultArtifactVersion> versions = resolveVersions(project, log, versionDependency);
  final VersionRange versionRange = VersionRange.createFromVersionSpec(versionDependency.getVersion());
  for (final DefaultArtifactVersion version : versions)
    if (versionRange.containsVersion(version))
      resolvedVersions.add(MavenUtil.newDependency(getGroupId(), getArtifactId(), version.toString()));

  return resolvedVersions;
}
 
Example 2
Source File: ArtifactRetriever.java    From google-cloud-eclipse with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the latest published release artifact version in the version range,
 * or null if there is no such version.
 */
public ArtifactVersion getLatestReleaseVersion(
    String groupId, String artifactId, VersionRange range) {
  String coordinates = idToKey(groupId, artifactId);
  try {
    NavigableSet<ArtifactVersion> versions = availableVersions.get(coordinates);
    for (ArtifactVersion version : versions.descendingSet()) {
      if (isReleased(version)) {
        if (range == null || range.containsVersion(version)) {
          return version;
        }
      }
    }
  } catch (ExecutionException ex) {
    logger.log(
        Level.WARNING,
        "Could not retrieve version for artifact " + coordinates,
        ex.getCause());
  }
  return null;
}
 
Example 3
Source File: CdiContainerUnderTest.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
/**
 * Verify if the runtime is using the following CdiImplementation
 * 
 * @param cdiImplementation
 * @param versionRange
 *            optional - If not defined it will used the range defined on {@link CdiImplementation}
 * @return
 * @throws InvalidVersionSpecificationException
 */
public static boolean isCdiVersion(CdiImplementation cdiImplementation, String versionRange)
    throws InvalidVersionSpecificationException
{

    Class implementationClass = tryToLoadClassForName(cdiImplementation.getImplementationClassName());

    if (implementationClass == null)
    {
        return false;
    }

    VersionRange range = VersionRange.createFromVersionSpec(versionRange == null ? cdiImplementation
            .getVersionRange() : versionRange);
    String containerVersion = getJarSpecification(implementationClass);
    return containerVersion != null && range.containsVersion(new DefaultArtifactVersion(containerVersion));
}
 
Example 4
Source File: VersionUtil.java    From phantomjs-maven-plugin with MIT License 5 votes vote down vote up
private static boolean isWithin(String version, VersionRange versionRange) {
  ArtifactVersion artifactVersion = new DefaultArtifactVersion(version);
  boolean within = false;
  if (versionRange != null) {
    ArtifactVersion recommendedVersion = versionRange.getRecommendedVersion();
    // treat recommended version as minimum version
    within = recommendedVersion != null ?
      VersionUtil.isLessThanOrEqualTo(recommendedVersion, artifactVersion) :
      versionRange.containsVersion(artifactVersion);
  }
  return within;
}
 
Example 5
Source File: PluginBundleManager.java    From BIMserver with GNU Affero General Public License v3.0 4 votes vote down vote up
private void loadDependencies(String pluginBundleVersion, boolean strictDependencyChecking, Model model,
			DelegatingClassLoader delegatingClassLoader)
			throws DependencyCollectionException, InvalidVersionSpecificationException, Exception {
		if (model.getRepositories() != null) {
			for (Repository repository : model.getRepositories()) {
				mavenPluginRepository.addRepository(repository.getId(), "default", repository.getUrl());
			}
		}

		List<Dependency> dependenciesToResolve = new ArrayList<>();
		for (org.apache.maven.model.Dependency dependency2 : model.getDependencies()) {
			String scope = dependency2.getScope();
			if (scope != null && (scope.contentEquals("test"))) {
				// Skip
				continue;
			}
			Dependency d = new Dependency(new DefaultArtifact(dependency2.getGroupId(), dependency2.getArtifactId(), dependency2.getType(), dependency2.getVersion()), dependency2.getScope());
			Set<Exclusion> exclusions = new HashSet<>();
			d.setExclusions(exclusions);
			exclusions.add(new Exclusion("org.opensourcebim", "pluginbase", null, "jar"));
			exclusions.add(new Exclusion("org.opensourcebim", "shared", null, "jar"));
			exclusions.add(new Exclusion("org.opensourcebim", "ifcplugins", null, "jar"));
			dependenciesToResolve.add(d);
		}
		CollectRequest collectRequest = new CollectRequest(dependenciesToResolve, null, null);
		collectRequest.setRepositories(mavenPluginRepository.getRepositoriesAsList());
		CollectResult collectDependencies = mavenPluginRepository.getSystem().collectDependencies(mavenPluginRepository.getSession(), collectRequest);
		PreorderNodeListGenerator nlg = new PreorderNodeListGenerator();
		DependencyNode rootDep = collectDependencies.getRoot();
		rootDep.accept(nlg);
		
		for (Dependency dependency : nlg.getDependencies(true)) {
			if (dependency.getScope().contentEquals("test")) {
				continue;
			}
//			LOGGER.info(dependency.getArtifact().getGroupId() + "." + dependency.getArtifact().getArtifactId());
			Artifact dependencyArtifact = dependency.getArtifact();
			PluginBundleIdentifier pluginBundleIdentifier = new PluginBundleIdentifier(dependencyArtifact.getGroupId(), dependencyArtifact.getArtifactId());
			if (pluginBundleIdentifierToPluginBundle.containsKey(pluginBundleIdentifier)) {
				if (strictDependencyChecking) {
					String version = dependencyArtifact.getVersion();
					if (!version.contains("[") && !version.contains("(")) {
						version = "[" + version + "]";
					}
					VersionRange versionRange = VersionRange.createFromVersionSpec(version);
					// String version =
					// pluginBundleIdentifierToPluginBundle.get(pluginBundleIdentifier).getPluginBundleVersion().getVersion();
					ArtifactVersion artifactVersion = new DefaultArtifactVersion(pluginBundleVersion);
					if (versionRange.containsVersion(artifactVersion)) {
						// OK
					} else {
						throw new Exception(
								"Required dependency " + pluginBundleIdentifier + " is installed, but it's version (" + pluginBundleVersion + ") does not comply to the required version (" + dependencyArtifact.getVersion() + ")");
					}
				} else {
					LOGGER.info("Skipping strict dependency checking for dependency " + dependencyArtifact.getArtifactId());
				}
			} else {
				try {
					if (dependencyArtifact.getGroupId().contentEquals("com.sun.xml.ws")) {
						continue;
					}
					MavenPluginLocation mavenPluginLocation = mavenPluginRepository.getPluginLocation(dependencyArtifact.getGroupId(), dependencyArtifact.getArtifactId());
					if (dependencyArtifact.getExtension().contentEquals("jar")) {
						Path depJarFile = mavenPluginLocation.getVersionJar(dependencyArtifact.getVersion());
						
						FileJarClassLoader jarClassLoader = new FileJarClassLoader(pluginManager, delegatingClassLoader, depJarFile);
						jarClassLoaders.add(jarClassLoader);
						delegatingClassLoader.add(jarClassLoader);
					}
				} catch (Exception e) {
					e.printStackTrace();
					throw new Exception("Required dependency " + pluginBundleIdentifier + " is not installed");
				}
			}
		}
	}
 
Example 6
Source File: PluginBundleManager.java    From BIMserver with GNU Affero General Public License v3.0 4 votes vote down vote up
public PluginBundle loadFromPluginDir(PluginBundleVersionIdentifier pluginBundleVersionIdentifier, SPluginBundleVersion pluginBundleVersion, List<SPluginInformation> plugins, boolean strictDependencyChecking) throws Exception {
	Path target = pluginsDir.resolve(pluginBundleVersionIdentifier.getFileName());
	if (!Files.exists(target)) {
		throw new PluginException(target.toString() + " not found");
	}

	SPluginBundle sPluginBundle = new SPluginBundle();

	MavenXpp3Reader mavenreader = new MavenXpp3Reader();

	Model model = null;
	try (JarFile jarFile = new JarFile(target.toFile())) {
		ZipEntry entry = jarFile.getEntry("META-INF/maven/" + pluginBundleVersion.getGroupId() + "/" + pluginBundleVersion.getArtifactId() + "/pom.xml");
		try (InputStream inputStream = jarFile.getInputStream(entry)) {
			model = mavenreader.read(inputStream);
		}
	}
	sPluginBundle.setOrganization(model.getOrganization().getName());
	sPluginBundle.setName(model.getName());

	DelegatingClassLoader delegatingClassLoader = new DelegatingClassLoader(getClass().getClassLoader());

	loadDependencies(model.getVersion(), strictDependencyChecking, model, delegatingClassLoader);
	
	for (org.apache.maven.model.Dependency dependency : model.getDependencies()) {
		if (dependency.getGroupId().equals("org.opensourcebim") && (dependency.getArtifactId().equals("shared") || dependency.getArtifactId().equals("pluginbase") || dependency.getArtifactId().equals("ifcplugins"))) {
			// TODO Skip, we should also check the version though
		} else {
			PluginBundleIdentifier pluginBundleIdentifier = new PluginBundleIdentifier(dependency.getGroupId(), dependency.getArtifactId());
			if (pluginBundleIdentifierToPluginBundle.containsKey(pluginBundleIdentifier)) {
				if (strictDependencyChecking) {
					VersionRange versionRange = VersionRange.createFromVersion(dependency.getVersion());
					String version = pluginBundleIdentifierToPluginBundle.get(pluginBundleIdentifier).getPluginBundleVersion().getVersion();
					ArtifactVersion artifactVersion = new DefaultArtifactVersion(version);
					if (versionRange.containsVersion(artifactVersion)) {
						// OK
					} else {
						throw new Exception("Required dependency " + pluginBundleIdentifier + " is installed, but it's version (" + version + ") does not comply to the required version (" + dependency.getVersion() + ")");
					}
				} else {
					LOGGER.info("Skipping strict dependency checking for dependency " + dependency.getArtifactId());
				}
			} else {
				if (dependency.getGroupId().equals("org.opensourcebim") && (dependency.getArtifactId().equals("shared") || dependency.getArtifactId().equals("pluginbase"))) {
				} else {
					MavenPluginLocation mavenPluginLocation = mavenPluginRepository.getPluginLocation(dependency.getGroupId(), dependency.getArtifactId());

					try {
						Path depJarFile = mavenPluginLocation.getVersionJar(dependency.getVersion());

						FileJarClassLoader jarClassLoader = new FileJarClassLoader(pluginManager, delegatingClassLoader, depJarFile);
						jarClassLoaders.add(jarClassLoader);
						delegatingClassLoader.add(jarClassLoader);
					} catch (Exception e) {

					}
				}
			}
		}
	}
	return loadPlugin(pluginBundleVersionIdentifier, target, sPluginBundle, pluginBundleVersion, plugins, delegatingClassLoader);
}