Gradle - jar file name in java plugin

前端 未结 9 1832
借酒劲吻你
借酒劲吻你 2021-02-01 11:46

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

相关标签:
9条回答
  • 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'
    
    0 讨论(0)
  • 2021-02-01 12:27

    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")
        }
    }
    
    0 讨论(0)
  • 2021-02-01 12:29

    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

    0 讨论(0)
  • 2021-02-01 12:30

    If you are using a newer Gradle version, baseName, archieveName will now be deprecated. Instead, use something like

    jar {
       archivesBaseName = 'project1'
       archiveVersion = '1.0-SNAPSHOT'
    }
    
    0 讨论(0)
  • 2021-02-01 12:34

    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

    0 讨论(0)
  • 2021-02-01 12:37

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

    0 讨论(0)
提交回复
热议问题