On 18/01/10 4:15 AM, Craig Wickesser wrote:
I'm trying to get an executable JAR that contains groovy code that I can run by doing a simple: java -jar myapp.jar

Here's my build.gradle

usePlugin 'groovy'
usePlugin 'maven' // Maven plugin to install artifact in local Maven repo.

sourceCompatibility = '1.6'
targetCompatibility = '1.6'

manifest.mainAttributes("Main-Class" : "org.mindscratch.foo.ParseMain")

def localMavenRepo = 'file://' + new File(System.getProperty('user.home'), '.m2/repository').absolutePath
repositories {
// Use local Maven repo location. We don't need this if we only want to install // an artifact, but we do need it if we want to use dependencies from the local
    // repository.
    mavenRepo urls: localMavenRepo
}

// Project configuration:
version = '1.0-SNAPSHOT'
group = 'org.mindscratch.foo'

// The following line is not necessary. Default the install tasks depends on the // jar task, but this means no tests and checks are executed when we use the // install task. The following line makes the install tasks depend on the build task
// and now all tests and checks are done before install is executed.
install.dependsOn ':build'


repositories {
mavenCentral() // Define Maven central repository to look for dependencies.
}

dependencies {
groovy 'org.codehaus.groovy:groovy:1.6.7' // group:name:version is a nice shortcut notation for dependencies.
    testCompile 'junit:junit:4.7'
}


task initProject(description: 'Initialize project directory structure.') << {
    // Default package to be created in each src dir.
    def defaultPackage = 'org/mindscratch/foo'
    ['java', 'groovy', 'resources'].each {
        // convention.sourceSets contains the directory structure
        // for our Groovy project. So we use this struture
        // and make a directory for each node.
        convention.sourceSets.all."${it}".srcDirs*.each { dir ->
            def newDir = new File(dir, defaultPackage)
logger.info <http://logger.info> "Creating directory $newDir" // gradle -i shows this message.
            newDir.mkdirs()  // Create dir.
        }
    }
}


I run:
$ gradle install -i
$ cd build/libs
$ java -jar foo-1.0-SNAPSHOT.jar

..doesn't work b/c the Groovy jar isn't on the classpath (or inside my JAR). GUess what I need is a JAR that contians the Groovy jar inside of it (maybe in a "lib" folder). Can you help?


The cookbook has some options for creating a JAR which contains its compile or runtime dependencies: http://docs.codehaus.org/display/GRADLE/Cookbook#Cookbook-Creatingafatjar

There's also a JIRA issue for making this easier: http://jira.codehaus.org/browse/GRADLE-566


--
Adam Murdoch
Gradle Developer
http://www.gradle.org

Reply via email to