Add group discriminant in case of conflict
Prior to this commit, the repackage goal silently ignored the case of two libraries having the same name and version but a different group. As a result, the second library was overwriting the first one in the repackaged jar. This commit adds support for custom Library names and updates the Maven and Gradle plugins so that the name includes the group ID when there would otherwise be a duplicate. Fixes gh-1475
This commit is contained in:
committed by
Phillip Webb
parent
449752c9e2
commit
f46fe32264
@@ -75,18 +75,18 @@ class ProjectLibraries implements Libraries {
|
||||
|
||||
@Override
|
||||
public void doWithLibraries(LibraryCallback callback) throws IOException {
|
||||
Set<Library> custom = getLibraries(this.customConfigurationName,
|
||||
Set<GradleLibrary> custom = getLibraries(this.customConfigurationName,
|
||||
LibraryScope.CUSTOM);
|
||||
if (custom != null) {
|
||||
libraries(custom, callback);
|
||||
}
|
||||
else {
|
||||
Set<Library> compile = getLibraries("compile", LibraryScope.COMPILE);
|
||||
Set<GradleLibrary> compile = getLibraries("compile", LibraryScope.COMPILE);
|
||||
|
||||
Set<Library> runtime = getLibraries("runtime", LibraryScope.RUNTIME);
|
||||
Set<GradleLibrary> runtime = getLibraries("runtime", LibraryScope.RUNTIME);
|
||||
runtime = minus(runtime, compile);
|
||||
|
||||
Set<Library> provided = getLibraries(this.providedConfigurationName,
|
||||
Set<GradleLibrary> provided = getLibraries(this.providedConfigurationName,
|
||||
LibraryScope.PROVIDED);
|
||||
if (provided != null) {
|
||||
compile = minus(compile, provided);
|
||||
@@ -99,13 +99,13 @@ class ProjectLibraries implements Libraries {
|
||||
}
|
||||
}
|
||||
|
||||
private Set<Library> getLibraries(String configurationName, LibraryScope scope) {
|
||||
private Set<GradleLibrary> getLibraries(String configurationName, LibraryScope scope) {
|
||||
Configuration configuration = (configurationName == null ? null : this.project
|
||||
.getConfigurations().findByName(configurationName));
|
||||
if (configuration == null) {
|
||||
return null;
|
||||
}
|
||||
Set<Library> libraries = new LinkedHashSet<Library>();
|
||||
Set<GradleLibrary> libraries = new LinkedHashSet<GradleLibrary>();
|
||||
for (ResolvedArtifact artifact : configuration.getResolvedConfiguration()
|
||||
.getResolvedArtifacts()) {
|
||||
libraries.add(new ResolvedArtifactLibrary(artifact, scope));
|
||||
@@ -115,14 +115,14 @@ class ProjectLibraries implements Libraries {
|
||||
return libraries;
|
||||
}
|
||||
|
||||
private Set<Library> getLibrariesForFileDependencies(Configuration configuration,
|
||||
private Set<GradleLibrary> getLibrariesForFileDependencies(Configuration configuration,
|
||||
LibraryScope scope) {
|
||||
Set<Library> libraries = new LinkedHashSet<Library>();
|
||||
Set<GradleLibrary> libraries = new LinkedHashSet<GradleLibrary>();
|
||||
for (Dependency dependency : configuration.getIncoming().getDependencies()) {
|
||||
if (dependency instanceof FileCollectionDependency) {
|
||||
FileCollectionDependency fileDependency = (FileCollectionDependency) dependency;
|
||||
for (File file : fileDependency.resolve()) {
|
||||
libraries.add(new Library(file, scope));
|
||||
libraries.add(new GradleLibrary(fileDependency.getGroup(), file, scope));
|
||||
}
|
||||
}
|
||||
else if (dependency instanceof ProjectDependency) {
|
||||
@@ -134,16 +134,16 @@ class ProjectLibraries implements Libraries {
|
||||
return libraries;
|
||||
}
|
||||
|
||||
private Set<Library> minus(Set<Library> source, Set<Library> toRemove) {
|
||||
private Set<GradleLibrary> minus(Set<GradleLibrary> source, Set<GradleLibrary> toRemove) {
|
||||
if (source == null || toRemove == null) {
|
||||
return source;
|
||||
}
|
||||
Set<File> filesToRemove = new HashSet<File>();
|
||||
for (Library library : toRemove) {
|
||||
for (GradleLibrary library : toRemove) {
|
||||
filesToRemove.add(library.getFile());
|
||||
}
|
||||
Set<Library> result = new LinkedHashSet<Library>();
|
||||
for (Library library : source) {
|
||||
Set<GradleLibrary> result = new LinkedHashSet<GradleLibrary>();
|
||||
for (GradleLibrary library : source) {
|
||||
if (!filesToRemove.contains(library.getFile())) {
|
||||
result.add(library);
|
||||
}
|
||||
@@ -151,24 +151,63 @@ class ProjectLibraries implements Libraries {
|
||||
return result;
|
||||
}
|
||||
|
||||
private void libraries(Set<Library> libraries, LibraryCallback callback)
|
||||
private void libraries(Set<GradleLibrary> libraries, LibraryCallback callback)
|
||||
throws IOException {
|
||||
if (libraries != null) {
|
||||
for (Library library : libraries) {
|
||||
Set<String> duplicates = getDuplicates(libraries);
|
||||
for (GradleLibrary library : libraries) {
|
||||
library.setIncludeGroupName(duplicates.contains(library.getName()));
|
||||
callback.library(library);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Set<String> getDuplicates(Set<GradleLibrary> libraries) {
|
||||
Set<String> duplicates = new HashSet<String>();
|
||||
Set<String> seen = new HashSet<String>();
|
||||
for (GradleLibrary library : libraries) {
|
||||
if (library.getFile() != null && !seen.add(library.getFile().getName())) {
|
||||
duplicates.add(library.getFile().getName());
|
||||
}
|
||||
}
|
||||
return duplicates;
|
||||
}
|
||||
|
||||
private class GradleLibrary extends Library {
|
||||
|
||||
private final String group;
|
||||
|
||||
private boolean includeGroupName;
|
||||
|
||||
public GradleLibrary(String group, File file, LibraryScope scope) {
|
||||
super(file, scope);
|
||||
this.group = group;
|
||||
}
|
||||
|
||||
public void setIncludeGroupName(boolean includeGroupName) {
|
||||
this.includeGroupName = includeGroupName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
String name = super.getName();
|
||||
if(this.includeGroupName && this.group != null) {
|
||||
name = this.group + "-" + name;
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Adapts a {@link ResolvedArtifact} to a {@link Library}.
|
||||
*/
|
||||
private class ResolvedArtifactLibrary extends Library {
|
||||
private class ResolvedArtifactLibrary extends GradleLibrary {
|
||||
|
||||
private final ResolvedArtifact artifact;
|
||||
|
||||
public ResolvedArtifactLibrary(ResolvedArtifact artifact, LibraryScope scope) {
|
||||
super(artifact.getFile(), scope);
|
||||
super(null, artifact.getFile(), scope);
|
||||
this.artifact = artifact;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user