Search code examples
gradlesshgradle-pluginspring-boot-gradle-plugingradle-task

Reference spring-boot assembled jar in gradle ssh plugin


I would like to create a custom task in gradle, that will upload assembled jar to a remote server.

My best working result is

doLast {
    ssh.run {
        def jarFile = "${jar.baseName}-${version}.jar"
        session(remotes.role('qa')) {
            put from: file("build/libs/${jarFile}"), into: "/home/${user}"
            execute("sudo cp ${jarFile} /usr/share/feedserver/", pty: true)
            ...
        }
    }
}

However this uses jar.baseName property, which is deprecated and gives a warning:

The baseName property has been deprecated. This is scheduled to be removed in Gradle 7.0. Please use the archiveBaseName property instead.

What would be the right way replacing it ?

Suggested archiveBaseName does not work, E.g.

task updateOnQA(dependsOn: bootJar) {
    doLast {
        def jarFile = "${jar.archiveBaseName}-${version}.jar"
        print "${jarFile}";
    }
}

Outputs

task ':myproject:jar' property 'archiveBaseName'-1.0.12.jar


Solution

  • The deprecated baseName property returns a String, whereas the new archiveBaseName returns a Provider<String>. To get the actual value from the provider, you have to call .get(). The same goes for the version property, which has been replaced by the provider called archiveVersion.

    If you want to use those new properties, you could update your task to this:

    task updateOnQA(dependsOn: bootJar) {
        doLast {
            def jarFile = "${jar.archiveBaseName.get()}-${archiveVersion.get()}.jar"
            print "${jarFile}"
        }
    }
    

    But if you are looking for the actual path of the jar file, you could use:

    put from: jar.archiveFile.get().getAsFile(), into: "/home/${user}"
    

    Here, the first .get() call returns a RegularFile from the provider, and the .getAsFile() call converts it to a normal File.