Hello there, I am writing a ci script and I want t...
# community-support
s
Hello there, I am writing a ci script and I want to be able to control the file name of the jar that gets created when
gradlew bootJar
is executed from the terminal. Is there a flag associated with the
bootJar
task that can allow me to change the filename? I know it can done by configuring the
build.gradle
file but that's not what I am looking for.
v
You mean a commandline flag? Probably not, but just check it
./gradlew help --task bootJar
. If not, you could have an init script that makes the necessary changes and include that using
-I
.
s
hmm....that command shows only a single option to "rerun". Ok, so my real need is that I am building a spring boot application inside a dockerfile and I want the jar file to be the same name no matter the
version
in
build.gradle
.
v
Set
version
on
bootJar
to
null
I guess
s
Hmm..I really dont want to have a static configuration to name the bootjar as I would not want people trying to build locally facing issues. I guess I can backup the
build.gradle
file edit it with sed right before I build and restore the backup after build. Seems very hacky...
I have another idea that might help me. Is it possible to know the file name of the jar that's created?
c
perhaps rename the jar as you move it to your container.
s
@Chris Lee The jar is getting built in the container (multistaged)
c
sure, simply rename it once built.
s
Getting to know the name that it got built with is the challenge here. I can do some bash stuff to get the jar name but I was looking if there was some functionality in gradle itself to get it.
c
mv build/libs/<name>-*.jar <dest>/<name>.jar
or somesuch will do it (<name> is generally the known project name, the wildcard handles the version), if you’re not able to modify the build script accordingly.
s
As I said, writing bash is not a challenge for me and that's not what I am asking help for. Its getting the value of <name> right after build, without needing to grep the build.gradle.
c
if you are building a known project,
<name>
is a well-known value, generally the project name. Is there some additional unstated requirement preventing using that well known name?
s
I would know the name beforehand and it would be static, true, but the jar gets suffixed with the version which isn't static, and I would need to grep the build.gradle file to get that value first
c
No, as noted just wildcard the variable portion:
mv build/libs/<name>-*.jar <dest>/<name>.jar
s
Hmmm, now that I think about it, that certainly solves my problem the first way. Its not like a single bootrun will create multiples of a jar with the same name and different versions
c
you can check what is created and adjust the wildcard/glob pattern accordingly.
…you will get multiple versions if the build isn’t epehemeral (or calling
clean
before building)
s
Hmm, that's certainly one way. I was hoping that gradle would help be somehow so that I don't have to run any scripts, but this is pretty much a one liner and can't get better than that. Thank you for the ideas. I will work on this tomorrow, back to bed for me 🙏
👍 1
v
I already told you how to do it with Gradle. Write an init script that sets the
version
property of the
bootJar
task to
null
and use it with
-I
parameter or by putting it to
~/.gradle/init.d/
.
s
@Vampire ah sorry, I forgot to mention that I was having issues doing that with an init script. I have tried a few ways but neither of them works. Here's a few scripts I have tried:
Copy code
bootJar {
    version = null
}
Gives the error:
Could not find method bootJar() for arguments [init_4dts4ab5klk4h2jow1p9qaoa7$_run_closure1@557b2fb0] on build of type org.gradle.invocation.DefaultGradle.
Copy code
gradle.projectsEvaluated {
    bootJar {
        version = null
    }
}
Same error as above
Copy code
gradle.allprojects {
    task bootJar {
        version = null
    }
}
Gives the error:
Cannot add task 'bootJar' as a task with that name already exists.
This one is due to
build.gradle
also creating that task, so I wrote the next one
Copy code
gradle.projectsEvaluated {
    tasks.whenTaskAdded { task ->
        if (task.name == 'bootJar') {
            project.version = null
        }
    }
}
Gives the error:
Could not get unknown property 'tasks' for build
. So next I tried using
allprojects
instead and that builds but doesn't change the jar name. Sorry for the wall of text but I am at a loss here. I would love to use a gradle way of doing this still but I just cant seem to figure out the correct script.
v
If course setting the project version does not work and might even break other things.
BootJar
is a subclass of
Jar
, so the property you want to set is
archiveVersion
. And don't use
whenTaskAdded
, that completely breaks task configuration avoidance. Use
tasks.named { it == "bootJar" }.configureEach { ... }
.
s
Hmm...I am really sorry but things are just not working. I tried
Copy code
gradle.projectsEvaluated {
    tasks.named { it == "bootJar" }.configureEach {
        archiveFileName.set("my.jar")
    }
}
and
Copy code
tasks.named { it == "bootJar" }.configureEach {
    it.archiveFileName.set("my.jar")
}
They are all giving me the error
Could not get unknown property 'tasks' for build of type org.gradle.invocation.DefaultGradle.
Instead doing
Copy code
gradle.allprojects {
    tasks.named { it == "bootJar" }.configureEach {
        archiveFileName.set("my.jar")
    }
}
is giving the error
No signature of method: org.gradle.api.internal.tasks.DefaultTaskContainer.named() is applicable for argument types
. So, I tried doing
Copy code
gradle.allprojects {
    tasks.named ("bootJar").configureEach {
        archiveFileName.set("my.jar")
    }
}
which gives me
Task with name 'bootJar' not found in root project
. Honestly, I have no idea what I am doing wrong...
v
tasks.named { ... }
works lazy,
tasks.named(...)
not. But
tasks.named { ... }
is pretty young. If it complains that it does not exist, your Gradle version is too old. Either use a recent Gradle version, or alternatively use
tasks.matching { it.name == "bootJar" }.configureEach { ... }
s
Great! That works! My current project is on 7.6.1 so I guess its too old for that. Thank you for helping me get to the bottom of this. Much appreciate the help by you both
👌 1