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'
}
}
Related
I have a Gradle-based project with root build.gradle declaring dependency on Spring Boot BOM:
ext {
springBootVersion = '2.6.3'
}
subprojects {
dependencies {
implementation platform("org.springframework.boot:spring-boot-dependencies:$springBootVersion")
}
}
then in a subproject I have dependencies on liquibase-core and liquibase-cdi:
ext {
liquiBaseVersion = '4.4.3'
}
dependencies {
runtimeOnly "org.liquibase:liquibase-cdi:$liquiBaseVersion"
runtimeOnly "org.liquibase:liquibase-core"
}
the problem is that the version of liquibase-core is automatically picked up from Spring Boot BOM declared in root build.gradle, but liquibase-cdi is not specified in that BOM, so I have to declare its version explicitly (otherwise gradle build fails).
I'd like to use somehow the version of liquibase-core specified in Spring Boot BOM for liquibase-cdi like this can be done with Maven placeholder ${liquibase.version} referencing the property specified in <properties/> of parent pom.
Is there a way to do the same with Gradle?
You can use
dependencyManagement.importedProperties['spring.version']
to get for example the spring.version property which is managed via the Dependency Management Plugin.
See https://docs.spring.io/dependency-management-plugin/docs/current/reference/html/#accessing-properties for details.
You can also use the programmatic access to the dependency management to find the managed version of Liquibase:
def liquibaseVersion = managedVersions['org.liquibase:liquibase-core']
See https://docs.spring.io/dependency-management-plugin/docs/current/reference/html/#working-with-managed-versions-programmatic-access
The essence of the problem.
I have several services.
Starter - to start and stop the rest
Service_for_calc - for calculating some operations
Service_sample - service for example
Common_Service-a service for storing models and utils
According to my idea, I will run a starter that will run the rest of the services. Each service will have the same endpoints and models. For example, take the WhoIs functionality.
Each service must tell you who it is.
I don't want to create a model and #Service in every service (module).
I want to create this in Common_service and just import the ready-made logic.
I tried to do this via gradle
to do this in the root settings. gradle I wrote
include 'Service_for_calc'
include 'Common_Service'
include 'Service_sample'
include 'Starter '
and in the service (module) I prescribed it
implementation project(':hub.common')
But I ran into some problem, I can't even describe it clearly, because each time it looked different, but here are the errors that I got when trying to work this way
The module does not see classes from common or does not see the package (despite the fact that the IDE began to suggest them to me)
Some kind of trouble started with the dependency (specifically, Spring dependencies will start working every other time)
Sometimes gradle did not see and threw an error on the implementation project (': hub. common'), with the error that there is no such project ( the name was correct)
After I removed the dependencies, reloaded Gradle and installed it, it worked, but when I tried to open this project on someone else's computer, point 1 was repeated
Maybe I'm doing something wrong, or maybe I shouldn't do it at all.
Is this approach practiced? Was it worth doing it via gradle or should I try it via classpath?
Is it worth doing whole services in a common project?
I will be glad to have a detailed answer!
You have module name conflict, if you have a module named include 'Common_Service' then you should implementation project(':Common_Service')
PS: Here git repo with multi-module, maybe this helps you.
I solve it by root build.gradle
buildscript {
ext {
springBootVersion = '2.4.4'
dependencyManagementVersion = '1.0.11.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
classpath "io.spring.gradle:dependency-management-plugin:${dependencyManagementVersion}"
}
}
allprojects {
group = 'omegabi.back.hub'
version = '0.0.1-SNAPSHOT'
}
subprojects {
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
sourceCompatibility = 11
repositories {
mavenCentral()
}
dependencies {
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
implementation 'org.springframework.boot:spring-boot-starter-test'
}
}
project(':hub.sample_service') {
dependencies {
compile project(':hub.common_service')
}
}
I solved this problem by deleting settings.gradle in every project except the root one!
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
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
I am using Spring 3.2 with Java based configuration and have some problems with my unit tests (JUnit 4.8.1). So this is a test runner:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(classes={TestConfig.class})
public class ManualTest
{
#Autowired
...
Howeever, I am receiving this error:
Caused by: java.lang.IllegalStateException: CGLIB is required to process #Configuration classes. Either add CGLIB to the classpath or remove the following #Configuration bean definitions: [testConfig]
at org.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses(ConfigurationClassPostProcessor.java:327)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanFactory(ConfigurationClassPostProcessor.java:222)
As the Spring blog states, Spring 3.2 is inlining CGLIB 3. So why do I receive this error?
I am using Gradle 1.3 as build management tool and STS as IDE. When calling gradle eclipse gradle pulls in the dependencies twice: one time as plain jar and one time as library:
First as plain jar:
and than as library:
In the plain jar section I had still Spring 3.1 configured, while in the library section there was Spring 3.2. So I removed the plain jars and everything was working.
This is my project build.gradle
configurations
{
driver
}
dependencies
{
driver 'com.oracle:ojdbc6:11.2.0'
compile "org.springframework:spring-jdbc:$springVersion"
testCompile 'com.oracle:ojdbc6:11.2.0'
testCompile "org.springframework:spring-test:$springVersion"
testCompile "commons-dbcp:commons-dbcp:$dbcpVersion"
testCompile "junit:junit:$junitVersion"
testCompile "org.slf4j:slf4j-log4j12:$slf4jVersion"
}
sourceSets
{
main
{
java
{
srcDirs 'src/main/java', "$buildDir/generated-sources/"
}
}
}
And the build.gradle from the master project
configure(allprojects)
{
ext.dbcpVersion = '1.4'
ext.springVersion = '3.2.0.RELEASE'
ext.junitVersion = '4.8.1'
ext.slf4jVersion = '1.7.2'
}
subprojects
{
// Artifact settings
group = 'xxx'
version = '1.0-SNAPSHOT'
// Standard plugins
apply plugin: 'java'
apply plugin: 'eclipse'
// Repositories
repositories
{
mavenLocal()
maven
{
url "http://repo.springsource.org/release"
}
mavenCentral()
}
// Standard dependencies
dependencies
{
}
}
I deleted all Eclipse projects and settings and all Gradle temporary files. Then I tried to import the project in Eclipse (Import Gradle project..). This failed with an exception. Then I deleted the Gradle settings within the Eclipse project and after that the import worked.
So I will not use gradle eclipse with version 1.3.
Also the additional source set path did not make its way into the Eclipse project as source path.
I had the same issue. Just add this dependency to your pom.xml file:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
And your unit tests and runtime code should work properly without cglib errors.