use of org.eclipse.aether.graph.Dependency in project BIMserver by opensourceBIM.
the class PluginBundleManager method loadDependencies.
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 mvnDependency : model.getDependencies()) {
String scope = mvnDependency.getScope();
if (scope != null && (scope.contentEquals("test"))) {
// Skip
continue;
}
Dependency d = new Dependency(new DefaultArtifact(mvnDependency.getGroupId(), mvnDependency.getArtifactId(), mvnDependency.getType(), mvnDependency.getVersion()), scope);
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"));
if (!isDependencyProvided(mvnDependency))
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");
}
}
}
}
use of org.eclipse.aether.graph.Dependency in project qpid-broker-j by apache.
the class ClasspathQuery method getJarFiles.
private static Set<File> getJarFiles(final Collection<String> gavs) {
Set<File> jars = new HashSet<>();
for (final String gav : gavs) {
Artifact artifact = new DefaultArtifact(gav);
DependencyFilter classpathFlter = DependencyFilterUtils.classpathFilter(JavaScopes.COMPILE);
CollectRequest collectRequest = new CollectRequest();
collectRequest.setRoot(new Dependency(artifact, JavaScopes.COMPILE));
collectRequest.setRepositories(Booter.newRepositories());
DependencyRequest dependencyRequest = new DependencyRequest(collectRequest, classpathFlter);
List<ArtifactResult> artifactResults = null;
try {
artifactResults = _mavenRepositorySystem.resolveDependencies(_mavenRepositorySession, dependencyRequest).getArtifactResults();
} catch (DependencyResolutionException e) {
throw new RuntimeException(String.format("Error while dependency resolution for '%s'", gav), e);
}
if (artifactResults == null) {
throw new RuntimeException(String.format("Could not resolve dependency for '%s'", gav));
}
for (ArtifactResult artifactResult : artifactResults) {
System.out.println(artifactResult.getArtifact() + " resolved to " + artifactResult.getArtifact().getFile());
}
jars.addAll(artifactResults.stream().map(result -> result.getArtifact().getFile()).collect(Collectors.toSet()));
}
return jars;
}
use of org.eclipse.aether.graph.Dependency in project wildfly-swarm by wildfly-swarm.
the class MavenArtifactResolvingHelper method resolveAll.
@Override
public Set<ArtifactSpec> resolveAll(Collection<ArtifactSpec> specs, boolean transitive, boolean defaultExcludes) throws Exception {
if (specs.isEmpty()) {
return Collections.emptySet();
}
List<DependencyNode> nodes;
if (transitive) {
final CollectRequest request = new CollectRequest();
request.setRepositories(this.remoteRepositories);
// SWARM-1031
if (this.dependencyManagement != null) {
List<Dependency> managedDependencies = this.dependencyManagement.getDependencies().stream().map(mavenDep -> {
DefaultArtifact artifact = new DefaultArtifact(mavenDep.getGroupId(), mavenDep.getArtifactId(), mavenDep.getType(), mavenDep.getVersion());
return new Dependency(artifact, mavenDep.getScope());
}).collect(Collectors.toList());
request.setManagedDependencies(managedDependencies);
}
specs.forEach(spec -> request.addDependency(new Dependency(new DefaultArtifact(spec.groupId(), spec.artifactId(), spec.classifier(), spec.type(), spec.version()), "compile")));
RepositorySystemSession tempSession = new RepositorySystemSessionWrapper(this.session, new ConflictResolver(new NearestVersionSelector(), new JavaScopeSelector(), new SimpleOptionalitySelector(), new JavaScopeDeriver()), defaultExcludes);
CollectResult result = this.system.collectDependencies(tempSession, request);
PreorderNodeListGenerator gen = new PreorderNodeListGenerator();
result.getRoot().accept(gen);
nodes = gen.getNodes();
} else {
nodes = new ArrayList<>();
for (ArtifactSpec spec : specs) {
Dependency dependency = new Dependency(new DefaultArtifact(spec.groupId(), spec.artifactId(), spec.classifier(), spec.type(), spec.version()), "compile");
DefaultDependencyNode node = new DefaultDependencyNode(dependency);
nodes.add(node);
}
}
List<DependencyNode> extraDependencies = ExtraArtifactsHandler.getExtraDependencies(nodes);
nodes.addAll(extraDependencies);
resolveDependenciesInParallel(nodes);
return nodes.stream().filter(node -> !"system".equals(node.getDependency().getScope())).map(node -> {
final Artifact artifact = node.getArtifact();
return new ArtifactSpec(node.getDependency().getScope(), artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), artifact.getExtension(), artifact.getClassifier(), null);
}).map(this::resolve).filter(Objects::nonNull).collect(Collectors.toSet());
}
use of org.eclipse.aether.graph.Dependency in project gate-core by GateNLP.
the class SimpleMavenCache method cacheArtifact.
public void cacheArtifact(Artifact artifact) throws IOException, SettingsBuildingException, DependencyCollectionException, DependencyResolutionException, ArtifactResolutionException, ModelBuildingException {
List<RemoteRepository> repos = getRepositoryList();
Dependency dependency = new Dependency(artifact, "runtime");
RepositorySystem repoSystem = getRepositorySystem();
RepositorySystemSession repoSession = getRepositorySession(repoSystem, null);
CollectRequest collectRequest = new CollectRequest(dependency, repos);
DependencyNode node = repoSystem.collectDependencies(repoSession, collectRequest).getRoot();
DependencyRequest dependencyRequest = new DependencyRequest();
dependencyRequest.setRoot(node);
DependencyResult result = repoSystem.resolveDependencies(repoSession, dependencyRequest);
for (ArtifactResult ar : result.getArtifactResults()) {
// generate output file name from the *requested* artifact rather
// than the resolved one (e.g. if we requested a -SNAPSHOT version
// but got a timestamped one)
File file = getArtifactFile(ar.getRequest().getArtifact());
FileUtils.copyFile(ar.getArtifact().getFile(), file);
// get the POM corresponding to the specific resolved JAR
Artifact pomArtifact = new SubArtifact(ar.getArtifact(), "", "pom");
ArtifactRequest artifactRequest = new ArtifactRequest(pomArtifact, repos, null);
ArtifactResult artifactResult = repoSystem.resolveArtifact(repoSession, artifactRequest);
// but copy it to a file named for the original requested version number
file = getArtifactFile(new SubArtifact(ar.getRequest().getArtifact(), "", "pom"));
FileUtils.copyFile(artifactResult.getArtifact().getFile(), file);
cacheParents(artifactResult.getArtifact().getFile(), repoSystem, repoSession, repos);
}
}
use of org.eclipse.aether.graph.Dependency in project mule by mulesoft.
the class AetherClassPathClassifier method buildTestRunnerUrlClassification.
/**
* Application classification is being done by resolving the direct dependencies with scope
* {@value org.eclipse.aether.util.artifact.JavaScopes#TEST} for the rootArtifact. Due to Eclipse Aether resolution excludes by
* {@value org.eclipse.aether.util.artifact.JavaScopes#TEST} dependencies an imaginary pom will be created with these
* dependencies as {@value org.eclipse.aether.util.artifact.JavaScopes#COMPILE} so the dependency graph can be resolved (with
* the same results as it will be obtained from Maven).
* <p/>
* If the rootArtifact was classified as plugin its {@value org.eclipse.aether.util.artifact.JavaScopes#COMPILE} will be changed
* to {@value org.eclipse.aether.util.artifact.JavaScopes#PROVIDED} in order to exclude them from the dependency graph.
* <p/>
* Filtering logic includes the following pattern to includes the patterns defined at
* {@link ClassPathClassifierContext#getTestInclusions()}. It also excludes
* {@link ClassPathClassifierContext#getExcludedArtifacts()}, {@link ClassPathClassifierContext#getTestExclusions()}.
* <p/>
* If the application artifact has not been classified as plugin its going to be resolved as {@value #JAR_EXTENSION} in order to
* include this its compiled classes classification.
*
* @param context {@link ClassPathClassifierContext} with settings for the classification process
* @param directDependencies {@link List} of {@link Dependency} with direct dependencies for the rootArtifact
* @param rootArtifactType {@link ArtifactClassificationType} for rootArtifact @return {@link URL}s for application class loader
* @param rootArtifactRemoteRepositories remote repositories defined at the rootArtifact
*/
private List<URL> buildTestRunnerUrlClassification(ClassPathClassifierContext context, List<Dependency> directDependencies, ArtifactClassificationType rootArtifactType, List<RemoteRepository> rootArtifactRemoteRepositories) {
logger.debug("Building application classification");
Artifact rootArtifact = context.getRootArtifact();
DependencyFilter dependencyFilter = new PatternInclusionsDependencyFilter(context.getTestInclusions());
logger.debug("Using filter for dependency graph to include: '{}'", context.getTestInclusions());
List<File> appFiles = newArrayList();
List<String> exclusionsPatterns = newArrayList();
if (APPLICATION.equals(rootArtifactType)) {
logger.debug("RootArtifact identified as {} so is going to be added to application classification", APPLICATION);
File rootArtifactOutputFile = resolveRootArtifactFile(rootArtifact);
if (rootArtifactOutputFile != null) {
appFiles.add(rootArtifactOutputFile);
} else {
logger.warn("rootArtifact '{}' identified as {} but doesn't have an output {} file", rootArtifact, rootArtifactType, JAR_EXTENSION);
}
} else {
logger.debug("RootArtifact already classified as plugin or module, excluding it from application classification");
exclusionsPatterns.add(rootArtifact.getGroupId() + MAVEN_COORDINATES_SEPARATOR + rootArtifact.getArtifactId() + MAVEN_COORDINATES_SEPARATOR + "*" + MAVEN_COORDINATES_SEPARATOR + "*" + MAVEN_COORDINATES_SEPARATOR + rootArtifact.getVersion());
}
directDependencies = directDependencies.stream().map(toTransform -> {
if (toTransform.getScope().equals(TEST)) {
if (TESTS_CLASSIFIER.equals(toTransform.getArtifact().getClassifier())) {
// Exclude transitive dependencies of test-jar artifacts
return toTransform.setScope(COMPILE).setExclusions(singleton(new Exclusion("*", "*", "*", "*")));
} else {
return toTransform.setScope(COMPILE);
}
}
if ((PLUGIN.equals(rootArtifactType) || MULE_PLUGIN_CLASSIFIER.equals(toTransform.getArtifact().getClassifier()) || MULE_SERVICE_CLASSIFIER.equals(toTransform.getArtifact().getClassifier())) && toTransform.getScope().equals(COMPILE)) {
return toTransform.setScope(PROVIDED);
}
if (rootArtifactType == MODULE && toTransform.getScope().equals(COMPILE)) {
return toTransform.setScope(PROVIDED);
}
Artifact artifact = toTransform.getArtifact();
if (context.getApplicationSharedLibCoordinates().contains(artifact.getGroupId() + ":" + artifact.getArtifactId())) {
return toTransform.setScope(COMPILE);
}
return toTransform;
}).collect(toList());
logger.debug("OR exclude: {}", context.getExcludedArtifacts());
exclusionsPatterns.addAll(context.getExcludedArtifacts());
if (!context.getTestExclusions().isEmpty()) {
logger.debug("OR exclude application specific artifacts: {}", context.getTestExclusions());
exclusionsPatterns.addAll(context.getTestExclusions());
}
try {
List<Dependency> managedDependencies = newArrayList(dependencyResolver.readArtifactDescriptor(rootArtifact).getManagedDependencies());
managedDependencies.addAll(directDependencies.stream().filter(directDependency -> !directDependency.getScope().equals(TEST)).collect(toList()));
logger.debug("Resolving dependency graph for '{}' scope direct dependencies: {} and managed dependencies {}", TEST, directDependencies, managedDependencies);
final Dependency rootTestDependency = new Dependency(new DefaultArtifact(rootArtifact.getGroupId(), rootArtifact.getArtifactId(), TESTS_CLASSIFIER, JAR_EXTENSION, rootArtifact.getVersion()), TEST);
List<File> urls = dependencyResolver.resolveDependencies(rootTestDependency, directDependencies, managedDependencies, orFilter(dependencyFilter, new PatternExclusionsDependencyFilter(exclusionsPatterns)), rootArtifactRemoteRepositories);
appFiles.addAll(urls);
} catch (Exception e) {
throw new IllegalStateException("Couldn't resolve dependencies for application '" + context.getRootArtifact() + "' classification", e);
}
List<URL> testRunnerLibUrls = newArrayList(toUrl(appFiles));
logger.debug("Appending URLs to test runner plugin: {}", context.getTestRunnerPluginUrls());
testRunnerLibUrls.addAll(context.getTestRunnerPluginUrls());
resolveSnapshotVersionsToTimestampedFromClassPath(testRunnerLibUrls, context.getClassPathURLs());
return testRunnerLibUrls;
}
Aggregations