Nick Humrich
Nick Humrich

Reputation: 15785

Build and run a jar inside of a Gradle task

I am using Java, Spring, and Gradle. I am new to gradle and am trying to understand some stuff. I currently have gradle successfully building a jar file. I would like to add a task so that gradle runs the jar that it builds. this way I could do

./gradlew clean build run 

or I could just do it in one command if possible. The pseudocode of which would be

task run {
   clean
   build
   java -jar myJar.jar
}

How would I go about doing this?

---INFO----

my current build.gradle file:

buildscript {
    repositories {
        maven { url "http://repo.spring.io/libs-snapshot" }
        mavenLocal()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:0.5.0.M6")
    }

}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'

jar {
    baseName = 'gs-rest-service'
    version =  '0.1.0'
}

repositories {
    mavenCentral()
    maven { url "http://repo.spring.io/libs-snapshot" }
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web:0.5.0.M6")
    compile("com.fasterxml.jackson.core:jackson-databind")
    testCompile("junit:junit:4.11")
}

task wrapper(type: Wrapper) {
    gradleVersion = '1.10'
}

Upvotes: 1

Views: 4138

Answers (1)

Peter Niederwieser
Peter Niederwieser

Reputation: 123960

It's important to think in task dependencies. run wouldn't execute some other tasks sequentially (tasks can't do that); rather, it would declare task dependencies on its prerequisites, such as perhaps jar. Anyway, the easiest solution is to just apply the application plugin, which you can learn more about in the Gradle User Guide.

Upvotes: 2

Related Questions