Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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))
}
}
}
}
@uklance

This comment has been minimized.

Copy link

@uklance uklance commented Jun 30, 2016

Slight tweak required (see here)

Change this

private void copyJars(Configuration configuration) {
   ...
   File moduleDir = new File(repoDir, "${moduleVersionId.group}/${moduleVersionId.name}/${moduleVersionId.version}")
   ...
}
private void copyPoms(Configuration configuration) {
   ...
   File moduleDir = new File(repoDir, "${componentId.group}/${componentId.module}/${componentId.version}")
   ...
}

To this

private void copyJars(Configuration configuration) {
   ...
   File moduleDir = new File(repoDir, "${moduleVersionId.group.replace('.','/')}/${moduleVersionId.name}/${moduleVersionId.version}")
   ...
}
private void copyPoms(Configuration configuration) {
   ...
   File moduleDir = new File(repoDir, "${componentId.group.replace('.','/')}/${componentId.module}/${componentId.version}")
   ...
}
@lukecwik

This comment has been minimized.

Copy link

@lukecwik lukecwik commented Dec 21, 2017

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

This comment has been minimized.

Copy link

@lbispham 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