Use a Spring Boot Application as a Dependency with gradle - java

I have got some trouble using a spring boot (2.1) application as dependency for an other spring boot application.
I'm aware that this is not the recommended approach, but for simplicity reasons I would like to go that route. The offical documentation just shows how to do that with maven and not how to do that with gradle.
Project A:
plugins {
id "org.springframework.boot" version "2.1.1.RELEASE"
}
apply plugin: 'io.spring.dependency-management'
Project B:
plugins {
id "org.springframework.boot" version "2.1.1.RELEASE"
}
apply plugin: 'io.spring.dependency-management'
dependencies {
compile project(':Project A')
}
This however results that the application.properties (from resources) of project A are getting loaded though project b is executed.
Anyone some tips or can direct me to a working simple example?
Update:
By adding the following it works, when building the final spring boot jar. Unfortunately the problem still exists, when trying to execute it directly in Intellij.
jar {
enabled= true
exclude("**/application.properties")
}

I assume that ProjectB depend on ProjectA becuase you need some common function or service ? Maybe you should extract some common module from ProjectA first. For example
ProjectA-common
ProjectA-app (runnable application with application.properties)
Then you can import ProjectA-common as library without troule at application.properties.
If you still have to include all ProjectA , you can exclude specific configuration from properity. And write a new one to overwrite it.
spring:
profiles: dev
autoconfigure:
exclude:
- com.example.config.ProjectAConfiguration
- org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration

Related

Enable Vaadin Flow Production Mode in Spring Initializr Gradle Project

I run into confusion while trying to set a com.vaadin:vaadin-spring-boot-starter Gradle project into production mode.
The project is part of a multi-module project and its (simplified) build.gradle file looks like this:
buildscript {
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
dependencyManagement {
imports {
mavenBom "com.vaadin:vaadin-bom:${vaadinVersion}"
}
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-actuator")
compile("com.vaadin:vaadin-spring-boot-starter")
// ... futher more
}
I've found several projects and documentation focusing this topic, such as:
Vaadin Maven Plugin
Flow Maven Plugin
Gradle Vaadin Flow Plugin
Setting Flow into Production Mode with Maven
My current state is that I added vaadin.productionMode=true to the application.yaml file, which causes the following error on HTTP GET:
Failed to find the bundle manifest file 'frontend://vaadin-flow-bundle-manifest.json' in the servlet context for 'ES6' browsers. If you are running a dev-mode servlet container in maven e.g. jetty:run change it to jetty:run-exploded. If you are not compiling frontend resources, include the 'vaadin-maven-plugin' in your build script. Otherwise, you can skip this error either by disabling production mode, or by setting the servlet parameter 'original.frontend.resources=true'.
So in general, I think that I'm on the right way.
Setting the servlet parameter 'original.frontend.resources=true' removes the error, but it seems like a work-around to me, thus I want to avoid it.
Disabling production mode is obviously not an option :-)
So my question is: How can I include the vaadin-maven-plugin in my Gradle build script. As I am using Flow only, should I rather use the flow-maven-plugin?
Update 1: I want to set a Spring Initializr Gradle project with Vaadin dependency into production mode. I do not want to create a new gradle-vaadin-flow-plugin project.
The Gradle equivalent of vaadin-maven-plugin would be com.devsoap.vaadin-flow (1), and you also need to configure vaadin { productionMode = true } in build.gradle (2)
It's also possible to configure the gradle property so that it depends on a build time parameter, as explained here: configure vaadin.productionMode = findProperty('productionMode') ?: false in build.gradle, and add a placeholder in the #VaadinServletConfiguration that will be preprocessed when building the project.

Gradle compile project failing with spring boot plugin

I'm trying to understand why adding the Gradle 4.x Spring Boot plugin to a Gradle dependency is causing my build to fail. Setup based on this link:
Project
|--build.gradle //plugin here is fine
|--settings.gradle
Dependency
|--build.gradle //plugin here causes failure
|--com.activemq.common //dependency I want to import
In Dependency/build.gradle if I just have:
//Dependency/build.gradle
apply plugin: 'java'
gradle build --> This works as expected
Now if I add the Spring Boot Plugin it fails:
//Dependency/build.gradle
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
repositories {
mavenCentral()
}
I get an error that it can't find a package that's under Dependency
gradle build --> Application.java:5: error: package com.activemq.common does
not exist
I can just remove the plugin but the dependency is also Spring Boot so I would like to have it.
I tried doing gradle build --info, but didn't see anything useful. Also tried Gradle 5 but got different errors that I'm still investigating.
Can anyone explain why adding the plugin would cause this failure?
Looks like I needed to fix 2 things
I could not use a typical gradle.build like above, but had to use a special "Spring Boot’s dependency management in isolation" version which uses "mavenBom" - this example worked for me https://github.com/spring-guides/gs-multi-module/blob/master/complete/library/build.gradle.
I also could not use a settings.gradle in the Project/Dependency folders, but rather needed to put a settings.gradle at the top level like this:
https://github.com/spring-guides/gs-multi-module/blob/master/complete/settings.gradle
This didn't work:
Project
|--settings.gradle
Dependency
|--settings.gradle
This worked:
Project
|
Dependency
|
settings.gradle

Spring boot and Gradle multi-modules project, failed to load dependencies correctly

