Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix circular dependencies when project have the same name
Before this commit, during dependency resolution, a synthetic module version identifier was generated by project, using the group and name of the project. However, it's possible for a project in gradle to have the same name as another in the same build, leading to duplicates. In this case the projects were mixed together and lead to a circular dependency. This commit fixes the problem by making sure we generate distinct module version identifiers for such projects, by using the full project path as the name instead of the short name. This also makes it possible to publish valid publications when using the maven or ivy publish plugins. However, we detect this problem early and warn the user that they should overwrite the project identity in this case.
- Loading branch information
Showing
15 changed files
with
875 additions
and
94 deletions.
There are no files selected for viewing
158 changes: 158 additions & 0 deletions
158
...cts/core/src/main/java/org/gradle/api/internal/artifacts/DefaultProjectModuleFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
/* | ||
* Copyright 2020 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.gradle.api.internal.artifacts; | ||
|
||
import com.google.common.base.Splitter; | ||
import com.google.common.collect.Lists; | ||
import com.google.common.collect.Maps; | ||
import org.gradle.api.Project; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
public class DefaultProjectModuleFactory implements ProjectModuleFactory { | ||
private static final Splitter SPLITTER = Splitter.on(':') | ||
.omitEmptyStrings(); | ||
|
||
private final Map<Project, Module> projectToModule = Maps.newConcurrentMap(); | ||
|
||
public DefaultProjectModuleFactory() { | ||
} | ||
|
||
private List<Project> findDuplicates(Project project) { | ||
Set<Project> projects = project.getRootProject().getAllprojects(); | ||
String current = toGroupAndArtifact(project); | ||
List<Project> duplicates = null; | ||
for (Project projectIdentifier : projects) { | ||
if (project != projectIdentifier) { | ||
String ga = toGroupAndArtifact(projectIdentifier); | ||
if (current.equals(ga)) { | ||
if (duplicates == null) { | ||
duplicates = Lists.newArrayList(); | ||
} | ||
duplicates.add(projectIdentifier); | ||
} | ||
} | ||
} | ||
return duplicates == null ? Collections.emptyList() : duplicates; | ||
} | ||
|
||
private static String toGroupAndArtifact(Project projectIdentifier) { | ||
return projectIdentifier.getGroup() + ":" + projectIdentifier.getName(); | ||
} | ||
|
||
@Override | ||
public Module getModule(Project project) { | ||
return projectToModule.computeIfAbsent(project, this::createId); | ||
} | ||
|
||
private Module createId(Project project) { | ||
return new DynamicDeduplicatingModuleProjectIdentifier(project); | ||
} | ||
|
||
private abstract class AbstractProjectBackedModule implements ProjectBackedModule { | ||
|
||
private final Project project; | ||
|
||
@Override | ||
public Project getProject() { | ||
return project; | ||
} | ||
|
||
public AbstractProjectBackedModule(Project project) { | ||
this.project = project; | ||
} | ||
|
||
@Override | ||
public List<Project> getProjectsWithSameCoordinates() { | ||
List<Project> ids = findDuplicates(project); | ||
if (ids.isEmpty()) { | ||
return Collections.emptyList(); | ||
} | ||
return ids.stream() | ||
.filter(id -> id != project) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
@Override | ||
public String getGroup() { | ||
return String.valueOf(project.getGroup()); | ||
} | ||
|
||
@Override | ||
public String getVersion() { | ||
return project.getVersion().toString(); | ||
} | ||
|
||
@Override | ||
public String getStatus() { | ||
return project.getStatus().toString(); | ||
} | ||
|
||
@Override | ||
public String getProjectPath() { | ||
return project.getPath(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
|
||
AbstractProjectBackedModule that = (AbstractProjectBackedModule) o; | ||
|
||
if (!project.equals(that.project)) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return project.hashCode(); | ||
} | ||
} | ||
|
||
private class DynamicDeduplicatingModuleProjectIdentifier extends AbstractProjectBackedModule { | ||
private final Project project; | ||
|
||
private DynamicDeduplicatingModuleProjectIdentifier(Project project) { | ||
super(project); | ||
this.project = project; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
List<Project> duplicates = findDuplicates(project); | ||
if (duplicates.isEmpty()) { | ||
return project.getName(); | ||
} | ||
List<String> strings = SPLITTER.splitToList(project.getPath()); | ||
if (strings.size() <= 1) { | ||
return project.getName(); | ||
} | ||
return String.join("-", strings.subList(0, strings.size() - 1)) + "-" + project.getName(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
subprojects/core/src/main/java/org/gradle/api/internal/artifacts/ProjectModuleFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright 2020 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.gradle.api.internal.artifacts; | ||
|
||
import org.gradle.api.Project; | ||
|
||
public interface ProjectModuleFactory { | ||
Module getModule(Project project); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.