Skip to content

Commit

Permalink
Merge pull request #2601 from stuartwdouglas/2511
Browse files Browse the repository at this point in the history
Fixes #2511, swagger-ui does not work in gradle
  • Loading branch information
stuartwdouglas authored May 28, 2019
2 parents 605473a + 4083aff commit f438459
Showing 1 changed file with 69 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;
import java.util.function.BiFunction;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -58,18 +60,81 @@ public ClassPathArtifactResolver(ClassLoader classLoader) {

@Override
public ResolvedArtifact getArtifact(String groupId, String artifactId, String classifier) {

Pattern filePatten;
if (classifier == null || classifier.isEmpty()) {
filePatten = Pattern.compile(artifactId + "-(\\d.*)\\.jar");
} else {
filePatten = Pattern.compile(artifactId + "-" + classifier + "-(\\d.*)\\.jar");
}
List<BiFunction<StoredUrl, Matcher, ResolvedArtifact>> resolvers = createResolvers(groupId, artifactId, classifier);
for (StoredUrl url : pathList) {

Matcher matcher = filePatten.matcher(url.fileName);
if (matcher.matches()) {
for (BiFunction<StoredUrl, Matcher, ResolvedArtifact> resolver : resolvers) {
ResolvedArtifact result = resolver.apply(url, matcher);
if (result != null) {
return result;
}
}
}

}
throw new RuntimeException("Could not resolve artifact " + groupId + ":" + artifactId + ":" + classifier
+ ". Please make sure it is present and contains a META-INF/MANIFEST.MF file. Note that artifacts that are part of the same project may not always be resolvable, in this case you should generate a META-INF/jandex.idx file instead using the Jandex Maven plugin.");
}

private List<BiFunction<StoredUrl, Matcher, ResolvedArtifact>> createResolvers(String groupId, String artifactId,
String classifier) {
return Arrays.asList(createMavenResolver(groupId, artifactId, classifier),
createGradleResolver(groupId, artifactId, classifier));
}

private BiFunction<StoredUrl, Matcher, ResolvedArtifact> createGradleResolver(String groupId, String artifactId,
String classifier) {
return new BiFunction<StoredUrl, Matcher, ResolvedArtifact>() {
@Override
public ResolvedArtifact apply(StoredUrl url, Matcher matcher) {
//gradle name format in reverse order is:
//filename
//hash
//version
//artifact-id
//group-id

if (url.path.getNameCount() < 5) {
return null;
}
String fileArtifact = url.path.getName(url.path.getNameCount() - 4).toString();
String fileGroup = url.path.getName(url.path.getNameCount() - 5).toString();
if (fileGroup.equals(groupId) && fileArtifact.equals(artifactId)) {
try {
return new ResolvedArtifact(groupId, artifactId, matcher.group(1), classifier, url.path);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
return null;
}
};

}

public BiFunction<StoredUrl, Matcher, ResolvedArtifact> createMavenResolver(String groupId, String artifactId,
String classifier) {
Pattern filePatten;
if (classifier == null || classifier.isEmpty()) {
filePatten = Pattern.compile(artifactId + "-(\\d.*)\\.jar");
} else {
filePatten = Pattern.compile(artifactId + "-" + classifier + "-(\\d.*)\\.jar");
}
return new BiFunction<StoredUrl, Matcher, ResolvedArtifact>() {
@Override
public ResolvedArtifact apply(StoredUrl url, Matcher matcher) {
String[] groupParts = groupId.split("\\.");
if (url.path.getNameCount() < groupParts.length + 2) {
continue;
return null;
}

boolean matches = true;
Expand All @@ -88,10 +153,10 @@ public ResolvedArtifact getArtifact(String groupId, String artifactId, String cl
}

}
return null;
}
}
throw new RuntimeException("Could not resolve artifact " + groupId + ":" + artifactId + ":" + classifier
+ ". Please make sure it is present and contains a META-INF/MANIFEST.MF file. Note that artifacts that are part of the same project may not always be resolvable, in this case you should generate a META-INF/jandex.idx file instead using the Jandex Maven plugin.");
};

}

static class StoredUrl {
Expand Down

0 comments on commit f438459

Please sign in to comment.