Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 866
CMPS Avatar asked Jun 24 '15 16:06

CMPS


People also ask

How do I create a JAR file in Gradle?

Create a Jar File Clean the project. Run the Gradle fatJar task. The Jar is created under the $project/build/libs/ folder. 5. Run It Run it – java -jar hello-all-1.0.jar.

How do I create a custom task in Gradle?

Custom Task Definition Inside build.gradle To create a straightforward Gradle task, we need to add its definition to our build.gradle file: task welcome { doLast { println 'Welcome in the Baeldung!' } } The main goal of the above task is just to print text “Welcome in the Baeldung!”.

What is the use of projectbuilder in Gradle?

} You can use the ProjectBuilder class to create Project instances to use when you test your task class. With Gradle, it’s very simple to implement a task that is skipped when all of its inputs and outputs are up to date (see Incremental Builds ).

How do I process inputs incrementally in Gradle?

If you need to use the old API, have a look at the documentation in the user manual for Gradle 5.3.1. For a task to process inputs incrementally, that task must contain an incremental task action . This is a task action method that has a single InputChanges parameter.


1 Answers

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

like image 51
RaGe Avatar answered Sep 18 '22 17:09

RaGe