Gradle custom task for jar - java

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!

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

Related

Create a Jar file with dependencies from settings.gradle

I want to create a jar file which can be used by command like java -cp .\TrendAnalyzer.jar trend_detect.Main.
However, I can't find a way to create a jar file from settings.gradle, not build.gradle.
How can I create a jar file with dependencies from settings.gradle, which contains include etc. information.
Add com.github.johnrengelman.shadow plugin to main module build.jar
plugins {
// *snip*
id 'com.github.johnrengelman.shadow' version '7.1.2'
}
application {
// Define the main class for the application.
mainClass = 'jaso92559.app.App'
}
jar {
manifest {
// Define the main class for the application.
attributes "Main-Class": "jaso92559.app.App"
}
}
run ./gradle shadowJar command on the path where settings.gradle exists.
In app\build\libs, app-all.jar file is generated and it can be run java -jar app-all.jar.
According to https://github.com/yukihane/stackoverflow-qa/tree/main/jaso92559
To create a jar file from settings.gradle, you can use the jar task in Gradle. This task allows you to package the compiled code and any dependencies into a jar file, which can then be executed using the java command with the -cp option.
Creating a jar file using the jar task in settings.gradle:
// settings.gradle
include ':trend_detect'
// build.gradle (in the trend_detect module)
apply plugin: 'java'
jar {
manifest {
attributes 'Main-Class': 'trend_detect.Main'
}
from {
configurations.compile.collect {
it.isDirectory() ? it :
zipTree(it) }
}
}
The include statement in settings.gradle specifies that the trend_detect module should be included in the build.
In the build.gradle file for the trend_detect module, the jar task is defined to create a jar file with a MANIFEST.MF file that specifies the main class (trend_detect.Main). The from statement in the jar task specifies that the jar file should include all of the compiled code and dependencies from the compile configuration.
Once the jar file has been created, you can run it using the java command with the -cp option, as follows:
java -cp .\TrendAnalyzer.jar trend_detect.Main
This will execute the main() method in the trend_detect.Main class, using the compiled code and dependencies contained in the jar file.

creating fatjar from obsufucated jar and library jar depedencies?

