Skip to content

Commit

Permalink
Use non-strict parsing of pom.xml (fixes #238)
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-manes committed Jun 15, 2018
1 parent 96995f4 commit 296d042
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 36 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ apply plugin: 'nexus'
apply plugin: 'codenarc'

group = 'com.github.ben-manes'
version = '0.18.0'
version = '0.19.0'

sourceCompatibility = '1.6'

Expand Down
2 changes: 1 addition & 1 deletion examples/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ configurations {
unresolvable2
}

dependencyUpdates.resolutionStrategy = {
dependencyUpdates.resolutionStrategy {
componentSelection { rules ->
rules.all { ComponentSelection selection ->
boolean rejected = ['alpha', 'beta', 'rc', 'cr', 'm'].any { qualifier ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ class Resolver {
final Closure resolutionStrategy
final boolean useSelectionRules
final boolean collectProjectUrls
final ConcurrentMap<ModuleVersionIdentifier, ProjectUrl> projectUrls;
final ConcurrentMap<ModuleVersionIdentifier, ProjectUrl> projectUrls

Resolver(Project project, Closure resolutionStrategy, Object pool) {
this.projectUrls = new ConcurrentHashMap<>();
this.projectUrls = new ConcurrentHashMap<>()
this.resolutionStrategy = resolutionStrategy
this.project = project
this.pool = pool
Expand Down Expand Up @@ -238,7 +238,7 @@ class Resolver {
project.logger.info(" - ${repository.name}: ${repository.dirs}")
} else if (repository instanceof MavenArtifactRepository ||
repository instanceof IvyArtifactRepository) {
project.logger.info(" - ${repository.name}: ${repository.url}");
project.logger.info(" - ${repository.name}: ${repository.url}")
} else {
project.logger.info(" - ${repository.name}: ${repository.getClass().simpleName}")
}
Expand All @@ -264,43 +264,48 @@ class Resolver {
}

private String resolveProjectUrl(ModuleVersionIdentifier id) {
ArtifactResolutionResult resolutionResult = project.dependencies.createArtifactResolutionQuery()
.forComponents(DefaultModuleComponentIdentifier.newId(id))
.withArtifacts(MavenModule, MavenPomArtifact)
.execute()

// size is 0 for gradle plugins, 1 for normal dependencies
for (ComponentArtifactsResult result : resolutionResult.resolvedComponents) {
Set<ArtifactResult> artifacts = result.getArtifacts(MavenPomArtifact)
// size should always be 1
for (ArtifactResult artifact : result.getArtifacts(MavenPomArtifact)) {
if (artifact instanceof ResolvedArtifactResult) {
File file = ((ResolvedArtifactResult) artifact).file
project.logger.info("Pom file for ${id} is ${file}")

String url = getUrlFromPom(file)
if (url) {
project.logger.info("Found url for ${id}: ${url}")
return url
} else {
ModuleVersionIdentifier parent = getParentFromPom(file)
if (parent && "${parent.group}:${parent.name}" != "org.sonatype.oss:oss-parent") {
url = getProjectUrl(parent)
if (url) {
return url
try {
ArtifactResolutionResult resolutionResult = project.dependencies.createArtifactResolutionQuery()
.forComponents(DefaultModuleComponentIdentifier.newId(id))
.withArtifacts(MavenModule, MavenPomArtifact)
.execute()

// size is 0 for gradle plugins, 1 for normal dependencies
for (ComponentArtifactsResult result : resolutionResult.resolvedComponents) {
Set<ArtifactResult> artifacts = result.getArtifacts(MavenPomArtifact)
// size should always be 1
for (ArtifactResult artifact : result.getArtifacts(MavenPomArtifact)) {
if (artifact instanceof ResolvedArtifactResult) {
File file = ((ResolvedArtifactResult) artifact).file
project.logger.info("Pom file for ${id} is ${file}")

String url = getUrlFromPom(file)
if (url) {
project.logger.info("Found url for ${id}: ${url}")
return url
} else {
ModuleVersionIdentifier parent = getParentFromPom(file)
if (parent && "${parent.group}:${parent.name}" != "org.sonatype.oss:oss-parent") {
url = getProjectUrl(parent)
if (url) {
return url
}
}
}
}
}
}
project.logger.info("Did not find url for ${id}")
return null
} catch (Exception e) {
project.logger.info("Failed to resolve the project's url", e)
return null
}
project.logger.info("Did not find url for ${id}")
return null;
}

@TypeChecked(SKIP)
private static String getUrlFromPom(File file) {
def pom = new XmlSlurper().parse(file)
def pom = new XmlSlurper(/* validating */ false, /* namespaceAware */ false).parse(file)
if (pom.url) {
return pom.url
}
Expand All @@ -309,21 +314,21 @@ class Resolver {

@TypeChecked(SKIP)
private static ModuleVersionIdentifier getParentFromPom(File file) {
def pom = new XmlSlurper().parse(file)
def pom = new XmlSlurper(/* validating */ false, /* namespaceAware */ false).parse(file)
def parent = pom.children().find { child -> child.name() == 'parent' }
if (parent) {
String groupId = parent.groupId
String artifactId = parent.artifactId
String version = parent.version
if (groupId && artifactId && version) {
return DefaultModuleVersionIdentifier.newId(groupId, artifactId, version);
return DefaultModuleVersionIdentifier.newId(groupId, artifactId, version)
}
}
return null
}

private static final class ProjectUrl {
boolean isResolved;
String url;
boolean isResolved
String url
}
}

0 comments on commit 296d042

Please sign in to comment.