CMPS
CMPS

Reputation: 7769

Gradle custom task for jar

I am using Gradle for my Java project and I was wondering if it's possible to prevent gradle build from creating a jar file each time executed.

Also, is it possible to have a custom task for creating the Jar with dependencies at a specific location build/myAppJar/.

Here's what I have so far for the custom task:

task toJar (type: Jar) {
    from configurations.compile.collect { zipTree it }
    manifest {
        attributes 'Implementation-Title': 'myApp',
                'Implementation-Version': 1.0,
                'Main-Class': mainClassName
    }
}

The above code will create the jar file under build/libs, and the file does not contain the compiled files, only the dependencies.

Thank you!

Upvotes: 10

Views: 36405

Answers (1)

RaGe
RaGe

Reputation: 23805

The build task comes from the java plugin. You can control when a jar is built with something like this:

jar {
  onlyIf { condition }
}

You can set jar to be built when you declare something else to be true, or hard set it to false to never build a jar.

You can include sourcesets in your toJar custom task, to include compiled files into your jar.

task toJar (type: Jar) {
    from sourceSets.all
}

You are explicitly calling for all compile time dependencies to be included in the jar here: from configurations.compile.collect

ref: Gradle java plugin

Upvotes: 8

Related Questions