Generating a launcher shell script (with the correct runtime classpath)

Let’s say I have a parent project with two subprojects “lib” and “app”.

The “lib” project depends on the jackson-core library, v2.1.0.

The “core” project depends on the “lib” project (it doesn’t make use of jackson-core directly).

I want to create a Gradle task to automatically generate a shell script that launches “app” with the right classpath. Here’s my attempt:

task launcher(dependsOn: compileJava) {

String packageName = projectDir.name.replace(’-’, ‘_’)

String mainClassName = ‘com.dropbox.sdk.examples.’ + packageName + ‘.Main’

project.buildDir.mkdirs()

File launcherFile = new File(project.buildDir, “run”)

Writer out = new OutputStreamWriter(new FileOutputStream(launcherFile), “UTF-8”)

try {

out.write("#! /bin/sh\n")

out.write(“java -cp " + project.runtimeClasspath.getAsPath() + " " + mainClassName + " “$@””)

}

finally {

out.close()

}

ant.chmod(file: launcherFile.path, perm: “+x”)

}

build.dependsOn launcher

This generates the script “run”, but it doesn’t have the correct classpath. The classpath in the script has the following entries: 1. /home/kannan/gradle-deps/app/build/classes/main 2. /home/kannan/gradle-deps/app/build/resources/main 3. /home/kannan/gradle-deps/lib/build/libs/lib.jar

My first issue is with the 3rd entry. I would like that to point to the “classes” directory of the “lib” project.

My second issue is that the “jackson-core” JAR file isn’t included.

Any tips on what I can do about this?

Please take a look at our application plugin for your “app” project. http://gradle.org/docs/current/userguide/application_plugin.html

I tried this but it didn’t work for me. I want to be able to run the script from the ‘build’ output directory but the script’s CLASSPATH doesn’t seem set up for that. The script’s CLASSPATH also didn’t mention the dependency (jackson-core.jar) though maybe it’s mean to be included in one of the other JARs?