private ProjectComponentIdentifier getReplacementFor(ModuleComponentSelector selector) { ModuleIdentifier candidateId = DefaultModuleIdentifier.newId(selector.getGroup(), selector.getModule()); Collection<ProjectComponentIdentifier> providingProjects = replacementMap.get(candidateId); if (providingProjects.isEmpty()) { LOGGER.info("Found no composite build substitute for module '" + candidateId + "'."); return null; } if (providingProjects.size() == 1) { ProjectComponentIdentifier match = providingProjects.iterator().next(); LOGGER.info("Found project '" + match + "' as substitute for module '" + candidateId + "'."); return match; } SortedSet<String> sortedProjects = Sets.newTreeSet(CollectionUtils.collect(providingProjects, new Transformer<String, ProjectComponentIdentifier>() { @Override public String transform(ProjectComponentIdentifier projectComponentIdentifier) { return projectComponentIdentifier.getDisplayName(); } })); String failureMessage = String.format("Module version '%s' is not unique in composite: can be provided by %s.", selector.getDisplayName(), sortedProjects); throw new ModuleVersionResolveException(selector, failureMessage); }
private ImmutableMap<File, String> calculate() { ImmutableMap.Builder<File, String> files = ImmutableMap.builder(); for (ResolvedArtifact artifact : getResolvedArtifacts()) { boolean isProject = artifact.getId().getComponentIdentifier() instanceof ProjectComponentIdentifier; if (isProject) { // rename project dependencies ProjectComponentIdentifier projectComponentIdentifier = (ProjectComponentIdentifier) artifact.getId().getComponentIdentifier(); files.put(artifact.getFile(), renameForProject(projectComponentIdentifier, artifact.getFile())); } else { boolean isExternalModule = artifact.getId().getComponentIdentifier() instanceof ModuleComponentIdentifier; if (isExternalModule) { ModuleComponentIdentifier moduleComponentIdentifier = (ModuleComponentIdentifier) artifact.getId().getComponentIdentifier(); files.put(artifact.getFile(), renameForModule(moduleComponentIdentifier, artifact.getFile())); } else { // don't rename other types of dependencies files.put(artifact.getFile(), artifact.getFile().getName()); } } } return files.build(); }
/** * 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; }); }); }
public void write(Encoder encoder, ComponentIdentifier value) throws IOException { if(value == null) { throw new IllegalArgumentException("Provided component identifier may not be null"); } if(value instanceof DefaultModuleComponentIdentifier) { ModuleComponentIdentifier moduleComponentIdentifier = (ModuleComponentIdentifier)value; encoder.writeByte(Implementation.MODULE.getId()); encoder.writeString(moduleComponentIdentifier.getGroup()); encoder.writeString(moduleComponentIdentifier.getModule()); encoder.writeString(moduleComponentIdentifier.getVersion()); } else if(value instanceof DefaultProjectComponentIdentifier) { ProjectComponentIdentifier projectComponentIdentifier = (ProjectComponentIdentifier)value; encoder.writeByte(Implementation.BUILD.getId()); encoder.writeString(projectComponentIdentifier.getProjectPath()); } else { throw new IllegalArgumentException("Unsupported component identifier class: " + value.getClass()); } }
@Override public void resolve(DependencyMetadata dependency, BuildableComponentIdResolveResult result) { if (dependency.getSelector() instanceof ProjectComponentSelector) { ProjectComponentSelector selector = (ProjectComponentSelector) dependency.getSelector(); ProjectComponentIdentifier project = componentIdentifierFactory.createProjectComponentIdentifier(selector); LocalComponentMetadata componentMetaData = localComponentRegistry.getComponent(project); if (componentMetaData == null) { result.failed(new ModuleVersionResolveException(selector, project + " not found.")); } else { result.resolved(componentMetaData); } } }
@Override public void resolve(ComponentIdentifier identifier, ComponentOverrideMetadata componentOverrideMetadata, BuildableComponentResolveResult result) { if (identifier instanceof ProjectComponentIdentifier) { ProjectComponentIdentifier projectId = (ProjectComponentIdentifier) identifier; LocalComponentMetadata componentMetaData = localComponentRegistry.getComponent(projectId); if (componentMetaData == null) { result.failed(new ModuleVersionResolveException(DefaultProjectComponentSelector.newSelector(projectId), projectId + " not found.")); } else { result.resolved(componentMetaData); } } }
@Override public LocalComponentMetadata getComponent(ProjectComponentIdentifier projectIdentifier) { LocalComponentMetadata metaData = projects.get(projectIdentifier); if (metaData !=null) { return metaData; } for (LocalComponentProvider provider : providers) { LocalComponentMetadata componentMetaData = provider.getComponent(projectIdentifier); if (componentMetaData != null) { projects.putIfAbsent(projectIdentifier, componentMetaData); return componentMetaData; } } return null; }
@Override public Iterable<LocalComponentArtifactMetadata> getAdditionalArtifacts(ProjectComponentIdentifier project) { for (LocalComponentProvider provider : providers) { Iterable<LocalComponentArtifactMetadata> artifacts = provider.getAdditionalArtifacts(project); if (artifacts != null) { return artifacts; } } return Collections.emptyList(); }
public LocalComponentMetadata getComponent(ProjectComponentIdentifier projectIdentifier) { if (!isLocalProject(projectIdentifier)) { return null; } ProjectInternal project = projectRegistry.getProject(projectIdentifier.getProjectPath()); if (project == null) { return null; } return getLocalComponentMetaData(project); }
@Override public void registerAdditionalArtifact(ProjectComponentIdentifier project, LocalComponentArtifactMetadata artifact) { if (!isLocalProject(project)) { return; } registeredArtifacts.put(project.getProjectPath(), artifact); }
@Override public Iterable<LocalComponentArtifactMetadata> getAdditionalArtifacts(ProjectComponentIdentifier projectIdentifier) { if (!isLocalProject(projectIdentifier)) { return null; } String projectPath = projectIdentifier.getProjectPath(); if (registeredArtifacts.containsKey(projectPath)) { return registeredArtifacts.get(projectPath); } return null; }
@Override public <T extends ComponentResolutionState> T select(Collection<? extends T> candidates) { // the collection will only be initialized if more than one project candidate is found Collection<T> projectCandidates = null; T foundProjectCandidate = null; // fine one or more project dependencies among conflicting modules for(T candidate: candidates) { ComponentResolveMetadata metaData = candidate.getMetaData(); if (metaData!=null && metaData.getComponentId() instanceof ProjectComponentIdentifier) { if (foundProjectCandidate==null) { // found the first project dependency foundProjectCandidate = candidate; } else { // found more than one if (projectCandidates==null) { projectCandidates = new ArrayList<T>(); projectCandidates.add(foundProjectCandidate); } projectCandidates.add(candidate); } } } // if more than one conflicting project dependencies // let the delegate resolver select among them if (projectCandidates!=null) { return delegate.select(projectCandidates); } // if found only one project dependency - return it, otherwise call the next resolver return foundProjectCandidate!=null ? foundProjectCandidate : delegate.select(candidates); }
@Override public void visitArtifacts(DependencyGraphNode from, DependencyGraphNode to, ArtifactSet artifacts) { artifactSets.put(artifacts.getId(), artifacts); // Don't collect build dependencies if not required if (!buildProjectDependencies) { return; } if (buildableArtifactSets.contains(artifacts.getId())) { return; } // Collect the build dependencies in 2 steps: collect the artifact sets while traversing and at the end of traversal unpack the build dependencies for each // We need to discard the artifact sets to avoid keeping strong references ConfigurationMetadata configurationMetadata = to.getMetadata(); if (!(configurationMetadata instanceof LocalConfigurationMetadata)) { return; } if (from.getOwner().getComponentId() instanceof ProjectComponentIdentifier) { // This is here to attempt to leave out build dependencies that would cause a cycle in the task graph for the current build, so that the cross-build cycle detection kicks in. It's not fully correct ProjectComponentIdentifier incomingId = (ProjectComponentIdentifier) from.getOwner().getComponentId(); if (!incomingId.getBuild().isCurrentBuild()) { return; } } buildableArtifactSets.add(artifacts.getId()); }
public void write(Encoder encoder, ComponentIdentifier value) throws IOException { if (value == null) { throw new IllegalArgumentException("Provided component identifier may not be null"); } Implementation implementation = resolveImplementation(value); encoder.writeByte(implementation.getId()); if (implementation == Implementation.MODULE) { ModuleComponentIdentifier moduleComponentIdentifier = (ModuleComponentIdentifier) value; encoder.writeString(moduleComponentIdentifier.getGroup()); encoder.writeString(moduleComponentIdentifier.getModule()); encoder.writeString(moduleComponentIdentifier.getVersion()); } else if (implementation == Implementation.BUILD) { ProjectComponentIdentifier projectComponentIdentifier = (ProjectComponentIdentifier) value; BuildIdentifier build = projectComponentIdentifier.getBuild(); buildIdentifierSerializer.write(encoder, build); encoder.writeString(projectComponentIdentifier.getProjectPath()); } else if (implementation == Implementation.LIBRARY) { LibraryBinaryIdentifier libraryIdentifier = (LibraryBinaryIdentifier) value; encoder.writeString(libraryIdentifier.getProjectPath()); encoder.writeString(libraryIdentifier.getLibraryName()); encoder.writeString(libraryIdentifier.getVariant()); } else { throw new IllegalStateException("Unsupported implementation type: " + implementation); } }
private Implementation resolveImplementation(ComponentIdentifier value) { Implementation implementation; if (value instanceof ModuleComponentIdentifier) { implementation = Implementation.MODULE; } else if (value instanceof ProjectComponentIdentifier) { implementation = Implementation.BUILD; } else if (value instanceof LibraryBinaryIdentifier) { implementation = Implementation.LIBRARY; } else { throw new IllegalArgumentException("Unsupported component identifier class: " + value.getClass()); } return implementation; }
@Override public void visitNode(DependencyGraphNode resolvedConfiguration) { ComponentIdentifier componentId = resolvedConfiguration.getOwner().getComponentId(); if (!rootId.equals(componentId) && componentId instanceof ProjectComponentIdentifier) { resolvedProjectConfigurations.add(new DefaultResolvedProjectConfiguration((ProjectComponentIdentifier) componentId, resolvedConfiguration.getResolvedConfigurationId().getConfiguration())); } }
@Override public ProjectComponentIdentifier createProjectComponentIdentifier(ProjectComponentSelector selector) { BuildIdentifier currentBuild = buildIdentity.getCurrentBuild(); if (selector.getBuildName().equals(currentBuild.getName())) { return new DefaultProjectComponentIdentifier(currentBuild, selector.getProjectPath()); } return new DefaultProjectComponentIdentifier(new DefaultBuildIdentifier(selector.getBuildName()), selector.getProjectPath()); }
/** * Finds an IDE metadata artifact with the specified type. Does not execute tasks to build the artifact. * * IDE metadata artifacts are registered by IDE plugins via {@link org.gradle.api.internal.artifacts.ivyservice.projectmodule.ProjectLocalComponentProvider#registerAdditionalArtifact(org.gradle.api.artifacts.component.ProjectComponentIdentifier, org.gradle.internal.component.local.model.LocalComponentArtifactMetadata)} */ public LocalComponentArtifactMetadata findArtifact(ProjectComponentIdentifier project, String type) { for (LocalComponentArtifactMetadata artifactMetaData : registry.getAdditionalArtifacts(project)) { if (artifactMetaData.getName().getType().equals(type)) { return artifactMetaData; } } return null; }
/** * Finds an IDE metadata artifact with the specified type, and executes tasks to build the artifact file. */ public File buildArtifactFile(ProjectComponentIdentifier project, String type) { LocalComponentArtifactMetadata artifactMetaData = findArtifact(project, type); if (artifactMetaData == null) { return null; } artifactBuilder.build(artifactMetaData); return artifactMetaData.getFile(); }
@Override public Set<ProjectComponentIdentifier> getAllProjects() { for (IncludedBuild build : includedBuilds.getBuilds()) { ensureRegistered((IncludedBuildInternal) build); } return projectMetadata.keySet(); }
public void register(ProjectComponentIdentifier project, LocalComponentMetadata localComponentMetadata, File projectDirectory) { if (projectMetadata.containsKey(project)) { String failureMessage = StringUtils.capitalize(project.getDisplayName()) +" is not unique in composite."; throw new GradleException(failureMessage); } projectMetadata.put(project, new RegisteredProject(localComponentMetadata, projectDirectory)); }
private RegisteredProject getRegisteredProject(ProjectComponentIdentifier project) { ensureRegistered(project); RegisteredProject registeredProject = projectMetadata.get(project); if (registeredProject == null) { throw new IllegalStateException(String.format("Requested %s which was never registered", project)); } return registeredProject; }
private void ensureRegistered(ProjectComponentIdentifier projectComponentIdentifier) { if (projectMetadata.containsKey(projectComponentIdentifier)) { return; } IncludedBuildInternal build = getBuild(projectComponentIdentifier); if (build == null) { return; } ensureRegistered(build); }
private void registerProject(IncludedBuild build, ProjectInternal project) { LocalComponentRegistry localComponentRegistry = project.getServices().get(LocalComponentRegistry.class); ProjectComponentIdentifier originalIdentifier = newProjectId(project); DefaultLocalComponentMetadata originalComponent = (DefaultLocalComponentMetadata) localComponentRegistry.getComponent(originalIdentifier); ProjectComponentIdentifier componentIdentifier = newProjectId(build, project.getPath()); context.registerSubstitution(originalComponent.getId(), componentIdentifier); }
private void registerProject(IncludedBuild build, ProjectInternal project) { LocalComponentRegistry localComponentRegistry = project.getServices().get(LocalComponentRegistry.class); ProjectComponentIdentifier originalIdentifier = newProjectId(project); DefaultLocalComponentMetadata originalComponent = (DefaultLocalComponentMetadata) localComponentRegistry.getComponent(originalIdentifier); ProjectComponentIdentifier componentIdentifier = newProjectId(build, project.getPath()); LocalComponentMetadata compositeComponent = createCompositeCopy(build, componentIdentifier, originalComponent); context.register(componentIdentifier, compositeComponent, project.getProjectDir()); for (LocalComponentArtifactMetadata artifactMetaData : localComponentRegistry.getAdditionalArtifacts(originalIdentifier)) { context.registerAdditionalArtifact(componentIdentifier, createCompositeCopy(componentIdentifier, artifactMetaData)); } }
private LocalComponentMetadata createCompositeCopy(IncludedBuild build, ProjectComponentIdentifier componentIdentifier, DefaultLocalComponentMetadata originalComponentMetadata) { DefaultLocalComponentMetadata compositeComponentMetadata = new DefaultLocalComponentMetadata(originalComponentMetadata.getId(), componentIdentifier, originalComponentMetadata.getStatus(), originalComponentMetadata.getAttributesSchema()); for (String configurationName : originalComponentMetadata.getConfigurationNames()) { LocalConfigurationMetadata originalConfiguration = originalComponentMetadata.getConfiguration(configurationName); compositeComponentMetadata.addConfiguration(configurationName, originalConfiguration.getDescription(), originalConfiguration.getExtendsFrom(), originalConfiguration.getHierarchy(), originalConfiguration.isVisible(), originalConfiguration.isTransitive(), originalConfiguration.getAttributes(), originalConfiguration.isCanBeConsumed(), originalConfiguration.isCanBeResolved()); Set<? extends LocalComponentArtifactMetadata> artifacts = originalConfiguration.getArtifacts(); for (LocalComponentArtifactMetadata originalArtifact : artifacts) { File artifactFile = originalArtifact.getFile(); Set<String> targetTasks = getArtifactTasks(originalArtifact); CompositeProjectComponentArtifactMetadata artifact = new CompositeProjectComponentArtifactMetadata(componentIdentifier, originalArtifact.getName(), artifactFile, targetTasks); compositeComponentMetadata.addArtifact(configurationName, artifact); } } for (LocalOriginDependencyMetadata dependency : originalComponentMetadata.getDependencies()) { if (dependency.getSelector() instanceof ProjectComponentSelector) { ProjectComponentSelector requested = (ProjectComponentSelector) dependency.getSelector(); dependency = dependency.withTarget(DefaultProjectComponentSelector.newSelector(build, requested)); } compositeComponentMetadata.addDependency(dependency); } for (Exclude exclude : originalComponentMetadata.getExcludeRules()) { compositeComponentMetadata.addExclude(exclude); } return compositeComponentMetadata; }
@Override public void execute(DependencySubstitution sub) { DependencySubstitutionInternal dependencySubstitution = (DependencySubstitutionInternal) sub; // Use the result of previous rules as the input for dependency substitution ComponentSelector requested = dependencySubstitution.getTarget(); if (requested instanceof ModuleComponentSelector) { ModuleComponentSelector selector = (ModuleComponentSelector) requested; ProjectComponentIdentifier replacement = getReplacementFor(selector); if (replacement != null) { dependencySubstitution.useTarget( DefaultProjectComponentSelector.newSelector(replacement), VersionSelectionReasons.COMPOSITE_BUILD); } } }
private static void registerEclipseArtifacts(Project project) { ProjectLocalComponentProvider projectComponentProvider = ((ProjectInternal) project).getServices().get(ProjectLocalComponentProvider.class); ProjectComponentIdentifier projectId = newProjectId(project); String projectName = project.getExtensions().getByType(EclipseModel.class).getProject().getName(); projectComponentProvider.registerAdditionalArtifact(projectId, createArtifact("project", projectId, projectName, project)); projectComponentProvider.registerAdditionalArtifact(projectId, createArtifact("classpath", projectId, projectName, project)); }
private static LocalComponentArtifactMetadata createArtifact(String extension, ProjectComponentIdentifier projectId, String projectName, Project project) { File projectFile = new File(project.getProjectDir(), "." + extension); Task byName = project.getTasks().getByName("eclipseProject"); String type = "eclipse." + extension; PublishArtifact publishArtifact = new DefaultPublishArtifact(projectName, extension, type, null, null, projectFile, byName); return new PublishArtifactLocalArtifactMetadata(projectId, publishArtifact); }
/** * Gets IDE project dependencies. * * @param configuration Configuration * @param project Project * @return IDE project dependencies */ public List<IdeProjectDependency> getIdeProjectDependencies(Configuration configuration, Project project) { ResolutionResult result = getIncomingResolutionResult(configuration); final Set<ResolvedComponentResult> projectComponents = CollectionUtils.filter(result.getAllComponents(), new Spec<ResolvedComponentResult>() { @Override public boolean isSatisfiedBy(ResolvedComponentResult element) { return element.getId() instanceof ProjectComponentIdentifier; } }); List<IdeProjectDependency> ideProjectDependencies = new ArrayList<IdeProjectDependency>(); ProjectComponentIdentifier thisProjectId = DefaultProjectComponentIdentifier.newProjectId(project); for (ResolvedComponentResult projectComponent : projectComponents) { ProjectComponentIdentifier projectId = (ProjectComponentIdentifier) projectComponent.getId(); if (thisProjectId.equals(projectId)) { continue; } if (!projectId.getBuild().isCurrentBuild()) { // Don't have access to the ProjectInstance: we can't use it to determine the name. ideProjectDependencies.add(new IdeProjectDependency(projectId)); } else { Project resolvedProject = project.project(projectId.getProjectPath()); ideProjectDependencies.add(new IdeProjectDependency(projectId, resolvedProject.getName())); } } return ideProjectDependencies; }
private static String determineProjectName(ProjectComponentIdentifier projectId) { assert !projectId.getBuild().isCurrentBuild(); String projectPath = projectId.getProjectPath(); if (projectPath.equals(":")) { return projectId.getBuild().getName(); } int index = projectPath.lastIndexOf(':'); return projectPath.substring(index + 1); }
private void includeModulesFromComposite(Project xmlProject) { PathFactory pathFactory = getPathFactory(); Set<ProjectComponentIdentifier> projectsInComposite = compositeContext.getAllProjects(); for (ProjectComponentIdentifier otherProjectId : projectsInComposite) { File imlFile = moduleToProjectMapper.buildArtifactFile(otherProjectId, "iml"); if (imlFile != null) { xmlProject.getModulePaths().add(pathFactory.relativePath("PROJECT_DIR", imlFile)); } } }
private static LocalComponentArtifactMetadata createImlArtifact(ProjectComponentIdentifier projectId, Project project) { String moduleName = project.getExtensions().getByType(IdeaModel.class).getModule().getName(); File imlFile = new File(project.getProjectDir(), moduleName + ".iml"); Task byName = project.getTasks().getByName("ideaModule"); PublishArtifact publishArtifact = new DefaultPublishArtifact(moduleName, "iml", "iml", null, null, imlFile, byName); return new PublishArtifactLocalArtifactMetadata(projectId, publishArtifact); }
static String renameForProject(ProjectComponentIdentifier id, File file) { String fileName = file.getName(); if (shouldBeRenamed(file)) { String projectPath = id.getProjectPath(); projectPath = projectPathToSafeFileName(projectPath); return maybePrefix(projectPath, file); } return fileName; }
@Override public FileCollectionInternal createDelegate() { ImmutableSet.Builder<File> files = ImmutableSet.builder(); for (ResolvedArtifact artifact : configuration.getResolvedConfiguration().getResolvedArtifacts()) { if ((artifact.getId().getComponentIdentifier() instanceof ProjectComponentIdentifier) == matchProjectComponents) { files.add(artifact.getFile()); } } return new SimpleFileCollection(files.build()); }
private boolean checkForExclusion(VariantDependencies configDependencies, ModuleVersionIdentifier moduleVersion, ResolvedComponentResult resolvedComponentResult) { if (configDependencies.getChecker().checkForExclusion(moduleVersion) || (apDependencies != null && apDependencies .hasSameResolvedDependency( moduleVersion) && !(resolvedComponentResult .getId() instanceof ProjectComponentIdentifier))) { return true; } return false; }
@Override protected boolean checkForExclusion(@NonNull VariantDependencies configDependencies, ModuleVersionIdentifier moduleVersion, ResolvedComponentResult resolvedComponentResult) { return super.checkForExclusion(configDependencies, moduleVersion, resolvedComponentResult) || ( apDependencies != null && apDependencies.hasSameResolvedDependency(moduleVersion) && !(resolvedComponentResult.getId() instanceof ProjectComponentIdentifier)); }
/** * Gets IDE project dependencies. * * @param configuration Configuration * @param project Project * @return IDE project dependencies */ public List<IdeProjectDependency> getIdeProjectDependencies(Configuration configuration, Project project) { ResolutionResult result = getIncomingResolutionResult(configuration); List<ResolvedComponentResult> projectComponents = findAllResolvedDependencyResults(result.getRoot().getDependencies(), ProjectComponentIdentifier.class); List<IdeProjectDependency> ideProjectDependencies = new ArrayList<IdeProjectDependency>(); for (ResolvedComponentResult projectComponent : projectComponents) { Project resolvedProject = project.project(((ProjectComponentIdentifier) projectComponent.getId()).getProjectPath()); ideProjectDependencies.add(new IdeProjectDependency(configuration, resolvedProject)); } return ideProjectDependencies; }