Skip to content

Instantly share code, notes, and snippets.

@bmuschko
Created December 4, 2015 04:58
Show Gist options
  • Save bmuschko/e4e04abca105c397145e to your computer and use it in GitHub Desktop.
Save bmuschko/e4e04abca105c397145e to your computer and use it in GitHub Desktop.
Creating an offline Maven repository with Gradle
package org.gradle.training
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction
import org.gradle.api.DefaultTask
import org.gradle.util.GFileUtils
import org.gradle.api.artifacts.Configuration
import org.gradle.api.artifacts.component.ModuleComponentIdentifier
import org.gradle.maven.MavenModule
import org.gradle.maven.MavenPomArtifact
class OfflineMavenRepository extends DefaultTask {
@Input
String configurationName = 'compile'
@OutputDirectory
File repoDir = new File(project.buildDir, 'offline-repo')
@TaskAction
void build() {
Configuration configuration = project.configurations.getByName(configurationName)
copyJars(configuration)
copyPoms(configuration)
}
private void copyJars(Configuration configuration) {
configuration.resolvedConfiguration.resolvedArtifacts.each { artifact ->
def moduleVersionId = artifact.moduleVersion.id
File moduleDir = new File(repoDir, "${moduleVersionId.group}/${moduleVersionId.name}/${moduleVersionId.version}")
GFileUtils.mkdirs(moduleDir)
GFileUtils.copyFile(artifact.file, new File(moduleDir, artifact.file.name))
}
}
private void copyPoms(Configuration configuration) {
def componentIds = configuration.incoming.resolutionResult.allDependencies.collect { it.selected.id }
def result = project.dependencies.createArtifactResolutionQuery()
.forComponents(componentIds)
.withArtifacts(MavenModule, MavenPomArtifact)
.execute()
for(component in result.resolvedComponents) {
def componentId = component.id
if(componentId instanceof ModuleComponentIdentifier) {
File moduleDir = new File(repoDir, "${componentId.group}/${componentId.module}/${componentId.version}")
GFileUtils.mkdirs(moduleDir)
File pomFile = component.getArtifacts(MavenPomArtifact)[0].file
GFileUtils.copyFile(pomFile, new File(moduleDir, pomFile.name))
}
}
}
}
@lukecwik
Copy link

This misses getting the parent poms and boms which prevents you from using this to gather all dependencies to run Gradle in offline mode.

@lbispham
Copy link

lbispham commented Jul 8, 2019

For those struggling with the issue lukecwik brought up, check out uklance's solution on GitHub. He has created a plugin that pulls in all the dependencies.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment