85

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.

1

11 Answers 11

132

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'
7
  • Just to keep the discussion complete: the first version does work and it also produces correct zips, maven archives and so on. I find it also more elegant. I will edit my answer to provide full script. Jul 21, 2011 at 17:44
  • Yes, this works too, but I do not see the POMs and JARs generated in the repository. I am using 'uploadArchives' to publish it to maven artifactory after declaring 'repositories'.
    – Chakri
    Jul 21, 2011 at 20:44
  • 1
    archivesBaseName works but jar.baseName can be problematic as your sources jar might not be named correctly.
    – opticyclic
    Feb 19, 2014 at 21:56
  • 1
    baseName was deprecated (Gradle 6.2): docs.gradle.org/6.2.2/dsl/… It was replaced by archivesBaseName.
    – ezzadeen
    Apr 13, 2020 at 16:01
  • in kotlin, the archiveBaseName is project.base.archivesBaseName, as of gradle 6.2
    – slycrel
    Sep 25, 2020 at 21:57
71

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'
8
  • 1
    Thanks a lot for the solution. It worked for me. Also found that we can use 'doFirst' for jar and it worked too. So what is the best way to achieve this.
    – Chakri
    Jul 21, 2011 at 16:25
  • what do you mean with "we can use doFirst for jar" ? jar is a "normal" gradle task, that can be augmented with custom action closures via doLast and doFirst. Jul 21, 2011 at 17:04
  • using this snippet makes also sure, that your other zips, maven archives etc have the correct naming as they all depend on your project name Jul 21, 2011 at 17:05
  • 3
    @koppor project.name is readonly in build.gradle for a very long time. As mentioned you have to use the settings.gradle file as shown above to change the project name(s). Jan 3, 2013 at 13:14
  • 1
    @DaniloTommasina You can specify the names of your projects in your single settings.gradle file, for both the root project and each subproject. Jan 13, 2018 at 12:34
29

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.

0
17

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")
    }
}
2
  • On Spring Boot add this to end of build.gradle but does not work.
    – e-info128
    Jul 7, 2020 at 20:23
  • At least in the Kotlin DSL this works. I have adapted the solution.
    – mrclrchtr
    Aug 10, 2020 at 13:34
16

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

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

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'
}
2
  • 1
    AFAIK it's archiveBaseName not archive*s*BaseName.
    – Martin
    Oct 20, 2022 at 8:20
  • 1
    yes it's archivesBaseName = 'project1' with an s. This sets the jar base name in the case where it builds multiple jars in lib folder. Such as when you build the ueber jar. Dec 22, 2022 at 2:01
4

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

1
  • 1
    This solution has the advantage that the changed name also applies to the source jar.
    – Martin
    Oct 20, 2022 at 8:25
2

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

2

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

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

1
  • Default gradle behavior is to add the version parameter to artifact name when run sending it like this: ./gradlew build -Pversion=${APPLICATION_VERSION} In this case APPLICATION_VERSION can be generated in the bash script, for instance getting the last tag from git Sep 21 at 0:27
0

In my case I had to add the following:

tasks.bootJar {
    archiveFileName.set("file-name-here.jar")
}

(Gradle 7.6.1, Spring Boot 3.0.6 and the Gradle Kotlin DSL)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.