I am trying with Gradle first time. I am trying with a maven java project to compile and create a jar file. It is compiling and creating the jar file in build/libs directory as
trunk-XXXVERSION-SNAPSHOT.jar
I am running gradle build file from trunk directory of this maven java project.
I want to get the project name (for ex: project1) in the jar file name, something like
project1-XXXVERSION-SNAPSHOT.jar
in build/libs directory.
Please suggest.
Here is the directory structure:
trunk
˪ build
˪ libs
˪ project1-1.0-SNAPSHOT.jar
˪ build.gradle
Include the following in build.gradle:
apply plugin: 'java'
apply plugin: 'maven'
archivesBaseName = 'project1'
version = '1.0-SNAPSHOT'
group = 'example'
This will produce the correct ZIPs, POMs and JARs.
Additionally, try setting:
archivesBaseName = 'project1'
or (deprecated):
jar.baseName = 'project1'
The default project name is taken from the directory the project is stored in. Instead of changing the naming of the jar explicitly you should set the project name correct for your build. At the moment this is not possible within the build.gradle file. Instead, you have to create a settings.gradle file in your root directory. This settings.gradle file should have this one liner included:
rootProject.name = 'project1'
I recently migrated to gradle 4.6 (from 3. something) and the
jar {
baseName = 'myjarname'
}
stopped working, gradle named my jar from the folder name.
So I switched to archivesBaseName = 'myjarname' which works.
Maybe this helps somebody else too.
If you has some submobule, you can use in build.gradle (for jar)
configurations {
jar.archiveName = 'submodule-jar.jar'
}
In Kotlin DSL you can also use:
tasks.jar {
archiveFileName.set("app.jar")
}
With Spring boot and Kotlin DSL you can use:
tasks {
bootJar {
archiveFileName.set("app.jar")
}
}
Currently using Kotlin as Gradle DSL. Following statement works for me:
tasks.withType<AbstractArchiveTask> {
setProperty("archiveFileName", "hello-world.jar")
}
It works on Spring Boot executable jars as well.
If you want to keep version numbers:
tasks.withType<AbstractArchiveTask> {
setProperty("archiveBaseName", "hello-world")
}
It will produce something like hello-world-1.2.3.jar
If you are using a newer Gradle version, baseName, archiveName will now be deprecated. Instead, use something like
jar {
archivesBaseName = 'project1'
archiveVersion = '1.0-SNAPSHOT'
}
if you want to append a date to the jar file name, you can do it like this:
jar {
baseName +='_' +new java.text.SimpleDateFormat("dd_MM_yyyy").format(new java.util.Date())
println(baseName) // just to verify
which results in <basename>_07_05_2020.jar
You have to remove the 'version' tag in your build.gradle file!
It can works in Gradle 7.5.1 with Groove DSL:
jar {
archiveFileName = "name.jar"
}
If you are using Kotlin DSL:
tasks.withType<Jar> {
archiveFileName.set("name.jar")
}
Related
I need to add the following headers to the MANIFEST.MF file using Gradle and Spring Boot:
Name
Specification-Title
Specification-Version
Specification-Vendor
Is there a way to configure the Gradle build process to read these values from settings.gradle and build.gradle and write them to the MANIFEST.MF file?
Writing them to the manifest file is described here: add manifest file to jar with gradle
To populate these values from settings.gradle and gradle.properties, consider the following:
build.gradle
plugins {
id "idea"
id "java"
}
jar {
manifest {
attributes 'Specification-Title': gradle.ext.jonesVar
attributes 'Specification-Version': bibibi
}
}
settings.gradle
gradle.ext.jonesVar = "jones"
gradle.properties
bibibi=3
The result from ./gradlew clean build gives build/libs/tmp.jar containing this manifest:
MANIFEST.MF
Manifest-Version: 1.0
Specification-Title: jones
Specification-Version: 3
If you need to do this for the bootJar, use bootJar { instead of jar { in your build.gradle.
I am going through the gradle jar build example at https://guides.gradle.org/building-jvm-libraries/
The java source is not in a default src/main/java directory, it's in org\example\mylib directory. How can I customise gradle to run gradle jar from this directory and compile the java source files to a jar?
The whole directory structure is \mylib\src\main\java\org\example\mylib
When I am in that directory, and run gradle jar there is a success message but then when I check with jar -tf build/libs/mylib-0.1.0.jar all I see is the manifest files. There are no java classes.
If I try and run gradle jar in the \mylib directory alone, then it fails with error message Task 'jar' not found in root project
The build.gradle file is:
apply plugin: 'java'
version = '0.1.0'
Try adding this to the gradle.build:
sourceSets {
main {
java {
srcDirs = ['src\main\java\org\example\mylib']
}
}
}
I'm trying to add a task (gen or gen2) to my build.gradle that does exactly the same as the Jar-task:
version = "0.0.1"
apply plugin: 'java'
task('gen', type: Jar) {
}
task gen2(type: Jar)
Running
gradle jar
generates a JAR-file that contains .class-files, while running
gradle gen
or
gradle gen2
generate a JAR-file that does NOT contain any .class-files.
Whats wrong with my class definition?
To build a jar with all the classes from main, as a default jar task would, do this:
task gen2(type: Jar){
baseName = 'gen2Jar'
from sourceSets.main.output
}
You can also do from(sourceSets.main.output){ include "package" } to customize what packages are included.
Alternatively, to copy settings from the default jar task:
task gen(type: Jar){
baseName = 'genJar'
with jar
}
Infact you can have both of these in the same build.gradle. Running gradle jar builds default jar. gradle gen builds genJar.jar and gradle gen2 builds gen2Jar.jar, all of which contain all the classes from java.main
I am trying to use Cascading in my Hadoop project. I am trying to implement first example given in Enterprise Data Workflows with Cascading book. I have written java class which contains Cascading related code and I have another build.graddle file which is supposed to compile that java class and build jar file out of it.
My folder structure is as follows :
main_folder
impatient
Main.java
build.gradle
My build.gradle file looks as below :
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'eclipse'
archivesBaseName = 'impatient'
repositories {
mavenLocal()
mavenCentral()
mavenRepo name: 'conjars', url: 'http://conjars.org/repo/'
}
ext.cascadingVersion = '2.1.0'
dependencies {
compile( group: 'cascading', name: 'cascading-core', version: cascadingVersion )
compile( group: 'cascading', name: 'cascading-hadoop', version: cascadingVersion )
}
jar {
description = "Assembles a Hadoop ready jar file"
doFirst {
into( 'lib' ) {
from configurations.compile
}
}
manifest {
attributes( "Main-Class": "impatient/Main" )
}
}
When I run gradle clean jar command from command prompt, I get build successful message. I tried to run this jar file using
hadoop jar impatient.jar <input file path> <output file path>
command but then it gives me Exception in thread "main" java.lang.ClassNotFoundException: impatient.Main exception.
So I checked contentes of jar file and found that that jar does not contain impatient/Main.class file.
Please note that I do not know anything about gradle.
Request someone to please tell me if there is anything wrong with gradle script or I am making some mistake.
Thanks !!!
Move your source file to
main_folder/impatient/src/main/java/Main.java
but leave build.gradle file where it is.
By default, Gradle uses src/main/java and src/test/java to look for production and test java sources (relative to root folder, which is impatient in your case)
I have a spring context file and a property file located in my project root.
When running gradle distZip i get all the sources included as well as the libraries, but how do I also include the two files?
These are the simplest working build files that solves the problem. You can easily make use of contents specification in your build. More information about CopySpec.
Using distribution plugin
apply plugin: 'distribution'
distributions {
main {
contents {
from "$projectDir"
into 'doc'
include 'README'
}
}
}
Using application plugin
apply plugin: 'application'
mainClassName = 'Main'
applicationDistribution.from("$projectDir") {
into 'doc'
include 'README'
}