How to add a runtime library on Gradle in the build command - gradle

I have a build.gradle file that I want to use to build a Spring Boot microservice for multiple projects. I have created a custom library with some classes that should not be in all of the projects but I don't want to have to edit the build.gradle file when I build the microservice depending on which project I will use it in.
How can I add a command/parameter to gradle build that can add runtime libraries to the Spring Boot app?
Something like (just an exaple):
gradle buildDocker -PaddRuntime=com.skios.lib:lib-common:0.2.35
Thanks for any directions

I am not sure I understand your issue fully and thus not convinced this is the best solution.
But since a Gradle build script accepts code, you can have conditionals in a dependencies block:
dependencies {
if (project.hasProperty('addRuntime')) {
runtimeOnly('com.skios.lib:lib-common:0.2.35')
}
}
and then on the command line: ./gradlew buildDocker -PaddRuntime

Related

ArchUnit: How to get gradle to execute the tests?

I have a gradle project where I've added the archunit-junit5 dependency and written some test classes with #ArchTests. These get picked up by IntelliJ.
How do I get gradle to execute them?
I've found the com.societegenerale.commons:arch-unit-gradle-plugin but that seems to need configuration in the gradle file.
I just want gradle to pick up the tests I already have in the test/java directory.
Gradle should pick up #ArchTests with archunit-junit5 if you useJUnitPlatform().
https://github.com/TNG/ArchUnit-Examples/tree/main/example-junit5 shows a quite minimal working example.

Use java S2I for spring boot app with gradle build in openshift

How to get started with java S2I build for spring boot application with Gradle build?
Please suggest me any Json/yml file needs to be imported to get started.
Here is a whole repo - https://github.com/dsevost/gradle-s2i - with a Dockerfile to build a S2I gradle builder image, and a template to build applications on top of it.
Perhaps it is worth noting the BUILDER parameter in the template with possible values of 'Gradle' or 'Maven'. According to the assemble script, it will build a gradle project even when both pom.xml and build.gradle are present in the project directory.
I haven't tried it myself but I glanced over the template and it looks OK...assuming the image and S2I scripts are functional.

gradle and dependency on spring-boot subproject

I've created a project with several gradle subprojects, including: "app" and "tests".
Tests have "app" in their dependencies. Tests use classes from "app"
When I run:
./gradlew clean test build
Everything works, tests run and pass.
But when I run:
./gradlew clean build
then the tests compilation fails with an error saying that a class is missing - in this case it's a spring-boot configuration class. I run this with --debug and it turns out that in the failing case app:bootRepackage task is executed before tests:test, the jar generated by app compilation is altered and that's why the classes cannot be found.
How can I make "./gradlew clean build" work properly?
Using: spring-boot 1.5, gradle 4.0 (and 4.1 too), io.spring.dependency-management plugin 1.0.0.RELEASE
Ideally, you shouldn't use a Spring Boot application (something that's been repackaged) as a dependency. From the documentation:
Like a war file, a Spring Boot application is not intended to be used as a dependency. If your application contains classes that you want to share with other projects, the recommended approach is to move that code into a separate module. The separate module can then be depended upon by your application and other projects.
If the proposed solution isn't possible in your situation, the documentation goes on to describe an alternative:
If you cannot rearrange your code as recommended above, Spring Boot’s Maven and Gradle plugins must be configured to produce a separate artifact that is suitable for use as a dependency. The executable archive cannot be used as a dependency as the executable jar format packages application classes in BOOT-INF/classes. This means that they cannot be found when the executable jar is used as a dependency.
To produce the two artifacts, one that can be used as a dependency and one that is executable, a classifier must be specified. This classifier is applied to the name of the executable archive, leaving the default archive for use as dependency.
To configure a classifier of exec … when using Gradle, the following configuration can be used:
bootRepackage {
classifier = 'exec'
}

Run specific project using gradle

I have a Groovy application, I am using Gradle as my build tool. I have 2 seperate projects a Ratpack project with it's own build.gradle file and then a React project with it's own build.gradle file.
When I do ./gradlew run It builds my Ratpack project which in my Ratpacks build.gradle file has
compile project(':react-app')
In the dependencies closure.
Is there a way to just be able to run one of the projects for example ./gradlew runRatPack which stops the React project from being built?
If you can change the following line
compile project(':react-app') which states there is a Project Dependency to
compile name : 'react-app' - artifact dependency, then the project will try to pick the artifact from the repository instead of building the project.
Please let me know if the issue is addressed and this solves your issue.

How to use classes from a neighbouring subproject during configuration phase

I want to compile one subproject, then have those classes on the classpath while building the other subproject. (A custom tass would use classes created by the first one).
Currently I'm trying:
buildscript {
dependencies {
classpath project(':MyOtherProject')
}
}
... with the following result:
Cannot use project dependencies in a script classpath definition.
You cannot build something to be used to build the something. (something being the multi-project build here)
You either need to make the MyOtherProject a complete separate build, that you either install to some repository and then depend on it per coordinates or trigger that separate build during configuration phase or your build, then depending on its outcomes.
The other alternative is, that you put it into buildSrc project. This is a full multi-project build contained in your root project that is built and added to the classpath of the main build scripts automatically by Gradle and is meant for Plugins and Custom tasks that you do not want to use in other builds also and thus do not make them a separate build.

Resources