Created
December 4, 2015 04:58
-
-
Save bmuschko/e4e04abca105c397145e to your computer and use it in GitHub Desktop.
Creating an offline Maven repository with Gradle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
} | |
} | |
} | |
} |
This misses getting the parent poms and boms which prevents you from using this to gather all dependencies to run Gradle in offline mode.
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
Slight tweak required (see here)
Change this
To this