org.gradle.api.artifacts.ResolvedDependency Java Examples

The following examples show how to use org.gradle.api.artifacts.ResolvedDependency. 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: GradleDependencyResolutionHelper.java    From thorntail with Apache License 2.0 6 votes vote down vote up
/**
 * Traverse the dependency tree and resolve the dependencies that will be associated with the given {@code root}
 * dependency. This method will move the entire dependency tree for known fractions to the top level so that the
 * build tool can do its packaging piece properly.
 *
 * @param knownFractions the list of known fractions. This collection should represent each fraction by its
 *                       GAV coordinates in the form {@code group:artifact}
 * @param scope          the scope associated with the {@code root} dependency.
 * @param root           the root dependency, which effectively translates to the key in the dependency map.
 * @param node           the current node (under the root dependency) that is being resolved.
 * @param depsMaps       the dependency map that needs to be populated with the child dependencies.
 */
private static void resolveDependencies(Collection<String> knownFractions, String scope, ResolvedDependency root,
                                        ResolvedDependency node, Map<DependencyDescriptor, Set<DependencyDescriptor>> depsMaps) {

    String key = String.format(GROUP_ARTIFACT_FORMAT, node.getModuleGroup(), node.getModuleName());
    DependencyDescriptor descriptor = asDescriptor(scope, node);
    if (depsMaps.containsKey(descriptor)) {
        return;
    }
    if (knownFractions.contains(key)) {
        depsMaps.put(descriptor, getDependenciesTransitively(scope, node));
        return;
    }
    if (!node.getModuleArtifacts().isEmpty()) {
        depsMaps.computeIfAbsent(asDescriptor(scope, root), __ -> new HashSet<>(3)).add(descriptor);
    }
    node.getChildren().forEach(rd -> resolveDependencies(knownFractions, scope, root, rd, depsMaps));
}
 
Example #2
Source File: GradleArtifactResolvingHelper.java    From ARCHIVE-wildfly-swarm with Apache License 2.0 6 votes vote down vote up
private Set<ResolvedDependency> doResolve(final Collection<ArtifactSpec> deps) {
    final Configuration config = this.project.getConfigurations().detachedConfiguration();
    final DependencySet dependencySet = config.getDependencies();

    deps.forEach(spec -> {
        final DefaultExternalModuleDependency d =
                new DefaultExternalModuleDependency(spec.groupId(), spec.artifactId(), spec.version());
        final DefaultDependencyArtifact da =
                new DefaultDependencyArtifact(spec.artifactId(), spec.type(), spec.type(), spec.classifier(), null);
        d.addArtifact(da);
        d.getExcludeRules().add(new DefaultExcludeRule());
        dependencySet.add(d);
    });

    return config.getResolvedConfiguration().getFirstLevelModuleDependencies();
}
 
Example #3
Source File: GradleArtifactResolvingHelper.java    From ARCHIVE-wildfly-swarm with Apache License 2.0 6 votes vote down vote up
@Override
public ArtifactSpec resolve(final ArtifactSpec spec) {
    if (spec.file != null) {
        return spec;
    }

    final Iterator<ResolvedDependency> iterator =
            doResolve(new HashSet<>(Collections.singletonList(spec))).iterator();
    if (iterator.hasNext()) {
        spec.file = iterator.next()
                .getModuleArtifacts()
                .iterator().next()
                .getFile();

        return spec;
    }

    return null;
}
 
Example #4
Source File: GradleDependencyResolutionHelper.java    From thorntail with Apache License 2.0 6 votes vote down vote up
/**
 * Get the dependencies (transitively) of the given parent element.
 *
 * @param scope  the scope to use for the parent.
 * @param parent the parent dependency.
 * @return a collection of all dependencies of the given parent.
 */
private static Set<DependencyDescriptor> getDependenciesTransitively(String scope, ResolvedDependency parent) {
    Stack<ResolvedDependency> stack = new Stack<>();
    stack.push(parent);
    Set<DependencyDescriptor> dependencies = new HashSet<>();
    while (!stack.empty()) {
        ResolvedDependency rd = stack.pop();
        // Skip the parent's artifacts.
        if (rd != parent) {
            rd.getModuleArtifacts().forEach(a -> dependencies.add(asDescriptor(scope, a)));
        }
        rd.getChildren().forEach(d -> {
            if (!stack.contains(d)) {
                stack.add(d);
            }
        });
    }
    return dependencies;
}
 