Basically I have a spring boot project build with Gradle.
The project has a root project that contains another 4 sub-modules.
The root project settings.gradle looks like this:
rootProject.name = 'proj'
include 'proj-app'
include 'proj-integration-tests'
include 'proj-model'
include 'proj-service'
The app module contains the spring-boot-gradle-plugin and exposes some api's.
What I wanted to do was to create proj-integration-tests sub-module that only contain the integration tests. The problem start here since I needed the proj-app dependency.
So in proj-integration-tests I have the build.gradle that contains:
dependencies {
testCompile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile project(':proj-app')
testCompile project(':proj-model')
}
and I needed the proj-app dependency since the integration test:
#RunWith(SpringRunner.class)
#SpringBootTest(classes = ProjApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
required the Spring boot application to start(ProjApplication.class) that is located in proj-app module.
The error that I got from Gradle is: "cannot find symbol ProjApplication".
Why Gradle could not manage properly the proj-app dependency?
Thanks in advance ;)
It seems that the proj-app dependency is build in a spring boot fashion way. This means that the artifact obtained is a executable spring boot far jar.This is why tha proj-integration-tests could not found the classes from proj-app at the compile time.
So in order to maintain the executable jar, and to have proj-app as a dependency in proj-integration-tests module, I've modified the build.gradle from proj app to create both jars: in a spring boot fashion way and the standard version:
bootJar {
baseName = 'proj-app-boot'
enabled = true
}
jar {
baseName = 'proj-app'
enabled = true
}

Gradle 4 - test dependency on another project's tests

I have the following multi project structure:
/
build.gradle
common/
build.gradle
src/main/
resources/common.properties
java/...
src/test/
resources/common.properties
java/...
app/
build.gradle
src/main/java/...
src/test/java/...
admin/
build.gradle
src/main/java/...
src/test/java/...
common project contains some common methods and they have their own unit tests and one CommonDefs class which contains data loaded from common.properties. When running unit tests, the common.properties file from the test resources supposedly overrides the one in the main resources, and the tests do work as expected.
The problem begins when running unit tests for app and admin projects - which contain tests that use CommonDefs from the common project.
Up until now I've used a generic solution similar to the method described in the following SO answer and it has worked perfectly for a couple years. This is what the root build.gradle contains:
allprojects {
plugins.withType(JavaPlugin) {
configurations {
testOutput
}
dependencies {
testOutput sourceSets.test.output
}
}
}
and app/build.gradle & admin/build.gradle both contain:
dependencies {
compile project(":common")
testCompile project(path: ":common", configuration: "testOutput")
// ...
}
I've decided it was time to upgrade Gradle (previously used 2.14.1), so I started testing the project against Gradle 4.2.1. Now every time I run the tests for app and admin, CommonDefs loads data from src/main/resources/common.properties instead of src/test/resources/common.properties.
I've tried printing the test classpath for the app project and it seems that Gradle 4.2.1 takes the common's main JAR first:
path\to\app\build\classes\java\test
path\to\app\build\resources\test
path\to\app\build\classes\java\main
path\to\app\build\resources\main
path\to\common\build\libs\common.jar
path\to\common\build\classes\java\test
path\to\common\build\resources\test
with Gradle 2.14.1 the result was:
path\to\app\build\classes\test
path\to\app\build\resources\test
path\to\app\build\classes\main
path\to\app\build\resources\main
path\to\common\build\classes\test
path\to\common\build\resources\test
path\to\common\build\libs\common.jar
Is there a way to tell the newer version of Gradle to prioritize the test output of common over its main output?
N.B
I also tried using the following in app/build.gradle:
evaluationDependsOn(":common")
dependencies {
compile project(":common")
testCompile project(":common").sourceSets.test.output
}
which seems to work, but it looks rather hacky and I'd like to stick to the solution with the configuration if possible.

Hibernate core being included from spring boot

I have a gradle project that has the following dependencies:
dependencies {
compile("com.googlecode.json-simple:json-simple:1.1.1")
compile("org.hibernate:hibernate-c3p0:5.2.12.Final")
compile("mysql:mysql-connector-java:5.1.44")
compile("org.springframework.boot:spring-boot-starter-aop")
compile("org.springframework.boot:spring-boot-starter-web")
compile group: 'org.springframework.boot', name: 'spring-boot-starter-log4j2'
}
And has the following to apply the spring boot plugin:
apply plugin: 'org.springframework.boot'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.9.RELEASE")
}
}
The problem I am having is that when I include the spring boot plugin, an older version of hibernate-core seems to be being imported into my project (5.0.12.Final). But my code uses the 5.2.12.Final hibernate-core library.
I can't understand exactly why the hibernate core library comes with the spring boot plugin, as I can't see it listed in its dependencies on maven central, however when I remove that dependency, the older version of hibernate seems to disappear.
I've tried excluding the module when declaring the dependency but that doesn't seem to be syntactically correct when excluding in the buildscript section.
Has anyone else had this problem? Any workarounds to exclude that version? Or maybe my setup all together is wrong.. Any help would be much appreciated :)
Finally figured this one out after many hours/
Seems obvious now after a bit more research, the problem was due to spring boots own dependency management, so all I have to do is specify the version of a particular module I want to use (if spring boot already includes it), and it worked! Here is what I added to my build.xml file
dependencyManagement {
dependencies {
dependency 'org.hibernate:hibernate-core:5.2.12.Final'
}
}

Categories

Resources