Override Spring Starter Version version in gradle - java

I would like the maven equivalent of properties in gradle:
<properties>
<spring-batch.version>4.0.0.M2</spring-batch.version>
</properties>
When I added ext['spring-batch.version'] = '4.0.0.M2' in build.gradle, imports are not working.
buildscript {
ext {
springBootVersion = '1.5.4.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
ext['spring-batch.version'] = '4.0.0.M2'
dependencies {
compile('org.springframework.boot:spring-boot-starter-batch')
compile("org.hsqldb:hsqldb")
}
I also tried to add spring-batch.version=4.0.0.M2 in gradle.properties, but not working also.

It's failing because 4.0.0.M2 isn't in Maven central.
To fix it, add the Spring milestone Maven repository:
repositories {
mavenCentral()
maven {
url "http://repo.spring.io/libs-milestone"
}
}

first I'd use the new plugin mechanis like so:
buildscript {
repositories { mavenCentral() }
}
plugins {
id 'java'
id 'application' // for docker needed the main class in jar manifest
id 'eclipse'
id 'idea'
id 'org.springframework.boot' version '1.5.4.RELEASE' // automagically includes io.spring.dependency-management
}
this should automagically give you the correct version of all org.springframework.boot dependencies, without having to specify them explicitly (so no need to give a version number for spring-batch.
if you'd like to define further project.ext properties, do so like:
ext {
group = 'minimal_cloud_stream_producer'
groupId = 'de.demo'
baseName = 'minimal_cloud_stream_producer'
port = 8080
}
maybe you have to add a dependencyManagement section too, like so
dependencyManagement {
imports {
mavenBom 'org.springframework.boot:spring-boot-starter-parent:1.5.4.RELEASE'
mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Dalston.SR1'
}
}

Related

Why do I get Gradle configure build error junit5 in IntelliJ?

I created clean project with spring boot initializer and I get configure build error. The message I get is: Could not find org.junit:junit-bom:5.4.0-SNAPSHOT. but in my gradle file I don't have it. What's wrong with it?
buildscript {
ext {
springBootVersion = '2.2.0.BUILD-SNAPSHOT'
}
repositories {
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 11
repositories {
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
dependencies {
implementation('org.springframework.boot:spring-boot-starter-security')
implementation('org.springframework.boot:spring-boot-starter-web')
testImplementation('org.springframework.boot:spring-boot-starter-test')
testImplementation('org.springframework.security:spring-security-test')
}
You're using a development (snapshot) version of Spring Boot that refers to a development (snapshot) version of JUnit 5, but you have not added JUnit's snapshot repository: https://oss.sonatype.org/content/repositories/snapshots/org/junit/junit-bom/5.4.0-SNAPSHOT/:
repositories {
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
}
But you'd better use generally available versions.
The Junit dependency comes with "spring-boot-starter-test", it may not be available in your spring version. Try to change the spring version or use a latest version and give it a go.

Gradle: created jar by gradle task bootJar is not working

I am creating a spring boot application with the following properties the project is running successfully when I run projects with Gradle bootrun it is running successfully but when i create a jar and deploy its not working here is my build.gradle
I have checked all the versions of my project are same.
Error :Error mapping to ad-hoc class .. At present, only #Result types that are discovered by the domain entity package scanning can be mapped.;
nested exception is org.neo4j.ogm.exception.core.MappingException: Error mapping to ad-hoc class com.vipul.... At present, only #Result types that are discovered by the domain entity package scanning can be mapped
PS: I KNOW ABOUT QUERY RESULT mapping but its working with bootRun but not with bootJar
buildscript {
ext {
springBootVersion = '2.0.5.RELEASE'
swaggerVersion = '2.5.0'
jacksonVersion = '2.9.2'
projectVersion = "0.0.1"
version = "0.0.1"
}
repositories {
mavenCentral()
jcenter()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: "io.spring.dependency-management"
apply plugin: 'idea'
apply plugin: 'application'
version = "$projectVersion-SNAPSHOT"
sourceCompatibility = 1.8
mainClassName = 'com.vipul.Application'
applicationDefaultJvmArgs = ["-Xdiag"]
repositories {
mavenCentral()
jcenter()
maven { url "https://plugins.gradle.org/m2/" }
maven{ url "https://repo.spring.io/plugins-release"}
}
task wrapper(type: Wrapper) { gradleVersion = '4.4' }
ext {
springCloudVersion = 'Finchley.RELEASE'
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-aop')
compile('org.springframework.boot:spring-boot-starter-cache')
compile('org.springframework.boot:spring-boot-starter-data-neo4j')
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
Try changing springBoot and springCloud versions in your build.gradle
**
springBootVersion='2.0.0.RELEASE'
**
It seems to be problem with spring boot version. springBootVersion = '2.0.5.RELEASE'

Spring Boot: can't load entities, services, etc. from jar

This issue is widely commented in Stack Overflow, but none of the solutions seems to fit my project.
I have two independant projects (MainApplication and LibraryApplication), then I want to export LibraryApplication , and import it into main project (this is a reduced approach for keeping things simple).
I exported the library.jar to libs/ folder in the main project, then I added the compile files instruction to build.gradle.
The jar is indeed added to the classpath, and I can see the classes from MainApplication. So I added #ComponentScan, #EnableJpaRepositories, #EntityScan, etc. but none of those annotations seems to work, because only MAIN_ENTITY is created when I run the MainApplication project.
LibraryApplication project structure
MainApplication project structure
Only MAIN_ENTITY is created
I tried many of the solutions commented in other related questions, but none works for me:
I moved MainApplication.java to an upper package level without results.
I created an AppConfig.java in LibraryApplication project, and configured #ComponentScan("com.app"),#EntityScan("com.app"), #EnableJpaRepositories("com.app") and injected the class in the MainApplication without results.
I removed the .* from my annotations, so instead of #ComponentScan("com.app.*") I have #ComponentScan("com.app") without results.
How can I achieve the expected results?
I created two github repos so you can check/download the code.
https://github.com/ferdonof/main.git
https://github.com/ferdonof/library.git
Thanks in advance!
UPDATE
Sharing the build.gradle config of each project:
For library
buildscript {
ext {
springBootVersion = '2.0.4.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'com.app'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
bootRepackage {
classifier = 'exec'
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-web')
runtime('com.h2database:h2')
compileOnly('org.projectlombok:lombok')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
For main
buildscript {
ext {
springBootVersion = '2.0.4.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'com.app'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile files('libs/library.jar')
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-web')
runtime('com.h2database:h2')
compileOnly('org.projectlombok:lombok')
testCompile('org.springframework.boot:spring-boot-starter-test')
}

Gradle, multiproject signing with gpg. Cannot configure the 'publishing' extension after it has been accessed

I have multiproject gradle configuration. I'd like to sign my artifacts before publishing. I do the following:
allprojects {
apply plugin: 'java'
sourceCompatibility = 1.10
}
subprojects {
group 'com.example'
version '1.0.0'
apply plugin: 'maven-publish'
apply plugin: 'signing'
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
...
}
test {
useJUnitPlatform()
}
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
}
}
repositories {
mavenLocal()
}
}
signing {
sign publishing.publications.mavenJava
}
}
I get the following error:
Cannot configure the 'publishing' extension after it has been
accessed.
I have no idea what does it mean. I use gradle 4.8.
I have 3 subprojects there.
I ran into the same issue with Gradle 4.9
Apply the signing plugin before the maven-publish.
Hope it helps.
apply plugin: 'signing'
apply plugin: 'maven-publish'

How to exclude transitive dependencies with gradle and substitute it with another

I'm in situation similar with this
(bug between JodaTime and versions of Java greater then 1.8u60).
So what I need is:
Upgrading to JodaTime version 2.8.1 or later.
The problem is: JodaTime is a transitive dependency in my project.
Build automation tool used in it is gradle. Need help to handle it.
buildscript:
buildscript {
ext {
springBootVersion = '1.2.4.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("io.spring.gradle:dependency-management-plugin:0.5.1.RELEASE")
classpath("org.flywaydb:flyway-gradle-plugin:3.2.1")
}
}
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'eclipse'
apply plugin: 'spring-boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'org.flywaydb.flyway'
jar {
baseName = 'xxxx'
version = 'alpha'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-data-jpa:1.2.4.RELEASE")
compile("org.springframework.boot:spring-boot-starter-aop:1.2.4.RELEASE")
compile("org.springframework.boot:spring-boot-starter-web:1.2.4.RELEASE")
compile("org.springframework.boot:spring-boot-starter-freemarker:1.2.4.RELEASE")
compile("com.amazonaws:aws-java-sdk:1.10.2")
compile("com.stripe:stripe-java:1.33.0")
compile("org.flywaydb:flyway-core:3.2.1")
compile("com.jolbox:bonecp:0.8.0.RELEASE")
runtime("org.postgresql:postgresql:9.4-1201-jdbc41")
testCompile("org.springframework.boot:spring-boot-starter-test:1.2.4.RELEASE")
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-starter-parent:1.0.2.RELEASE"
}
}
eclipse {
classpath {
containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'
}
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
You need to change the following piece of code in dependencies block:
compile("com.amazonaws:aws-java-sdk:1.10.2") {
exclude group: 'joda-time', module: 'joda-time'
}
compile("joda-time:joda-time:2.8.1")
put transitive false, and set between your dependencies the packages that you need rather that the ones in the transitive download.

Categories