Example #5
Source File: GradleDependencyResolutionHelper.java    From thorntail with Apache License 2.0 6 votes vote down vote up
/**
 * Translate the given {@link ResolvedDependency resolved dependency} in to a {@link DependencyDescriptor} reference.
 *
 * @param scope      the scope to assign to the descriptor.
 * @param dependency the resolved dependency reference.
 * @return an instance of {@link DependencyDescriptor}.
 */
private static DependencyDescriptor asDescriptor(String scope, ResolvedDependency dependency) {

    Set<ResolvedArtifact> artifacts = dependency.getModuleArtifacts();

    // Let us use the first artifact's type for determining the type.
    // I am not sure under what circumstances, would we need to check for multiple artifacts.
    String type = "jar";
    String classifier = null;
    File file = null;

    if (!artifacts.isEmpty()) {
        ResolvedArtifact ra = artifacts.iterator().next();
        type = ra.getType();
        classifier = ra.getClassifier();
        file = ra.getFile();
    }
    return new DefaultDependencyDescriptor(scope, dependency.getModuleGroup(), dependency.getModuleName(),
                                           dependency.getModuleVersion(), type, classifier, file);
}
 
Example #6
Source File: GradleArtifactResolvingHelper.java    From wildfly-swarm with Apache License 2.0 6 votes vote down vote up
private Set<ResolvedDependency> doResolve(final Collection<ArtifactSpec> deps) {
    final Configuration config = this.project.getConfigurations().detachedConfiguration();
    final DependencySet dependencySet = config.getDependencies();

    deps.stream()
            .forEach(spec -> {
                if (projects.containsKey(spec.groupId() + ":" + spec.artifactId() + ":" + spec.version())) {
                    dependencySet.add(new DefaultProjectDependency((ProjectInternal) projects.get(spec.groupId() + ":" + spec.artifactId() + ":" + spec.version()), new DefaultProjectAccessListener(), false));
                } else {
                    final DefaultExternalModuleDependency d =
                            new DefaultExternalModuleDependency(spec.groupId(), spec.artifactId(), spec.version());
                    final DefaultDependencyArtifact da =
                            new DefaultDependencyArtifact(spec.artifactId(), spec.type(), spec.type(), spec.classifier(), null);
                    d.addArtifact(da);
                    d.getExcludeRules().add(new DefaultExcludeRule());
                    dependencySet.add(d);
                }
            });

    return config.getResolvedConfiguration().getFirstLevelModuleDependencies();
}
 
Example #7
Source File: GradleArtifactResolvingHelper.java    From wildfly-swarm with Apache License 2.0 6 votes vote down vote up
@Override
public ArtifactSpec resolve(final ArtifactSpec spec) {
    if (spec.file != null) {
        return spec;
    }

    final Iterator<ResolvedDependency> iterator =
            doResolve(new HashSet<>(Collections.singletonList(spec))).iterator();
    if (iterator.hasNext()) {
        spec.file = iterator.next()
                .getModuleArtifacts()
                .iterator().next()
                .getFile();

        return spec;
    }

    return null;
}
 
Example #8
Source File: PinRequirementsTask.java    From pygradle with Apache License 2.0 6 votes vote down vote up
@TaskAction
public void writeOutPinnedFile() throws IOException {
    File pinnedFile = getPinnedFile();
    if (pinnedFile.exists()) {
        pinnedFile.delete();
    }
    pinnedFile.createNewFile();

    // Only add "soft pins" for the *direct* dependencies, so that we don't
    // burn in assumptions about our transitive dependency tree to the output
    // source artifact.
    StringBuilder contents = new StringBuilder();
    for (ResolvedDependency r : getPythonConfiguration().getResolvedConfiguration().getFirstLevelModuleDependencies()) {
        logger.info("Pinning {}=={}", r.getModuleName(), r.getModuleVersion());
        contents.append(r.getModuleName())
            .append("==")
            .append(r.getModuleVersion())
            .append(System.getProperty("line.separator"));  //moduleName==moduleVersion\n
    }

    FileUtils.write(pinnedFile, contents);
}
 
Example #9
Source File: GradleArtifact.java    From ossindex-gradle-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private boolean ancestorIsAlso(ResolvedDependency gradleArtifact) {
    if (parent == null) return false;
    GradleArtifact currentParent = parent;
    while(currentParent != null) {
        if(isEquivalent(gradleArtifact, currentParent)) {
            return true;
        }
        currentParent = currentParent.parent;
    }

    return false;
}
 
