Gradle compile project failing with spring boot plugin - java

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

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.

Use a Spring Boot Application as a Dependency with gradle

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

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'
}
}

Trying to include Gradle project as dependency, but include() not working

I have a Gradle project which depends on another Gradle project. My project structure is like this:
project1
build.gradle
settings.gradle
src/
project2
build.gradle
settings.gradle
src/
in project1/build.gradle I want to add project2 as a dependency:
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
sourceSets {
main {
java {
srcDirs = [ 'src' ]
}
}
}
include ':project2'
project(':project2').projectDir = new File("../project2")
dependencies {
compile project(':project2')
}
Unfortunately, I'm always getting this error message:
Error:(21, 0) Could not find method include() for arguments [:project2] on root project 'project1' of type org.gradle.api.Project.
I'm using Gradle 3.5 and I'm getting this error both on the command line (gradle build) and in IntelliJ. I found a few StackOverflow threads about the same issue (this and this), but they were not helpful.
The Gradle multi-project documentation also doesn't mention any specific requirements which I may be missing that can cause the error.
When I leave the include call out, I get the message that the project path could not be found in the root project.
I also tried moving the dependency to a subdirectory of project1, but without success.
I wonder what I'm doing wrong and why apparently not many other people are having the same problem. I'd be grateful for hints.
Note: this is not an Android project.
As pointed out in the first comment, include actually needs to go into settings.gradle and not into build.gradle. The same applies to changing the projectDir property.
Comment 3 gave also me another idea. The project can be included in settings.gradle as follows:
includeBuild '../project2'
and in project1/build.gradle I specify the dependency as
dependencies {
compile 'group:project2:version'
}
I generally like this better, since it's less code and looks cleaner. The downside, however, is that recursive composite builds aren't possible. So if project2 itself is also a composite build, this won't work.

Why do xml configs of gradle dependencies go with "Gradle__" prefix? Intellij IDEA 13

Java Spring project with Gradle 1.9 and vertx. Local gradle distribution.
Some lines of build.gradle
apply plugin 'java'
apply plugin 'groovy'
apply plugin 'idea'
buildscript {
repositories {
jcenter()
}
}
repositories {
mavenCentral()
}
dependencies {
...
compile 'org.springframework:spring-context-support:3.2.5.RELEASE'
compile 'org.springframework:spring-aop:3.2.5.RELEASE'
compile 'org.springframework:spring-aspects:3.2.5.RELEASE'
...
}
I have an existing gradle project downloaded from git with xml configs in .idea/libraries folder named spring-aop_3_2_5_RELEASE.xml, for example, where we can find xml tag <library name="spring-aop-3.2.5.RELEASE">...</library>.
After I had imported this project new file Gradle__spring-aop_3_2_5_RELEASE.xml appeared with only difference in name attribute of the library tag: Gradle: spring-aop-3.2.5.RELEASE. So i have duplicate xml configs for dependencies. I wonder why my gradle added that prefix.
The prefix is hardcoded, IDEA 13 needs a reimport of your old Gradle projects that were created in IDEA 12. It's not obvious, but there will be a notification about it in the next update.
In the Gradle generated project you can exclude the library files from the version control, same for the .iml files that can be also ignored when using Maven. Other files can be still shared (like code style, run configurations, inspection profiles, etc). Check this document for details.

Categories

Resources