53

I want to specify the jar name and version in build.gradle, so my build.gradle looks like below.

apply plugin: 'java'

version = "1.00.00"

 dependencies { 
     compile files('../../lib/abc.jar') 
} 

jar{
    manifest{
        attributes ("Fw-Version" : "2.50.00", "${parent.manifestSectionName}")

    }
    archiveName 'abc.jar'
}

So when I do

gradle clean build, what I was expecting was that the generated jar name would be abc-1.00.00.jar

But its not happening, the output jar name is getting as abc.jar only and its just ignoring the version. I want to specify both jar name and version, so how can I achieve that?

JulianHarty
  • 3,178
  • 3
  • 32
  • 46
amuser
  • 781
  • 2
  • 6
  • 11
  • 3
    Read https://docs.gradle.org/current/dsl/org.gradle.api.tasks.bundling.Jar.html#org.gradle.api.tasks.bundling.Jar:archiveName and https://docs.gradle.org/current/dsl/org.gradle.api.tasks.bundling.Jar.html#org.gradle.api.tasks.bundling.Jar:baseName – JB Nizet Jul 14 '15 at 11:57

10 Answers10

52

archiveName 'abc.jar' in your jar configuration is forcing the name to be 'abc.jar'. The default format for the jar name is ${baseName}-${appendix}-${version}-${classifier}.${extension}, where baseName is the name of your project and version is the version of the project. If you remove the archiveName line in your configuration you'll have the format you're after. If you want the name of the archive to be different from the project name set the baseName instead, e.g.

jar {
    manifest{
        attributes ("Fw-Version" : "2.50.00", "${parent.manifestSectionName}")
    }
    baseName 'abc'
}

See https://docs.gradle.org/current/dsl/org.gradle.api.tasks.bundling.Jar.html#org.gradle.api.tasks.bundling.Jar:archiveName for more info on configuring the jar task.

ThePyroEagle
  • 153
  • 12
andyberry88
  • 2,244
  • 20
  • 17
  • 13
    In 5.2.1 (current version) the property `baseName` is [deprecated](https://docs.gradle.org/5.2.1/dsl/org.gradle.api.tasks.bundling.Jar.html#org.gradle.api.tasks.bundling.Jar:archiveName), while the replacement [`archiveBaseName`](https://docs.gradle.org/5.2.1/dsl/org.gradle.api.tasks.bundling.Jar.html#org.gradle.api.tasks.bundling.Jar:archiveBaseName) is read-only. How do we do that in next version of Gradle? – Franklin Yu Mar 01 '19 at 14:43
  • 2
    `archiveFileName` – Sometowngeek Apr 14 '20 at 11:53
34

For Gradle 5+

build.gradle groovy DSL

archiveBaseName = 'foo'

build.gradle.kts kotlin DSL

archivesBaseName.set("foo") // Notice the double quotes
NonCreature0714
  • 5,744
  • 10
  • 30
  • 52
Ahmed Kamal
  • 2,660
  • 3
  • 21
  • 36
7

I wanted to achieve this in Kotlin DSL and this was the first question that came up on Google for me. The answers that involved Kotlin DSL did not work but I managed to find a solution.


Create this in the build.gradle.kts file:

tasks.jar {
  manifest {
    [THE LINES BELOW WILL BE INSERTED HERE]
  }
}

Assume that the project name is project and the project version is 1.0.

To change the base name of the jar file:

archiveBaseName.set("name") // .jar name: name-1.0.jar

To change the entire jar file name, write this instead:

archiveFileName.set("name.jar") // .jar name: name.jar

Use ${project.name} and ${project.version} to insert the project name and version respectively, e.g:

archiveFileName.set("${project.name}-${project.version}.jar") // .jar name: project-1.0.jar
Toggy Smith
  • 330
  • 2
  • 7
3

Read the doc of gradle,the baseName and archiveName is deprecated,you should use archivesBaseName in project level like this:

project(":web") {
    description = "web"
    archivesBaseName = "your-project-name-" + getVersionCode()

    jar {
        // Will include every single one of your dependencies, project or not
        from {
            configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
        }
    }
}

It works fine for my project. I hope it will be helpful for your project.

Dolphin
  • 29,069
  • 61
  • 260
  • 539
2

For Gradle 6.x

Set the version to empty.

  • version = ''
  • project.archivesBaseName - It is the way.

More on their DOC!

TyCy
  • 19
  • 2
2

For Gradle 5.x, 6.x and 7.x
(tested personally on Gradle 7.6 and verified the documentation of version 5)

In more recent versions of Gradle the results described by the OP are achieved automatically by the default building script if you specify the version property in your module build.gradle file (commonly seen right after your plugin block and before any other blocks).
So it would look something like this:

plugins {
    // your plugins...
}

// This is the string you want to have.
version = '1.0.0'

dependencies {
    // your dependencies
}

version accepts any string, so you can also append pre-release strings like 1.0.0-beta if you need to.

However, one more thing to note is that by default Gradle uses the module name as the base name or your archives and jar files. If your module name matches the name of your library this shouldn't be a problem, but a common scenario it to work with sub-modules where you usually have a lib or core module, and in this case your jar file will end up being named something like lib-1.0.0.jar. To avoid this, you can specify the archivesBaseName property. You can put it right below (or above) your version:

version = '1.0.0'
archivesBaseName = 'abc'

This will correctly generate a abc-1.0.0.jar file.
Note that archivesBaseName uses the plural archives, and it comes from the Project type, and NOT the singular form archive that comes from the jar task.

But, if you need more granularity other than what described above, you will indeed need to use the jar task, similarly to how the OP originally intended to.

1

In build.gradle.kts, I was able to customize jar naming as follows:

tasks.jar {
    archiveFileName.set("application.jar")
}

tasks.bootJar {
    archiveFileName.set("application-plain.jar")
}
TheJeff
  • 3,665
  • 34
  • 52
0

Add Apk base name in gradle

archivesBaseName = "appname-${new Date().format('dd-MM-yy-HHmm')}"

Gradle file

defaultConfig {
    applicationId "com.example.app"
    minSdk 23
    targetSdk 33
    versionCode 5
    versionName "1.0.0"
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    archivesBaseName = "appname-${new Date().format('dd-MM-yy-HHmm')}"
    resValue 'string', "authority", applicationId + ".fileprovider"
}
Sumit Ojha
  • 283
  • 2
  • 8
-1

If you want to rename the JAR, source JAR and javadoc JAR with a single setting, while still including the classifier and version, set archiveBaseName at the top level.

RMorrisey
  • 7,637
  • 9
  • 53
  • 71
-1

<pre><code>
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'application'


jar {
    archivesBaseName = archivesBaseName + '-' + version
}

</pre></code>

For me I just add above

Aung Aung
  • 227
  • 1
  • 2
  • 6