Example #10
Source File: PackageTask.java    From wildfly-swarm with Apache License 2.0 5 votes vote down vote up
private void walk(final ResolvedDependency dep) {
    Set<ResolvedArtifact> artifacts = dep.getModuleArtifacts();
    for (ResolvedArtifact each : artifacts) {
        String[] parts = dep.getName().split(":");
        String groupId = parts[0];
        String artifactId = parts[1];
        String version = parts[2];
        this.tool.dependency("compile", groupId, artifactId, version, each.getExtension(),
                             each.getClassifier(), each.getFile());
    }

    dep.getChildren().forEach(this::walk);
}
 
Example #11
Source File: GradleArtifact.java    From ossindex-gradle-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void addChildren(ResolvedDependency gradleArtifact) {
    gradleArtifact.getChildren().forEach(c -> {
                if(!ancestorIsAlso(c)){
                    GradleArtifact child = new GradleArtifact(this, c);
                    children.add(child);
                }
            }
    );
}
 
Example #12
Source File: DefaultResolvedArtifact.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public DefaultResolvedArtifact(ResolvedModuleVersion owner, Factory<ResolvedDependency> ownerSource, IvyArtifactName artifact, Factory<File> artifactSource, long id) {
    this.ownerSource = ownerSource;
    this.owner = owner;
    this.artifact = artifact;
    this.id = id;
    this.artifactSource = artifactSource;
}
 
Example #13
Source File: GradleArtifact.java    From ossindex-gradle-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public GradleArtifact(GradleArtifact parent, ResolvedDependency gradleArtifact) {
    this.parent = parent;
    children = new HashSet<>();
    name = gradleArtifact.getModule().getId().getName();
    group = gradleArtifact.getModule().getId().getGroup();
    version = gradleArtifact.getModule().getId().getVersion();
    addChildren(gradleArtifact);
}
 
Example #14
Source File: DependencyTaskTest.java    From play-services-plugins with Apache License 2.0 5 votes vote down vote up
@NotNull
private ResolvedConfiguration mockResolvedConfigurationFromDependencySet(
        Set<ResolvedDependency> resolvedDependencySet) {

  LenientConfiguration lenientConfiguration = mock(LenientConfiguration.class);
  when(lenientConfiguration.getFirstLevelModuleDependencies()).thenReturn(resolvedDependencySet);
  ResolvedConfiguration resolvedConfiguration = mock(ResolvedConfiguration.class);
  when(resolvedConfiguration.getLenientConfiguration()).thenReturn(lenientConfiguration);
  return resolvedConfiguration;
}
 
Example #15
Source File: DependencyTaskTest.java    From play-services-plugins with Apache License 2.0 5 votes vote down vote up
@NotNull
private ResolvedConfiguration mockResolvedConfiguration(Set<ResolvedArtifact> artifactSet) {
  ResolvedDependency resolvedDependency = mock(ResolvedDependency.class);
  when(resolvedDependency.getAllModuleArtifacts()).thenReturn(artifactSet);
  Set<ResolvedDependency> resolvedDependencySet = Collections.singleton(resolvedDependency);
  return mockResolvedConfigurationFromDependencySet(resolvedDependencySet);
}
 
Example #16
Source File: DependencyTaskTest.java    From play-services-plugins with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetResolvedArtifacts_libraryProject_returnsArtifacts() {
  ResolvedDependency libraryResolvedDependency = mock(ResolvedDependency.class);
  when(libraryResolvedDependency.getModuleVersion())
          .thenReturn(DependencyTask.LOCAL_LIBRARY_VERSION);
  ResolvedDependency libraryChildResolvedDependency = mock(ResolvedDependency.class);
  Set<ResolvedArtifact> libraryChildArtifactSet =
          prepareArtifactSet(/* start= */ 0, /* count= */ 2);
  when(libraryChildResolvedDependency.getAllModuleArtifacts())
          .thenReturn(libraryChildArtifactSet);
  when(libraryResolvedDependency.getChildren())
          .thenReturn(Collections.singleton(libraryChildResolvedDependency));

  Set<ResolvedArtifact> appArtifactSet = prepareArtifactSet(/* start= */ 2, /* count= */ 2);
  ResolvedDependency appResolvedDependency = mock(ResolvedDependency.class);
  when(appResolvedDependency.getAllModuleArtifacts()).thenReturn(appArtifactSet);

  Set<ResolvedDependency> resolvedDependencySet = new HashSet<>(
          Arrays.asList(libraryResolvedDependency, appResolvedDependency));
  ResolvedConfiguration resolvedConfiguration = mockResolvedConfigurationFromDependencySet(
          resolvedDependencySet);

  Configuration configuration = mock(Configuration.class);
  when(configuration.getName()).thenReturn("compile");
  when(configuration.isCanBeResolved()).thenReturn(true);
  when(configuration.getResolvedConfiguration()).thenReturn(resolvedConfiguration);

  Set<ResolvedArtifact> artifactSuperSet = new HashSet<>();
  artifactSuperSet.addAll(appArtifactSet);
  artifactSuperSet.addAll(libraryChildArtifactSet);
  assertThat(dependencyTask.getResolvedArtifacts(configuration), is(artifactSuperSet));
  // Calling getAllModuleArtifacts on a library will cause an exception.
  verify(libraryResolvedDependency, never()).getAllModuleArtifacts();
}
 
