protected Properties getThinProperties(Configuration configuration) { Properties properties = new Properties(); // TODO: add computed flag to task and offer option not to compute transitives properties.setProperty("computed", "true"); if (configuration != null) { for (ResolvedArtifact artifact : configuration.getResolvedConfiguration() .getResolvedArtifacts()) { properties.setProperty("dependencies." + key(artifact, properties), coordinates(artifact, true)); } } return properties; }
/** * For the given "resolved configuration" (all of the artifacts in a Gradle Configuration, such as "compile"), * return all of the bundle symbolic names in the bundles. */ private List<String> symbolicNames(ResolvedConfiguration resolvedConfiguration) { return symbolicNames(project.files(resolvedConfiguration.getResolvedArtifacts())); }
private static Collection<ResolvedArtifact> mainClassPath(Project p, String confName, boolean fail) { Configuration conf = p.getConfigurations().getByName(confName); if (conf == null) { if (fail) { throw new GradleException("Cannot find " + confName + " configuration for project " + p); } return Collections.emptyList(); } return conf.getResolvedConfiguration().getResolvedArtifacts(); }
/** * Resolve all buildscript dependencies of this project and it's subprojects. * * @return All resolved buildscript dependencies. */ protected List<DependencyIdentifier> resolveBuildDependencies() { return getProject().getAllprojects().stream() // .map(project -> project.getBuildscript().getConfigurations().getByName(ScriptHandler.CLASSPATH_CONFIGURATION).getResolvedConfiguration()) // .flatMap(confguration -> confguration.getResolvedArtifacts().stream()) // .map(resolvedArtifact -> resolvedArtifact.getModuleVersion().getId()) // .map(DependencyIdentifier::new) // .distinct() // .filter(this::filterIgnoredDependencies) // .sorted(new DependencyIdentifierComparator()) // .collect(Collectors.toList()); }
/** * Find a resolved artifact in a configuration * * @param project Project to search * @param configuration Configuration to search * @param artifactPredicate Predicate to test artifacts * @return Found element or empty value */ public static Optional<ProjectElement<ResolvedArtifact>> findResolvedArtifact(Project project, Configuration configuration, Predicate<@NonNull ResolvedArtifact> artifactPredicate) { Optional<ProjectElement<ResolvedArtifact>> info = Optional.empty(); for (@NonNull ResolvedArtifact artifact : configuration.getResolvedConfiguration().getResolvedArtifacts()) { if (artifactPredicate.test(artifact)) { info = Optional.of(new ProjectElement<>(project, configuration, artifact)); break; } } return info; }
/** * Resolve all dependencies of all configurations of this project and it's subprojects. * * @return All resolved dependencies. */ protected List<DependencyIdentifier> resolveDependencies() { return getProject().getAllprojects().stream() // all projects .flatMap(project -> project.getConfigurations().stream()) // get all configurations .filter(Configuration::isCanBeResolved) // only if the configuration can be resolved .flatMap(configuration -> configuration.getResolvedConfiguration().getResolvedArtifacts().stream()) // get all artifacts .filter(resolvedArtifact -> !(resolvedArtifact.getId().getComponentIdentifier() instanceof DefaultProjectComponentIdentifier)) .map(resolvedArtifact -> resolvedArtifact.getModuleVersion().getId()) // map to ModuleVersionIdentifier .map(DependencyIdentifier::new) // .distinct() // .filter(this::filterIgnoredDependencies) // .sorted(new DependencyIdentifierComparator()) // .collect(Collectors.toList()); }
private Set<GradleLibrary> getLibraries(String configurationName, LibraryScope scope) { Configuration configuration = (configurationName == null ? null : this.project.getConfigurations().findByName(configurationName)); if (configuration == null) { return null; } Set<GradleLibrary> libraries = new LinkedHashSet<GradleLibrary>(); for (ResolvedArtifact artifact : configuration.getResolvedConfiguration().getResolvedArtifacts()) { libraries.add(new ResolvedArtifactLibrary(artifact, scope)); } libraries.addAll(getLibrariesForFileDependencies(configuration, scope)); return libraries; }
@Override public List<Pom> resolvePoms(List<PomReference> pomReferences, PropertySource properties) { return createPoms(createConfiguration(pomReferences).getResolvedConfiguration().getResolvedArtifacts(), pomReferences, properties); }
/** * Find a resolved artifact in a configuration * * @param project Project to search * @param configuration Configuration to search * @param artifactPredicate Predicate to test artifacts * @return Found element or empty value */ public static Optional<ProjectElement<ResolvedArtifact>> findResolvedArtifact(Project project, Configuration configuration, Predicate<@NonNull ResolvedArtifact> artifactPredicate) { Optional<ProjectElement<ResolvedArtifact>> info = Optional.empty(); for (@NonNull ResolvedArtifact artifact : configuration.getResolvedConfiguration().getResolvedArtifacts()) { if (artifactPredicate.test(artifact)) { info = Optional.of(new ProjectElement<>(project, configuration, artifact)); break; } } return info; }
@Override public InputStream getInputStream() throws Exception { // create a temporary configuration to resolve the file Configuration conf = project.getConfigurations().detachedConfiguration( project.getDependencies().create(dependencyNotation)); ResolvedArtifact artifactId = conf.getResolvedConfiguration().getResolvedArtifacts().iterator().next(); logger.info("Selected recommendation source " + artifactId.getId() + ", you requested " + dependencyNotation); return new FileInputStream(artifactId.getFile()); } };
@Test public void testGetResolvedArtifacts_returnArtifact() { Set<ResolvedArtifact> artifactSet = (Set<ResolvedArtifact>) mock(Set.class); ResolvedConfiguration resolvedConfiguration = mock(ResolvedConfiguration.class); when(resolvedConfiguration.getResolvedArtifacts()).thenReturn(artifactSet); Configuration configuration = mock(Configuration.class); when(configuration.getName()).thenReturn("compile"); when(configuration.isCanBeResolved()).thenReturn(true); when(configuration.getResolvedConfiguration()).thenReturn(resolvedConfiguration); assertThat(dependencyTask.getResolvedArtifacts(configuration), is(artifactSet)); }
private void addConfigurationArtifacts(String[] configurations, Set<URL> urls) throws IOException { for (String configuration : configurations) { getLogger().debug("Adding configuration to classpath: " + configuration); ResolvedConfiguration resolvedConfiguration = getProject().getConfigurations().getByName(configuration).getResolvedConfiguration(); for (ResolvedArtifact artifact : resolvedConfiguration.getResolvedArtifacts()) { URL artifactUrl = artifact.getFile().toURI().toURL(); getLogger().debug("Adding artifact to classpath: " + artifactUrl); urls.add(artifactUrl); } } }
private void collectArtifacts( Configuration configuration, Map<ModuleVersionIdentifier, List<ResolvedArtifact>> artifacts) { Set<ResolvedArtifact> allArtifacts; if (extraModelInfo.getMode() != STANDARD) { allArtifacts = configuration.getResolvedConfiguration().getLenientConfiguration().getArtifacts( Specs.satisfyAll()); } else { allArtifacts = configuration.getResolvedConfiguration().getResolvedArtifacts(); } for (ResolvedArtifact artifact : allArtifacts) { ModuleVersionIdentifier id = artifact.getModuleVersion().getId(); List<ResolvedArtifact> moduleArtifacts = artifacts.get(id); if (moduleArtifacts == null) { moduleArtifacts = Lists.newArrayList(); artifacts.put(id, moduleArtifacts); } if (!moduleArtifacts.contains(artifact)) { moduleArtifacts.add(artifact); } } }
@Test public void testAddArtifacts() { ResolvedConfiguration resolvedConfiguration = spy(ResolvedConfiguration.class); Set<ResolvedArtifact> artifacts = preppareArtifactSet(3); when(resolvedConfiguration.getResolvedArtifacts()).thenReturn(artifacts); Configuration configuration = mock(Configuration.class); when(configuration.isCanBeResolved()).thenReturn(true); when(configuration.getName()).thenReturn("compile"); when(configuration.getResolvedConfiguration()).thenReturn(resolvedConfiguration); dependencyTask.addArtifacts(artifacts); assertThat(dependencyTask.artifactInfos.size(), is(3)); }
@Test public void testGetResolvedArtifacts_ResolveException() { ResolvedConfiguration resolvedConfiguration = mock(ResolvedConfiguration.class); when(resolvedConfiguration.getResolvedArtifacts()).thenThrow(ResolveException.class); Configuration configuration = mock(Configuration.class); when(configuration.getName()).thenReturn("compile"); when(configuration.isCanBeResolved()).thenReturn(true); when(configuration.getResolvedConfiguration()).thenReturn(resolvedConfiguration); assertThat(dependencyTask.getResolvedArtifacts(configuration), is(nullValue())); }
@Test public void testAddArtifacts_willNotAddDuplicate() { ResolvedConfiguration resolvedConfiguration = spy(ResolvedConfiguration.class); Set<ResolvedArtifact> artifacts = preppareArtifactSet(2); when(resolvedConfiguration.getResolvedArtifacts()).thenReturn(artifacts); Configuration configuration = mock(Configuration.class); when(configuration.isCanBeResolved()).thenReturn(true); when(configuration.getName()).thenReturn("compile"); when(configuration.getResolvedConfiguration()).thenReturn(resolvedConfiguration); String[] keySets = new String[] {"location1", "location2"}; dependencyTask.artifactSet = new HashSet<>(Arrays.asList(keySets)); dependencyTask.addArtifacts(artifacts); assertThat(dependencyTask.artifactInfos.size(), is(1)); }
private void addConfigurationArtifacts(String[] configurations, Set<URL> urls) throws IOException { getLogger().debug("running: addConfigurationArtifacts()"); for (String configuration : configurations) { getLogger().debug("Adding configuration to classpath: " + configuration); ResolvedConfiguration resolvedConfiguration = getProject().getConfigurations().getByName(configuration).getResolvedConfiguration(); for (ResolvedArtifact artifact : resolvedConfiguration.getResolvedArtifacts()) { URL artifactUrl = artifact.getFile().toURI().toURL(); getLogger().debug("Adding artifact to classpath: " + artifactUrl); urls.add(artifactUrl); } } }
private void addGosuRuntimeDependencies() { Set<ResolvedArtifact> buildScriptDeps = _project.getBuildscript().getConfigurations().getByName("classpath").getResolvedConfiguration().getResolvedArtifacts(); ResolvedArtifact gosuCore = GosuPlugin.getArtifactWithName("gosu-core", buildScriptDeps); ResolvedArtifact gosuCoreApi = GosuPlugin.getArtifactWithName("gosu-core-api", buildScriptDeps); //inject Gosu jar dependencies into the classpath of the project implementing this plugin _project.getDependencies().add("runtime", gosuCore.getModuleVersion().getId().toString()); _project.getDependencies().add("compile", gosuCoreApi.getModuleVersion().getId().toString()); }
@Nullable private String getJacocoVersion() { Project candidateProject = project; boolean shouldFailWithException = false; while (candidateProject != null) { Set<ResolvedArtifact> resolvedArtifacts = candidateProject.getBuildscript().getConfigurations().getByName("classpath") .getResolvedConfiguration().getResolvedArtifacts(); for (ResolvedArtifact artifact : resolvedArtifacts) { ModuleVersionIdentifier moduleVersion = artifact.getModuleVersion().getId(); if ("org.jacoco.core".equals(moduleVersion.getName())) { return moduleVersion.getVersion(); } } if (!resolvedArtifacts.isEmpty()) { // not in the DSL test case, where nothing will have been resolved. shouldFailWithException = true; } candidateProject = candidateProject.getParent(); } if (shouldFailWithException) { throw new IllegalStateException( "Could not find project build script dependency on org.jacoco.core"); } project.getLogger().error( "No resolved dependencies found when searching for the jacoco version."); return DEFAULT_JACOCO_VERSION; }
private DefaultGosuCompileSpec createSpec() { DefaultGosuCompileSpec spec = new DefaultGosuCompileSpecFactory(_compileOptions).create(); Project project = getProject(); spec.setSource(getSource()); //project.files([ "src/main/gosu" ]) spec.setSourceRoots(getSourceRoots()); spec.setDestinationDir(getDestinationDir()); spec.setClasspath(getClasspath()); //Force gosu-core into the classpath. Normally it's a runtime dependency but compilation requires it. Set<ResolvedArtifact> projectDeps = project.getConfigurations().getByName("runtime").getResolvedConfiguration().getResolvedArtifacts(); File gosuCore = GosuPlugin.getArtifactWithName("gosu-core", projectDeps).getFile(); spec.setGosuClasspath( Collections.singletonList( gosuCore ) ); if(LOGGER.isDebugEnabled()) { LOGGER.debug("Gosu Compiler Spec classpath is:"); for(File file : spec.getClasspath()) { LOGGER.debug(file.getAbsolutePath()); } LOGGER.debug("Gosu Compile Spec gosuClasspath is:"); for(File file : spec.getGosuClasspath()) { LOGGER.debug(file.getAbsolutePath()); } } return spec; }