I have a a fatjar, which is a deployable jar with main method defined, and which depedencies are copied via gradle.
This works fine.
I want to obsufucate/encrypt my jar.
Using proguard I can obsufucate myJar, and specify libraryjars. How do I get a farJar created from the obsufucated jar and its dependencies ?
So far I can only obsufucate the original jar, not obsufucate and bundle as fat jar.
task fatJar(type: Jar) {
manifest {
attributes 'Implementation-Title': 'Metriculous',
'Implementation-Version': version,
'Main-Class': 'au.com.metriculous.MetriculousServer'
}
baseName = project.name + '-all'
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
my proguard task looks pretty much the same as the samples, the library jars don't get added to jar as fatJar. And I don't want to define lots of rules for not obsufucating injars, which I could do if I simply obsufucate the whole fatjar ?

Gradle compile external dependencies into exploded directory

I'm trying to compile a project with Gradle, but when I run it I get a ClassLoader error saying that classes in my compile dependencies cannot be found. I see from here that it is possible to include external dependencies in a 'fat jar' through editing the jar group in build.gradle:
jar {
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}
However, I don't particularly want to package as a jar. Is it possible to do something similar when compiling to an exploded directory?
Also, a second question springs to mind: reflecting on making a 'fat jar' and thinking about the modules system in Java 9, how would one go about compiling external dependencies into modular jars using Gradle?

Main class in Jar file from src/test

I need to distribute one of our integration tests, it lives under src/test. So I'm using gradle to build a jar pointing to this class.
I figured this out from poking around the net:
task fatJar(type: Jar) {
zip64 = true
manifest {
attributes 'Implementation-Title': 'Gradle Jar File Example',
'Implementation-Version': 1.3,
'Main-Class': 'org.example.AnIntegrationTest'
}
from sourceSets.test.output
}
When I try running this I get the:
Error: Could not find or load main class org.example.AnIntegrationTest
The main method is there, and 'jar tf' does show me the class in the package.
What am I missing here?
Your jar file is missing test-runtime dependencies. One possible solution is to package all required dependencies into your jar as well. Note that this will increase the size of your distributable jar.
Add an additional from clause to your jar task:
task fatJar(type: Jar) {
zip64 = true
manifest {
attributes 'Implementation-Title': 'Gradle Jar File Example',
'Implementation-Version': 1.3,
'Main-Class': 'org.example.AnIntegrationTest'
}
from sourceSets.test.output
//collect all dependencies
from { configurations.testRuntime.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}

Add classpath in manifest using Gradle

I would like my Gradle build script to add the complete Classpath to the manifest file contained in JAR file created after the build.
Example:
Manifest-Version: 1.0
Class-Path: MyProject.jar SomeLibrary.jar AnotherLib.jar
My build script already add some information to the manifest this way:
jar {
manifest {
attributes("Implementation-Title": project.name,
"Implementation-Version": version,
"Main-Class": mainClassName,
}
}
How do I get the list of dependencies to add to the manifest?
This page of Java tutorials describes more in detail how and why adding classpath to the manifest: Adding Classes to the JAR File's Classpath
Found a solution on Gradle's forum:
jar {
manifest {
attributes(
"Class-Path": configurations.compile.collect { it.getName() }.join(' '))
}
}
Source: Manifest with Classpath in Jar Task for Subprojects
In the latest versions of gradle, compile and runtime becomes deprecated. Instead, use runtimeClasspath as follows:
'Class-Path': configurations.runtimeClasspath.files.collect { it.getName() }.join(' ')
EDIT:
Note that if you are using Kotlin DSL, you can configure the manifest as follows:
configure<JavaPluginConvention> {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
manifest {
attributes(
"Manifest-Version" to "1.0",
"Main-Class" to "io.fouad.AppLauncher")
}
}
tasks.withType(Jar::class) {
manifest {
attributes["Manifest-Version"] = "1.0"
attributes["Main-Class"] = "io.fouad.AppLauncher"
}
}
Place this at the end of the buid.gradle file. Change the com.example.Main to your own Main class.
jar {
doFirst {
manifest {
if (!configurations.compile.isEmpty()) {
attributes(
'Class-Path': configurations.compile.collect{it.toURI().toString()}.join(' '),
'Main-Class': 'com.example.Main')
}
}
}
}
The top answers helped me a lot. Here is what worked for me:
jar {
manifest {
attributes "Main-Class": "your.package.classWithMain"
attributes "Class-Path": configurations.compile.collect { it.absolutePath }.join(" ")
}
}
So, instead of name, I had to use absolutePath. This may or may not work for you. Some suggest using runtime instead of compile. I used compile because, I have a compile section in dependencies in my build.gradle. So, the jar step picks up dependencies from there. The best thing to do is pick up something that you think will work, do a gradle build, then find the JAR file and expand it to find the META-INF/MANIFEST.MF file. You should be able to see all the directories separated by spaces. If not, you should try something different. Autocomplete feature of your IDE should be helpful in seeing what all methods or fields are available under configurations/compile etc. All this can be done easily in IntelliJ.
Oh.. and if you want to see where the library JARs are physically located on your disk, right click on your project->open module settings->Libraries and then click on any library.
This is another solution for Kotlin DSL (build.gradle.kts).
When running your app, the library files are supposed to be in libs/ subdirectory of the app.
tasks.jar {
manifest.attributes["Main-Class"] = "com.example.MyMainClass"
manifest.attributes["Class-Path"] = configurations
.runtimeClasspath
.get()
.joinToString(separator = " ") { file ->
"libs/${file.name}"
}
}
I know this is likely trivial for the groovy people here, but in my case, I wanted to change the location of the Class-Path in the manifest file depending on whether I was going to run in the production environment or local environment. I did this by making my build.gradle's jar section look like this:
jar {
from configurations.runtime
manifest {
attributes ('Main-Class': 'com.me.Main',
'Class-Path': configurations.runtime.files.collect { jarDir+"/$it.name" }.join(' ')
)
}
}
In this case, the argument to gradle build is passed like so:
$ gradle build -PjarDir="/opt/silly/path/"
Looks like gradle has evolved. This is another answer that looks similar to others, but there is a key difference: if you use a new keyword implementation in the dependencies, none of the other answers will work and you'll get an empty class path
dependencies {
// ensure the keyword here matches what
// you have in the jar->manifest->attributes
// that is "implementation"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.3'
// ...
}
// by default, implementation cannot be referenced,
// this allows us to use it below
project.configurations.implementation.setCanBeResolved(true)
jar{
manifest {
attributes(
"Main-Class": "app.Program",
"Class-Path": configurations.implementation.collect { it.name }.join(' ')
)
}
dependsOn ('dependencies')
}
If your project has external library dependencies, you could copy the jars to a folder and add the classpath entries in the manifest.
def dependsDir = "${buildDir}/libs/dependencies/"
task copyDependencies(type: Copy) {
from configurations.compile
into "${dependsDir}"
}
task createJar(dependsOn: copyDependencies, type: Jar) {
manifest {
attributes('Main-Class': 'com.example.gradle.App',
'Class-Path': configurations.compile.collect { 'dependencies/' + it.getName() }.join(' ')
)
}
with jar
}
More details can be read here
I managed to create a custom Jar file with its own manifest file like so:
task createJar(type : Jar) {
manifest {
attributes(
'Manifest-Version': "1.0",
'Main-Class': "org.springframework.boot.loader.JarLauncher",
'Start-Class': "com.my.app.AppApplication",
'Spring-Boot-Version': "2.2.4.RELEASE",
'Spring-Boot-Classes': "BOOT-INF/classes/",
'Spring-Boot-Lib': "BOOT-INF/lib/"
)
}
def originDir = file("${buildDir}/unpacked")
def destinationFile = "${buildDir}/repackaged/${project.name}-${version}"
entryCompression ZipEntryCompression.STORED // no compression in case there are files in BOOT-INF/lib
archiveName destinationFile
from originDir
archiveFile
}
I had a similar yet not identical problem.
I was publishing my lib jar L into Artifactory, and later fetching it as a dependency of module M, but the transitive dependencies, the ones which L need for compile and runtime, did not arrive with it. It took me sometime to realize that my jar was published into Artifactory with an empty pom file, hence gradle was not able to know which are L's transitive dependencies to be fetched.
The missing piece was an instruction, in the L's build.gradle, to publish the pom.
As often with gradle, the connection between the name of the instruction, and its meaning, is completely:
apply plugin: 'maven'
uploadArchives {
repositories {
mavenDeployer {
repository(url: "file://localhost/tmp/myRepo/")
}
}
}
Source: uploading_to_maven_repositories

Categories

Resources