Self-contained gradle wrapper distribution with dependencies

For a demo, I’m assuming there’s no internet connection and I want to distribute a self-contained zip file containing the gradle wrapper, source code and project dependencies.

I have largely got this to work by:

  • Adding in the gradle distribution (gradle-1.2-bin.zip) and referencing it as a local file://etc distributionUrl in gradle-wrapper.properties * Declaring my dependencies as normal then using the following task to copy them to a “libs” directory.
task makelib(type: Copy, dependsOn:classes) {
    into "libs"
    from configurations.all
}
  • Change my dependencies to point to:
compile fileTree(dir: 'libs', include: '*.jar')

I would really like the sources/javadoc as well and for the eclipse task to generate a correct config that references the sources.

I’m wondering if this is the correct approach or whether I should try instead a

repositories {
    flatDir {
        dirs 'libs'
    }

Any suggestions?

If I declare my dependencies as normal and use a “flatDir” repository it won’t work because there’s no way to find/resolve transitive dependencies. Perhaps I can zip up my gradle cache? That would be messy because I also use mavenLocal()

You want GRADLE-1989 (which is not implemented yet).

There’s no great solution to this problem at the moment. If you can manage it, the best thing to do is to use a file based maven/ivy repo. This means you need to get this content from somewhere though.

repositories {
  maven "file://$projectDir/maven-repo"
}

Another option is to run up an instance of Artifactory or Nexus on your local machine which acts as a proxy.

I decided to use my original solution (above). I manually added some of the lib sources into a libs_sources directory and manipulated the eclipse classpath to include them.

eclipse {
   classpath {
        file {
            whenMerged { classpath ->
                org.gradle.plugins.ide.eclipse.model.internal.FileReferenceFactory factory = new org.gradle.plugins.ide.eclipse.model.internal.FileReferenceFactory()
                classpath.entries.each {
                    if (it.kind == 'lib') {
                        String sourcePath = it.path.replace("/libs/","/libs_sources/").replace(".jar","-sources.jar")
                        File sourceJar = new File(sourcePath)
                        if (sourceJar.exists()) {
                             it.sourcePath = factory.fromPath(sourcePath)
                        }
                    }
                }
            }
        }
   }
}