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!
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.
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!”.
} 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 ).
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With