How to make bootRepackage depends on jar not war when using Gradle War Plugin - gradle

Without Gradle War Plugin, bootRepackage task depends on jar task but with Gradle War Plugin, it depends on war task.
How can I change it to depend on jar task even though I'm using Gradle War Plugin?
UPDATE:
I'm using war task to create a war file including documents to be deployed to a documentation server and I want to use bootRepackaged jar file to provide a service. My war task depends on asciidoctor task which depends on test task (I'm using Spring REST Docs.) but I don't want to run asciidoctor task or test task when using bootRepackage task.
I solved my problem with the following setup:
ext {
mainClassName = 'com.izeye.throwaway.Application'
}
task myBootRepackage(type: BootRepackage, dependsOn: jar) {
}
but I'm not sure this is a good practice.
This is a sample project having the above configuration:
https://github.com/izeye/spring-boot-throwaway-branches/tree/war

You should have been able to do this:
bootRepackage {
withJarTask jar
}
While this correctly causes the jar task's jar to be repackaged, it doesn't remove the dependency on the war task. This is another symptom of this Spring Boot issue.
Until this issue has been resolved, the approach that you've taken – declaring your own BootRepackage task and manually configuring the tasks that it depends upon – is your best option.

Related

is there a way in gradle to update an existing jar?

The stand-alone jar tool has the "u" option to add files to an existing jar.
Is there a built-in way to use the gradle jar api to do the same thing?
I would like to avoid using a gradle exec task to invoke the jar tool.
gradle does not update existing jars
you can achieve merging in multiple ways depending on you needs
ex: what takes precedence (old files or new files)
jar {
from zipTree('junit-4.12.jar')
}
or you can have extra Jar task where you explicitly do the same
task mergedJar(type: Jar, dependsOn: jar) {
archiveClassifier = 'merged'
from zipTree('junit-4.12.jar')
from zipTree(jar.archiveFile)
}

How can I create a gradle task to generate exploded war with Sprint boot "bootWar" plugin?

I use the "org.springframework.boot" plugin and use the bootWar task to generate a war file of my spring boot project.
I would like to have a task that creates an exploded version of this war file.
The standard way to do that with the 'war' plugin is :
task explodedWar(type: Sync) {
into "${war.archivePath.parentFile.getAbsolutePath()}/exploded/${war.archivePath.name}"
with war
}
How can i do the same thing with the spring-boot/bootWar plugin?
Try with:
task explodeBootWar(type: Sync) {
dependsOn bootWar
into "$buildDir/boot_war_exploded"
from project.zipTree(bootWar.archiveFile)
}
You can use the with method on the normal war task because it is basically just a copySpec. However, the bootWar task does some additional things, so you have to build and unzip the actual archive.

Build Gradle JAR without dependencies

From command line, I need to build an executable jar without dependencies.
The current "gradle build" command gives me a jar with dependencies.
Couldn't find this on StackOverflow. If it's a duplicate question, point me there. Thanks.
As you have SpringBoot plugin enabled, the default behavior is to build an executable jar (fat-jar) containing all dependencies, through thebootJar task.
You can still generate a single "standard" jar if you need, this is explained in the documentation : Spring Boot Gradle plugin
jar {
enabled = true
}
bootJar {
classifier = 'boot'
}

Modifying gradle task in a subproject

So, let's say I have this kind of project structure:
Root
projA
projA1-api
projA2-core
projA3-main
projB
projB1-api
projB2-core
projB3-main
projC
projC1-api
projC2-core
projC3-main
and so on...
Some of the subprojects (the ones ending in "-main") have war and spring boot plugins applied to them, the root build.gradle file does not.
That means that projects projA3-main, projB3-main and projC3-main have the bootWar task which disables the war task.
Now, the problem lies within the fact that our jenkins pipeline builds wars by executing the gradle war command, which sucks and can't be changed.
So, what I want to do is to modify the war task in each of the subprojects containing the war plugin by setting it to depend on the bootWar task. I can do this by adding war.dependsOn bootWar into the subprojects' build.gradle files and that works, but I want to extract that code into the root build.gradle.
Also, I want the war task to also execute another task (let's call it customPrintTask) defined in the root build.gradle which just prints stuff into some file.
To summarize:
Root build.gradle doesn't have, war, bootWar or Spring Boot plugins, but has the customPrintTask task
"-main" subprojects have bootWar and spring boot plugins, and they also have the war plugin, but because I am using Spring Boot 2+ gradle plugin, the war task does not generate the war.
Basically, I want something like this:
allprojects {
if (project.plugins.hasPlugin("war")) {
war.dependsOn bootWar
war.finalizedBy customPrintTask
}
}
I hope that makes sense.
Yo, figured it out.
allprojects {
tasks.withType(War) {
if (it.name != "bootWar") {
dependsOn(customPrintTask)
dependsOn(":" + it.project.name.replace("-main", "") + ":" + it.project.name + ":bootWar")
}
}
}
I know no one cares, but this way I iterated over every task with the type War and if the tasks name is not bootWar(because of circular dependency) then depend on my custom print task and also depend on the bootWar task which is located in the subproject I am currently iterating over, which is the reason it looks dumb.
It's pretty simple, not sure how I missed this one...looks extremely ugly, but it works!

Specify gradle configuration or customize dependencies in bootJar task?

Our application is using EclipseLink. For production artifacts we use static weaving, we have a Gradle task that builds a separate jar that should be included in the Spring Boot fat jar. During development we are not using weaving so we don't have this artifact.
What we would like to do is customize the classpath in the bootJar task so that we include the weaved artifact and exclude the source of the un-weaved module. Prior to 2.0.x of the Spring Boot Gradle plugin this was achieved by specifying a customConfiguration in a task of type bootRepackage, like this:
task singleJar(type: BootRepackage) {
customConfiguration = "weavedRuntime"
}
But this option seems to be missing in the 2.0.x version. Is there any way of overriding the configuration in the new version?
Alternatively we need to modify the classpath, but just for the bootJar task. The normal runtime classpath shoukd include the un-weaved module, but the bootJar classpath should include the weaved artifact and exclude the un-weaved module. Any suggestions on how to do this?

Resources