/** * Collects all project dependencies of the style "compile project(':mylib')" for any kind of * configuration [compile, runtime, etc]. It potentially will collect common test libraries in * configs like [test, integrationTest, etc], but it's either that or filter based on a * configuration containing the word "test" which feels dangerous. * * @param project this project we are containerizing * @return a list of projects that this project depends on. */ @VisibleForTesting static List<Project> getProjectDependencies(Project project) { return project .getConfigurations() .stream() .map(Configuration::getDependencies) .flatMap(DependencySet::stream) .filter(ProjectDependency.class::isInstance) .map(ProjectDependency.class::cast) .map(ProjectDependency::getDependencyProject) .collect(Collectors.toList()); }
/** * 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()); }
static Set<String> getResolvedModuleIdentifiers(Project project) { return project.getConfigurations().stream() .filter(Configuration::isCanBeResolved) .flatMap(configuration -> { try { ResolutionResult resolutionResult = configuration.getIncoming().getResolutionResult(); return resolutionResult .getAllComponents() .stream() .map(result -> result.getId()) .filter(cid -> !cid.equals(resolutionResult.getRoot().getId())) // remove the project .filter(cid -> cid instanceof ModuleComponentIdentifier) .map(mcid -> ((ModuleComponentIdentifier) mcid).getModuleIdentifier()) .map(mid -> mid.getGroup() + ":" + mid.getName()); } catch (Exception e) { throw new RuntimeException(String.format("Error during resolution of the dependency graph of " + "configuration %s", configuration), e); } }) .collect(Collectors.toSet()); } }
/** * Create Eclipse annotations tasks * * @param tasks Task container * @param configurations Container to access configurations * @param buildDir Build directory */ @Mutate public void createEclipseAnnotationsTasks(ModelMap<Task> tasks, ConfigurationContainer configurations, @Path("buildDir") File buildDir) { tasks.create("eclipseAnnotations", EclipseAnnotationsTask.class, t -> { t.setDescription("Generates external nullability annotations for dependencies."); t.setGroup("IDE"); ConventionMapping parameters = t.getConventionMapping(); parameters.map("jars", () -> { Set<File> jars = configurations.stream() .filter(c -> c.isCanBeResolved() && !c.getName().equals(JavaConfigPlugin.ANNOTATIONS_CONFIGURATION)) .map(c -> c.getResolvedConfiguration().getLenientConfiguration()) .flatMap(c -> c.getArtifacts().stream() .filter(a -> !(a.getId() .getComponentIdentifier() instanceof ProjectComponentIdentifier) && a.getType().equals("jar")) .map(a -> a.getFile())) .collect(Collectors.toSet()); return jars; }); }); }
/** * Create Eclipse annotations tasks * * @param tasks Task container * @param configurations Container to access configurations * @param buildDir Build directory */ @Mutate public void createEclipseAnnotationsTasks(ModelMap<Task> tasks, ConfigurationContainer configurations, @Path("buildDir") File buildDir) { tasks.create("eclipseAnnotations", EclipseAnnotationsTask.class, t -> { t.setDescription("Generates external nullability annotations for dependencies."); t.setGroup("IDE"); ConventionMapping parameters = t.getConventionMapping(); parameters.map("jars", () -> { Set<File> jars = configurations.stream() .filter(c -> c.isCanBeResolved() && !c.getName().equals(JavaConfigPlugin.ANNOTATIONS_CONFIGURATION)) .map(c -> c.getResolvedConfiguration().getLenientConfiguration()) .flatMap(c -> c.getArtifacts().stream() .filter(a -> !(a.getId() .getComponentIdentifier() instanceof ProjectComponentIdentifier) && a.getType().equals("jar")) .map(a -> a.getFile())) .collect(Collectors.toSet()); return jars; }); }); }
@Override protected void applyOnce(Project project) { ProjectDepsExtension extension = project.getExtensions().create(ProjectDepsExtension.NAME, ProjectDepsExtension.class); EclipseProjectPlugin.modifyEclipseProject(project, eclipseModel -> { // find the project's referenced projects and reference them explicitly in the eclipse model Task prepareEclipse = project.task("prepareEclipse"); prepareEclipse.doLast(task -> { Set<String> referencedProjects = eclipseModel.getProject().getReferencedProjects(); project.getConfigurations().stream() .flatMap(config -> config.getDependencies().stream()) .filter(dep -> dep instanceof ProjectDependency) .forEach(dep -> { referencedProjects.add(dep.getName()); }); }); // it's needed for generating the eclipseClasspath and eclipseProject Iterables.getOnlyElement(project.getTasksByName("eclipseClasspath", false)).dependsOn(prepareEclipse); Iterables.getOnlyElement(project.getTasksByName("eclipseProject", false)).dependsOn(prepareEclipse); // create robust classpath entries for all referenced projects eclipseModel.getClasspath().getFile().getXmlTransformer().addAction(xmlProvider -> { modifyClasspath(xmlProvider.asNode(), eclipseModel, extension); }); }); }