Example #17
Source File: DefaultResolvedArtifact.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public ResolvedDependency getResolvedDependency() {
    DeprecationLogger.nagUserOfDeprecated(
            "ResolvedArtifact.getResolvedDependency()",
            "For version info use ResolvedArtifact.getModuleVersion(), to access the dependency graph use ResolvedConfiguration.getFirstLevelModuleDependencies()"
    );
    //resolvedDependency is expensive so lazily create it
    return ownerSource.create();
}
 
Example #18
Source File: JapicmpTask.java    From japicmp-gradle-plugin with Apache License 2.0 5 votes vote down vote up
private void collectArchives(final List<JApiCmpWorkerAction.Archive> archives, ResolvedDependency resolvedDependency) {
    String version = resolvedDependency.getModule().getId().getVersion();
    archives.add(new JApiCmpWorkerAction.Archive(resolvedDependency.getAllModuleArtifacts().iterator().next().getFile(), version));
    for (ResolvedDependency dependency : resolvedDependency.getChildren()) {
        collectArchives(archives, dependency);
    }
}
 
Example #19
Source File: DefaultResolvedDependency.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public Set<ResolvedArtifact> getAllModuleArtifacts() {
    if (allModuleArtifactsCache == null) {
        Set<ResolvedArtifact> allArtifacts = new LinkedHashSet<ResolvedArtifact>();
        allArtifacts.addAll(getModuleArtifacts());
        for (ResolvedDependency childResolvedDependency : getChildren()) {
            allArtifacts.addAll(childResolvedDependency.getAllModuleArtifacts());
        }
        allModuleArtifactsCache = allArtifacts;
    }
    return allModuleArtifactsCache;
}
 
Example #20
Source File: DefaultResolvedDependency.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public Set<ResolvedArtifact> getParentArtifacts(ResolvedDependency parent) {
    if (!parents.contains(parent)) {
        throw new InvalidUserDataException("Provided dependency (" + parent + ") must be a parent of: " + this);
    }
    Set<ResolvedArtifact> artifacts = parentArtifacts.get(parent);
    return artifacts == null ? Collections.<ResolvedArtifact>emptySet() : artifacts;
}
 
Example #21
Source File: DefaultResolvedDependency.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public Set<ResolvedArtifact> getAllArtifacts(ResolvedDependency parent) {
    if (allArtifactsCache.get(parent) == null) {
        Set<ResolvedArtifact> allArtifacts = new LinkedHashSet<ResolvedArtifact>();
        allArtifacts.addAll(getArtifacts(parent));
        for (ResolvedDependency childResolvedDependency : getChildren()) {
            for (ResolvedDependency childParent : childResolvedDependency.getParents()) {
                allArtifacts.addAll(childResolvedDependency.getAllArtifacts(childParent));
            }
        }
        allArtifactsCache.put(parent, allArtifacts);
    }
    return allArtifactsCache.get(parent);
}
 
Example #22
Source File: DefaultResolvedDependency.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void addParentSpecificArtifacts(ResolvedDependency parent, Set<ResolvedArtifact> artifacts) {
    Set<ResolvedArtifact> parentArtifacts = this.parentArtifacts.get(parent);
    if (parentArtifacts == null) {
        parentArtifacts = new TreeSet<ResolvedArtifact>(new ResolvedArtifactComparator());
        this.parentArtifacts.put(parent, parentArtifacts);
    }
    parentArtifacts.addAll(artifacts);
    moduleArtifacts.addAll(artifacts);
}
 
