Specify gradle configuration or customize dependencies in bootJar task? - gradle

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?

Related

[Forge 1.8.9]: Including dependencies in JAR

I've been making a Forge mod for Minecraft 1.8.9 with the Forge MDK. So far my mod has 1 dependency, which is SnakeYAML. I added this line: compile 'org.yaml:snakeyaml:1.30' to my build.gradle file, so that I have SnakeYAML during development, but it doesn't get included when I build the JAR. Is there any way to include SnakeYAML in my JAR as well?
You can create a fat Jar. In summary, a fat Jar contains all of the dependency classes and resources in a single output Jar. I assume you're using Gradle as this is a Forge project.
Add the Shadow plugin
plugins {
id 'com.github.johnrengelman.shadow' version '7.1.2'
id 'java'
}
Then configure your dependencies to shadow in SnakeYAML:
dependencies {
implementation 'org.yaml:snakeyaml:1.30'
...
shadow 'org.yaml:snakeyaml:1.30'
}
Finally, use the ShadowJar task added under 'Shadow' to build your fat Jar. You may need to change the version of Shadow for the gradle version you're using. Refer to the documentation for any configuration you may wish to add.

spring-boot with the gradle java java-plugin

I use gradle with the new java-library plugin.
With this plugin we can use new configurations for dependencies instead of 'compile', 'runtime', 'test' etc.
see java-library plugin documentation
But with the spring-boot plugin, when I launch the task
gradle build
The produced jar does not contain the dependencies of the project.
It is because I use the 'implementation' configuration.
If I use 'compile' configuration as I did before, it works.
Is there a solution, to use these new configurations ?
Or do you plan to implement this new feature in the next versions of spring-boot.
Thank you :)

gradle bootRun > Use test classpath

The problem I was having, was that I wanted to include test classpath resources in SpringBoot's bootRun gradle task. Why? So that I could use a test profile with test resources, to mock integration points.
What I tried:
The spring boot documentation only offers the addResources = true option (I tried using customConfiguration as per the similar bootRepackage configuration, to no avail)
No additional options are visible by looking at the BootRunTask source code
The equivalent maven plugin has a plethora of options, including useTestClasspath (which isn't mirrored in the gradle version)
I came across the following solution, which solved this issue for me.
Basically, the BootRunTask extends the standard JavaExec task, which offers a classpath option. So, you can add the test classpath resources by using the following gradle configuration:
bootRun {
classpath = sourceSets.test.runtimeClasspath
}

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

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.

Set classpath from Gradle Plugin

When writing a gradle plugin, is it possible to add dependencies to the compile and testCompile classpath for projects that apply the plugin?
If so, is there a simple example that you can reference?
As an example; let's say I wanted to write a plugin that, among other things, added the AWS Java API jars to a project i.e. I get the the jars on the classpath of the project where I apply the plugin allowing me to compile against them.
Thanks
I am not fully sure I understand the question but you can look at the gradle war plugin (https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/groovy/org/gradle/api/plugins/WarPlugin.java) which defines new tasks providedCompile which extends compile
Edit : making plugin with its dependencies
In your example, its totally possible that the plugin defines its own dependencies like in a normal build.gradle file
repositories {
mavenCentral()
}
dependencies {
compile "com.amazonaws:aws-java-sdk-ec2:1.10.2"
}
see for example the was plugin (https://github.com/classmethod-aws/gradle-aws-plugin/blob/develop/build.gradle) when you apply this plugin in your own build the aws dependencies will be downloaded and available to your build.

Resources