From e7115a7119b9e8b8d0716c47b4baad726eb0dd9f Mon Sep 17 00:00:00 2001 From: Daniel Atallah Date: Fri, 6 Apr 2018 11:58:54 -0400 Subject: [PATCH 1/2] Add support for tracking the offline ivy dependencies in a separate path This allows build with both maven and ivy repositories to work when the ivy.xml contains custom configurations. --- README.md | 18 ++++++++- .../OfflineDependenciesPlugin.groovy | 7 ++++ .../UpdateOfflineRepositoryTask.groovy | 37 +++++++++++++------ 3 files changed, 50 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index d1eb920..88620f2 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,22 @@ offlineDependencies { } ``` +You can additionally override ```offlineIvyRepositoryRoot``` to separate Ivy dependencies from Maven dependencies: +```gradle +repositories { + ivy { + url offlineIvyRepositoryRoot + layout 'pattern', { + artifact '[organisation]/[module]/[revision]/[artifact]-[revision](-[classifier]).[ext]' + ivy '[organisation]/[module]/[revision]/ivy-[revision].xml' + m2compatible = true + } + } +} +``` +This may be necessary if you use both maven and ivy repositories in the same build and you use custom ivy configurations. + + ## Tasks Currently the plugin only exposes a single task: @@ -107,7 +123,7 @@ buildscript { } ``` -Just make sure that the `offlineRepositoryRoot` repository is first in the list. +Just make sure that the `offlineRepositoryRoot` (and `offlineIvyRepositoryRoot`, if used) repository is before the remote repositories. ## Example build.gradle diff --git a/src/main/groovy/io/pry/gradle/offline_dependencies/OfflineDependenciesPlugin.groovy b/src/main/groovy/io/pry/gradle/offline_dependencies/OfflineDependenciesPlugin.groovy index e53f703..7bcfaa2 100644 --- a/src/main/groovy/io/pry/gradle/offline_dependencies/OfflineDependenciesPlugin.groovy +++ b/src/main/groovy/io/pry/gradle/offline_dependencies/OfflineDependenciesPlugin.groovy @@ -27,8 +27,15 @@ class OfflineDependenciesPlugin implements Plugin { project.logger.info("Offline dependencies root configured at '${project.ext.offlineRepositoryRoot}'") + if (project.hasProperty("offlineIvyRepositoryRoot")) { + project.logger.info("Offline ivy dependencies root configured at '${project.ext.offlineIvyRepositoryRoot}'") + } else { + project.ext.offlineIvyRepositoryRoot = project.offlineRepositoryRoot + } + project.task('updateOfflineRepository', type: UpdateOfflineRepositoryTask) { conventionMapping.root = { "${project.offlineRepositoryRoot}" } + conventionMapping.ivyRoot = { "${project.offlineIvyRepositoryRoot}" } conventionMapping.configurationNames = { extension.configurations } conventionMapping.buildscriptConfigurationNames = { extension.buildScriptConfigurations } conventionMapping.includeSources = { extension.includeSources } diff --git a/src/main/groovy/io/pry/gradle/offline_dependencies/UpdateOfflineRepositoryTask.groovy b/src/main/groovy/io/pry/gradle/offline_dependencies/UpdateOfflineRepositoryTask.groovy index aa34611..5f575d9 100644 --- a/src/main/groovy/io/pry/gradle/offline_dependencies/UpdateOfflineRepositoryTask.groovy +++ b/src/main/groovy/io/pry/gradle/offline_dependencies/UpdateOfflineRepositoryTask.groovy @@ -14,6 +14,8 @@ import org.gradle.api.artifacts.ExternalModuleDependency import org.gradle.api.artifacts.UnknownConfigurationException import org.gradle.api.artifacts.component.ComponentIdentifier import org.gradle.api.artifacts.component.ModuleComponentIdentifier +import org.gradle.api.artifacts.result.ComponentArtifactsResult; +import org.gradle.api.artifacts.result.UnresolvedArtifactResult; import org.gradle.api.tasks.Input import org.gradle.api.tasks.InputFiles import org.gradle.api.tasks.OutputFiles @@ -27,6 +29,7 @@ import org.gradle.language.java.artifact.JavadocArtifact import org.gradle.maven.MavenModule import org.gradle.maven.MavenPomArtifact import org.gradle.util.GFileUtils +import java.util.Set; import static io.pry.gradle.offline_dependencies.Utils.addToMultimap @@ -37,6 +40,8 @@ class UpdateOfflineRepositoryTask extends DefaultTask { @Input GString root @Input + GString ivyRoot + @Input Set configurationNames @Input Set buildscriptConfigurationNames @@ -50,6 +55,8 @@ class UpdateOfflineRepositoryTask extends DefaultTask { boolean includeIvyXmls @Input boolean includeBuildscriptDependencies + + private Set ivyComponentIds = [] as Set @TaskAction void run() { @@ -179,9 +186,21 @@ class UpdateOfflineRepositoryTask extends DefaultTask { collectPoms(componentIds, repositoryFiles) } + def ivyComponents = [] as Set + //If we need to keep track of ivy stuff separately keep track of what the ivy dependencies are + if (!root.equals(ivyRoot) || this.getIncludeIvyXmls()) { + def ivyArtifacts = project.dependencies.createArtifactResolutionQuery() + .forComponents(componentIds) + .withArtifacts(IvyModule, IvyDescriptorArtifact) + .execute() + ivyComponents = ivyArtifacts.resolvedComponents + + ivyComponentIds = ivyComponents.collect { it.getId() } as Set + } + // collect ivy xml files - if (this.getIncludeIvyXmls()) { - collectIvyXmls(componentIds, repositoryFiles) + if (this.getIncludeIvyXmls() && !ivyComponents.isEmpty()) { + collectIvyXmls(componentIds, repositoryFiles, ivyComponents) } return repositoryFiles @@ -200,7 +219,7 @@ class UpdateOfflineRepositoryTask extends DefaultTask { for (component in mavenArtifacts.resolvedComponents) { def poms = component.getArtifacts(MavenPomArtifact) - if (poms?.empty) { + if (poms?.empty || poms.first() instanceof UnresolvedArtifactResult) { continue } @@ -259,15 +278,10 @@ class UpdateOfflineRepositoryTask extends DefaultTask { } } - private void collectIvyXmls(Set componentIds, Map> repositoryFiles) { + private void collectIvyXmls(Set componentIds, Map> repositoryFiles, Set ivyComponents) { logger.trace("Collecting ivy xml files") - def ivyArtifacts = project.dependencies.createArtifactResolutionQuery() - .forComponents(componentIds) - .withArtifacts(IvyModule, IvyDescriptorArtifact) - .execute() - - for (component in ivyArtifacts.resolvedComponents) { + for (component in ivyComponents) { def ivyXmls = component.getArtifacts(IvyDescriptorArtifact) if (ivyXmls?.empty) { @@ -302,7 +316,8 @@ class UpdateOfflineRepositoryTask extends DefaultTask { // Return the offline-repository target directory for the given component (naming follows maven conventions) protected File moduleDirectory(ModuleComponentIdentifier ci) { - new File("${getRoot()}".toString(), "${ci.group.tokenize(".").join("/")}/${ci.module}/${ci.version}") + new File("${ivyComponentIds.contains(ci) ? getIvyRoot() : getRoot()}".toString(), "${ci.group.tokenize(".").join("/")}/${ci.module}/${ci.version}") } + } From b24f3f3951402acc0195792e25aaad7f7d07885a Mon Sep 17 00:00:00 2001 From: Daniel Atallah Date: Mon, 28 Jan 2019 13:52:14 -0500 Subject: [PATCH 2/2] Fix whitespace to match upstream merge --- .../UpdateOfflineRepositoryTask.groovy | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/main/groovy/io/pry/gradle/offline_dependencies/UpdateOfflineRepositoryTask.groovy b/src/main/groovy/io/pry/gradle/offline_dependencies/UpdateOfflineRepositoryTask.groovy index 6a284bd..86f59b5 100644 --- a/src/main/groovy/io/pry/gradle/offline_dependencies/UpdateOfflineRepositoryTask.groovy +++ b/src/main/groovy/io/pry/gradle/offline_dependencies/UpdateOfflineRepositoryTask.groovy @@ -39,8 +39,8 @@ class UpdateOfflineRepositoryTask extends DefaultTask { @Input GString root @Input - GString ivyRoot - @Input + GString ivyRoot + @Input Set configurationNames @Input Set buildscriptConfigurationNames @@ -55,7 +55,7 @@ class UpdateOfflineRepositoryTask extends DefaultTask { @Input boolean includeBuildscriptDependencies - private Set ivyComponentIds = [] as Set + private Set ivyComponentIds = [] as Set @TaskAction void run() { @@ -180,17 +180,17 @@ class UpdateOfflineRepositoryTask extends DefaultTask { collectPoms(componentIds, repositoryFiles) } - def ivyComponents = [] as Set - //If we need to keep track of ivy stuff separately keep track of what the ivy dependencies are - if (!root.equals(ivyRoot) || this.getIncludeIvyXmls()) { - def ivyArtifacts = project.dependencies.createArtifactResolutionQuery() - .forComponents(componentIds) - .withArtifacts(IvyModule, IvyDescriptorArtifact) - .execute() - ivyComponents = ivyArtifacts.resolvedComponents + def ivyComponents = [] as Set + //If we need to keep track of ivy stuff separately keep track of what the ivy dependencies are + if (!root.equals(ivyRoot) || this.getIncludeIvyXmls()) { + def ivyArtifacts = project.dependencies.createArtifactResolutionQuery() + .forComponents(componentIds) + .withArtifacts(IvyModule, IvyDescriptorArtifact) + .execute() + ivyComponents = ivyArtifacts.resolvedComponents - ivyComponentIds = ivyComponents.collect { it.getId() } as Set - } + ivyComponentIds = ivyComponents.collect { it.getId() } as Set + } // collect ivy xml files if (this.getIncludeIvyXmls() && !ivyComponents.isEmpty()) { @@ -324,7 +324,7 @@ class UpdateOfflineRepositoryTask extends DefaultTask { // Return the offline-repository target directory for the given component (naming follows maven conventions) protected File moduleDirectory(ModuleComponentIdentifier ci) { - new File("${ivyComponentIds.contains(ci) ? getIvyRoot() : getRoot()}".toString(), "${ci.group.tokenize(".").join("/")}/${ci.module}/${ci.version}") + new File("${ivyComponentIds.contains(ci) ? getIvyRoot() : getRoot()}".toString(), "${ci.group.tokenize(".").join("/")}/${ci.module}/${ci.version}") } }