Gradle - jar file name in java plugin

Gradle

Gradle Problem Overview


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.

Gradle Solutions


Solution 1 - Gradle

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'

Solution 2 - Gradle

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'

Solution 3 - Gradle

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.

Solution 4 - Gradle

If you has some submobule, you can use in build.gradle (for jar)

configurations {
    jar.archiveName = 'submodule-jar.jar'
}

Solution 5 - Gradle

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")
    }
}

Solution 6 - Gradle

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

Solution 7 - Gradle

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'
}

Solution 8 - Gradle

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

Solution 9 - Gradle

You have to remove the 'version' tag in your build.gradle file!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChakriView Question on Stackoverflow
Solution 1 - GradleArtur NowakView Answer on Stackoverflow
Solution 2 - GradleRene GroeschkeView Answer on Stackoverflow
Solution 3 - Gradlevargen_View Answer on Stackoverflow
Solution 4 - GradleJan SeifrtView Answer on Stackoverflow
Solution 5 - GradlemrclrchtrView Answer on Stackoverflow
Solution 6 - GradleFırat KÜÇÜKView Answer on Stackoverflow
Solution 7 - GradletheekshanawjView Answer on Stackoverflow
Solution 8 - GradleChrisView Answer on Stackoverflow
Solution 9 - GradleDisembleergonView Answer on Stackoverflow