Skip to content

Commit

Permalink
Fix project publication coordinates being changed without notice
Browse files Browse the repository at this point in the history
Fixes #12281
  • Loading branch information
melix committed Feb 20, 2020
1 parent f4e1d67 commit a0a662c
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 4 deletions.
Expand Up @@ -578,4 +578,48 @@ $append

}

@Issue("https://github.com/gradle/gradle/issues/12281")
def "shouldn't change publication coordinates if GAV are different"() {
given:
settingsFile << """
rootProject.name='duplicates'
include(':a:module')
project(':a:module').projectDir = file('module1')
include(':b:module')
project(':b:module').projectDir = file('module2')
include(':c:module')
project(':c:module').projectDir = file('module3')
"""

buildFile << """
subprojects {
apply plugin: 'java'
apply plugin: 'ivy-publish'
publishing {
repositories {
ivy { url "${ivyRepo.uri}" }
}
publications {
ivy(IvyPublication) {
version = '1.1'
from components.java
}
}
}
}"""

file("module1/build.gradle") << "group = 'the.group'"
file("module2/build.gradle") << "group = 'the.group'"
file("module3/build.gradle") << "group = 'the.other.group'"

when:
succeeds ':c:module:publishIvyPublicationToIvyRepo'
def module = javaLibrary(ivyRepo.module("the.other.group", "module", "1.1"))

then:
module.assertPublishedAsJavaModule()
module.parsedIvy.organisation == 'the.other.group'
module.parsedModuleMetadata.component.group == 'the.other.group'
}
}
Expand Up @@ -84,6 +84,7 @@ private Module safeProjectCoordinatesProvider(Module module) {
private static final class LazyProjectModuleProvider implements Module {
private final ProjectBackedModule projectBackedModule;
private final AtomicBoolean warned = new AtomicBoolean();
private final AtomicBoolean hasDuplicates = new AtomicBoolean();

private LazyProjectModuleProvider(ProjectBackedModule module) {
this.projectBackedModule = module;
Expand All @@ -104,7 +105,13 @@ public String getGroup() {
@Override
public String getName() {
maybeWarnAboutDuplicates();
return projectBackedModule.getName();
if (hasDuplicates.get()) {
return projectBackedModule.getName();
} else {
// the project name is used for publication, even if internally
// for dependency resolution we use the "de-duplicated" name
return projectBackedModule.getProject().getName();
}
}

@Override
Expand All @@ -121,7 +128,9 @@ private void maybeWarnAboutDuplicates() {
if (!warned.getAndSet(true)) {
Project project = projectBackedModule.getProject();
List<Project> projectsWithSameId = projectBackedModule.getProjectsWithSameCoordinates();
if (!projectsWithSameId.isEmpty() && DefaultProjectModuleFactory.isDuplicateDetectionEnabled()) {
boolean duplicates = !projectsWithSameId.isEmpty() && DefaultProjectModuleFactory.isDuplicateDetectionEnabled();
hasDuplicates.set(duplicates);
if (duplicates) {
StringBuilder sb = new StringBuilder();
sb.append("Project ")
.append(project.getPath())
Expand Down
Expand Up @@ -785,4 +785,49 @@ $append
outputDoesNotContain "Project :a:core has the same (groupId, artifactId) as :b:core. You should set both the organisation and module name of the publication or opt out by adding the org.gradle.dependency.duplicate.project.detection system property to 'false'."
}

@Issue("https://github.com/gradle/gradle/issues/12281")
def "shouldn't change publication coordinates if GAV are different"() {
given:
settingsFile << """
rootProject.name='duplicates'
include(':a:module')
project(':a:module').projectDir = file('module1')
include(':b:module')
project(':b:module').projectDir = file('module2')
include(':c:module')
project(':c:module').projectDir = file('module3')
"""

buildFile << """
subprojects {
apply plugin: 'java'
apply plugin: 'maven-publish'
publishing {
repositories {
maven { url "${mavenRepo.uri}" }
}
publications {
maven(MavenPublication) {
version = '1.1'
from components.java
}
}
}
}"""

file("module1/build.gradle") << "group = 'the.group'"
file("module2/build.gradle") << "group = 'the.group'"
file("module3/build.gradle") << "group = 'the.other.group'"

when:
succeeds ':c:module:publishMavenPublicationToMavenRepo'
def module = javaLibrary(mavenRepo.module("the.other.group", "module", "1.1"))

then:
module.assertPublishedAsJavaModule()
module.parsedPom.groupId == 'the.other.group'
module.parsedModuleMetadata.component.group == 'the.other.group'
}

}
Expand Up @@ -275,6 +275,7 @@ private LazyProjectNameProvider safeProjectCoordinatesProvider(Module module) {
private static final class LazyProjectNameProvider {
private final ProjectBackedModule projectBackedModule;
private final AtomicBoolean warned = new AtomicBoolean();
private final AtomicBoolean hasDuplicates = new AtomicBoolean();

private LazyProjectNameProvider(ProjectBackedModule module) {
this.projectBackedModule = module;
Expand All @@ -287,14 +288,22 @@ public String getGroup() {

public String getName() {
maybeWarnAboutDuplicates();
return projectBackedModule.getName();
if (hasDuplicates.get()) {
return projectBackedModule.getName();
} else {
// the project name is used for publication, even if internally
// for dependency resolution we use the "de-duplicated" name
return projectBackedModule.getProject().getName();
}
}

private void maybeWarnAboutDuplicates() {
if (!warned.getAndSet(true)) {
Project project = projectBackedModule.getProject();
List<Project> projectsWithSameId = projectBackedModule.getProjectsWithSameCoordinates();
if (!projectsWithSameId.isEmpty() && DefaultProjectModuleFactory.isDuplicateDetectionEnabled()) {
boolean duplicates = !projectsWithSameId.isEmpty() && DefaultProjectModuleFactory.isDuplicateDetectionEnabled();
hasDuplicates.set(duplicates);
if (duplicates) {
StringBuilder sb = new StringBuilder();
sb.append("Project ")
.append(project.getPath())
Expand Down

0 comments on commit a0a662c

Please sign in to comment.