Example #23
Source File: DefaultResolvedArtifact.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public DefaultResolvedArtifact(ResolvedModuleVersion owner, Factory<ResolvedDependency> ownerSource, IvyArtifactName artifact, Factory<File> artifactSource, long id) {
    this.ownerSource = ownerSource;
    this.owner = owner;
    this.artifact = artifact;
    this.id = id;
    this.artifactSource = artifactSource;
}
 
Example #24
Source File: DefaultResolvedArtifact.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public ResolvedDependency getResolvedDependency() {
    DeprecationLogger.nagUserOfDeprecated(
            "ResolvedArtifact.getResolvedDependency()",
            "For version info use ResolvedArtifact.getModuleVersion(), to access the dependency graph use ResolvedConfiguration.getFirstLevelModuleDependencies()"
    );
    //resolvedDependency is expensive so lazily create it
    return ownerSource.create();
}
 
Example #25
Source File: DefaultResolvedDependency.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public Set<ResolvedArtifact> getAllModuleArtifacts() {
    if (allModuleArtifactsCache == null) {
        Set<ResolvedArtifact> allArtifacts = new LinkedHashSet<ResolvedArtifact>();
        allArtifacts.addAll(getModuleArtifacts());
        for (ResolvedDependency childResolvedDependency : getChildren()) {
            allArtifacts.addAll(childResolvedDependency.getAllModuleArtifacts());
        }
        allModuleArtifactsCache = allArtifacts;
    }
    return allModuleArtifactsCache;
}
 
Example #26
Source File: DefaultResolvedDependency.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public Set<ResolvedArtifact> getParentArtifacts(ResolvedDependency parent) {
    if (!parents.contains(parent)) {
        throw new InvalidUserDataException("Provided dependency (" + parent + ") must be a parent of: " + this);
    }
    Set<ResolvedArtifact> artifacts = parentArtifacts.get(parent);
    return artifacts == null ? Collections.<ResolvedArtifact>emptySet() : artifacts;
}
 
Example #27
Source File: DefaultResolvedDependency.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public Set<ResolvedArtifact> getAllArtifacts(ResolvedDependency parent) {
    if (allArtifactsCache.get(parent) == null) {
        Set<ResolvedArtifact> allArtifacts = new LinkedHashSet<ResolvedArtifact>();
        allArtifacts.addAll(getArtifacts(parent));
        for (ResolvedDependency childResolvedDependency : getChildren()) {
            for (ResolvedDependency childParent : childResolvedDependency.getParents()) {
                allArtifacts.addAll(childResolvedDependency.getAllArtifacts(childParent));
            }
        }
        allArtifactsCache.put(parent, allArtifacts);
    }
    return allArtifactsCache.get(parent);
}
 
Example #28
Source File: DefaultResolvedDependency.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void addParentSpecificArtifacts(ResolvedDependency parent, Set<ResolvedArtifact> artifacts) {
    Set<ResolvedArtifact> parentArtifacts = this.parentArtifacts.get(parent);
    if (parentArtifacts == null) {
        parentArtifacts = new TreeSet<ResolvedArtifact>(new ResolvedArtifactComparator());
        this.parentArtifacts.put(parent, parentArtifacts);
    }
    parentArtifacts.addAll(artifacts);
    moduleArtifacts.addAll(artifacts);
}
 
Example #29
Source File: PackageTask.java    From ARCHIVE-wildfly-swarm with Apache License 2.0 5 votes vote down vote up
private void walk(final boolean top, ResolvedDependency dep) {
    Set<ResolvedArtifact> artifacts = dep.getModuleArtifacts();
    for (ResolvedArtifact each : artifacts) {
        String[] parts = dep.getName().split(":");
        String groupId = parts[0];
        String artifactId = parts[1];
        String version = parts[2];
        this.tool.dependency("compile", groupId, artifactId, version, each.getExtension(),
                             each.getClassifier(), each.getFile(), top);
    }

    dep.getChildren().forEach(d -> walk(false, d));
}
 
Example #30
Source File: JapicmpTask.java    From japicmp-gradle-plugin with Apache License 2.0 5 votes vote down vote up
private List<JApiCmpWorkerAction.Archive> inferArchives(FileCollection fc) {
    if (fc instanceof Configuration) {
        final List<JApiCmpWorkerAction.Archive> archives = new ArrayList<>();
        Set<ResolvedDependency> firstLevelModuleDependencies = ((Configuration) fc).getResolvedConfiguration().getFirstLevelModuleDependencies();
        for (ResolvedDependency moduleDependency : firstLevelModuleDependencies) {
            collectArchives(archives, moduleDependency);
        }
        return archives;
    }

    return toArchives(fc);
}