Gradle multiple subproject with different spring boot plugin - java

I have multi project build structure and spring boot is applied from the parent to all subprojects. Here's the parent build.gradle
buildscript {
repositories {
maven {
url 'http://someurl.com/repository/MavenRepositoryGroup/'
}
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:1.5.8.RELEASE"
}
}
subprojects {
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'eclipse'
apply plugin: 'war'
apply plugin: 'org.springframework.boot'
..
..
..
}
}
Among multiple subprojects, I have a requirement to upgrade just one subproject to spring boot 2 version. How to do it?

It's pretty easy, just declare plugins in subprojects directly, where they are used. BTW, consider using new plugins DSL instead of apply. Take a look at a demo project I created for you. Here are the most interesting parts:
settings.gradle:
rootProject.name = "so53381565"
enableFeaturePreview("IMPROVED_POM_SUPPORT")
include(":a")
include(":b")
include(":c")
build.gradle:
subprojects {
apply plugin: "java"
repositories {
jcenter()
}
}
wrapper {
gradleVersion = "4.10.2"
distributionType = Wrapper.DistributionType.ALL
}
a/build.gradle:
plugins {
id "org.springframework.boot" version "1.5.8.RELEASE"
}
dependencies {
implementation "org.springframework.boot:spring-boot-starter"
}
b/build.gradle:
plugins {
id "org.springframework.boot" version "1.5.17.RELEASE"
}
dependencies {
implementation "org.springframework.boot:spring-boot-starter"
}
c/build.gradle:
plugins {
id "org.springframework.boot" version "2.0.6.RELEASE"
}
dependencies {
implementation "org.springframework.boot:spring-boot-dependencies:2.0.6.RELEASE"
implementation "org.springframework.boot:spring-boot-starter"
}
App.java:
#SpringBootApplication
public class App implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
#Override
public void run(String... args) throws Exception {
System.out.println(SpringVersion.getVersion());
}
}
App.java is the same in all three subprojects. Executing ./gradlew clean :a:bootRun :b:bootRun :c:bootRun will output something like:
…
4.3.12.RELEASE
…
4.3.20.RELEASE
…
5.0.10.RELEASE
…
As you see, you're using three different Springs in three different subprojects.
EDIT
First of all, you can still use apply, just put them in subprojects.
Second, you can use plugins with your own repositories and dependencies, even with those not published in Gradle's Plugin Repository by adding a pluginManagement block in settings.gradle (this is Kotlin DSL from my project):
pluginManagement {
repositories {
gradlePluginPortal()
add(jcenter())
add(maven("http://my.jfrog.io"))
}
resolutionStrategy {
eachPlugin {
if (requested.id.id == "by.dev.madhead.some-plugin") {
useModule("by.dev.madhead:some-gradle-plugin:${requested.version}")
}
}
}
}
Now I am able to use by.dev.madhead.some-plugin in plugins DSL across the project:
plugins {
id("by.dev.madhead.some-plugin").version("42")
}
And it will be substituted by by.dev.madhead:some-gradle-plugin:42

Related

is it possible to force java module dependencies to use specify version [duplicate]

I have a distributes projects with different sub-projects and I want to accomplish the following:
(root)
client
module A
module B
module C
model
I want to put
protoc {
artifact = 'com.google.protobuf:protoc:3.5.0'
}
plugins {
grpc {
artifact = "io.grpc:protoc-gen-grpc-java:1.7.0"
}
}
generateProtoTasks {
all()*.plugins {
grpc {}
}
} }
dependencies {
compile "com.google.api.grpc:proto-google-common-protos:1.0.0"
compile "io.grpc:grpc-netty:1.7.0"
compile "io.grpc:grpc-protobuf:1.7.0"
compile "io.grpc:grpc-stub:1.7.0"
}
for module A, B and C.
For now I have the following in my root build.gradle
subprojects{
apply plugin: 'java'
sourceCompatibility = 1.8
group 'project'
version '0.0.1-SNAPSHOT'
jar {
manifest {
attributes 'Main-Class': "com.project.${project.name}.App"
}
doFirst {
from { configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) } }
}
}
repositories {
mavenCentral()
mavenLocal()
}
dependencies {
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:1.9.5'
}
}
So every sub-project use java plugin, and has the defines dependencies and jar task.
How can I only put the first block for some sub-projects ?
I tried using a variable like in Multi-project Builds - Gradle but I couldn't access it in subprojects block.
Thank you in advance. I'm really interested in using Gradle correctly and it's a bit hard to get into it outside of simple Android/Java projects. Feel free to include any documentations I should read :)
Edit:
Thank you. I wouldn't have posted here if I hadn't search before. Apparently I was missing the keyword "subset" who would have gave me the solution you linked.
A solution is described here: https://discuss.gradle.org/t/configure-only-subset-of-subprojects/5379/3
You can run configure() with a list of projects.
project.ext {
subprojectList = subprojects.findAll{
it.name == 'subprojectA' ||
it.name == 'subprojectB' ||
it.name == 'subprojectC'
}
}
configure(project.subprojectList) {
// insert your custom configuration code
}
or
configure([project(':a'), project(':b'), project(':c')]) {
// configuration
}

Use Spring Boot as a transitive dependency from common JAR with Gradle

I have a common library that is published as a jar to maven local, which is then used by a set of very thin client applications. These applications basically feed their own configuration to the common library and initializes themselves as Spring applications.
This common library uses spring boot and a bunch of other dependencies. My question is, is it possible for me to not define spring boot as a dependency in the applications, and simply use the common library's spring version as a transitive dependency in all the applications?
I feel like this should be possible, but when I tried doing it, the springframework.org.boot classes are not being resolved (eg: #SpringBootApplication annotation).
These are my build.gradle files:
build.gradle from the common library
plugins {
id 'java-library'
id 'maven-publish'
}
group 'com.my.group'
version '1.0-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenLocal()
}
publishing {
publications {
mavenJava(MavenPublication) {
groupId = 'com.my.group'
artifactId = 'common-lib'
version = '0-alpha.1'
from components.java
}
}
}
configurations {
springBom
compileOnly.extendsFrom(springBom)
annotationProcessor.extendsFrom(springBom)
implementation.extendsFrom(springBom)
}
dependencies {
springBom enforcedPlatform('org.springframework.boot:spring-boot-dependencies:2.3.5.RELEASE')
api 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
implementation 'org.springframework.cloud:spring-cloud-aws-messaging'
annotationProcessor 'org.springframework.boot:spring-boot-autoconfigure-processor'
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
// a few other non-spring dependencies
}
build.gradle from a sample application
plugins {
id 'java'
}
group = 'com.my.group'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenLocal()
}
dependencies {
implementation group: 'com.my.group', name: 'common-lib', version: '0-alpha.1'
}
Example application initialization
package com.my.group.Core;
// none of the below are resolved
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
#SpringBootApplication(scanBasePackages = {"com.my.group"})
public class ConsumerApplication implements ApplicationRunner {
#Autowired
Core core;
public ConsumerApplication() {
}
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
#Override
public void run(ApplicationArguments args) {
try {
core.processEvents();
} catch (ConfigurationException | InitializationException e) {
e.printStackTrace();
}
}
}
If I add in the following dependencies to my applications, everything works as expected.
springBom enforcedPlatform('org.springframework.boot:spring-boot-dependencies:2.3.5.RELEASE')
implementation 'org.springframework.boot:spring-boot-starter'
I found the issue. I have not included api in the configurations and as a result, the application could not resolve the version. Fixed by updating the build.gradle in my common library like so:
configurations {
springBom
compileOnly.extendsFrom(springBom)
annotationProcessor.extendsFrom(springBom)
implementation.extendsFrom(springBom)
api.extendsFrom(springBom)
}

java.lang.ClassNotFoundException: org.postgresql.Driver from jooq code generating task

I am implementing java-spring-boot project ( jdk11, spring boot 2.3.0.RELEASE ) using jooq-hikari-hibernate-postgres for back end.
I have already created the tables using flyway.
I have created a task name "generateJooqRepo" in build.gradle to generate the java code for the repository. The task is running however failing with error message:
Execution failed for task ':generateJooqRepo'.
> java.lang.ClassNotFoundException: org.postgresql.Driver
Any insight on what I am doing wrong?
build.gradle:
import org.jooq.codegen.*
plugins {
id 'org.springframework.boot' version '2.3.0.RELEASE'
id 'org.flywaydb.flyway' version '6.4.4'
id "nu.studer.jooq" version "4.2"
}
apply plugin: 'io.spring.dependency-management'
apply plugin: 'application'
apply plugin: 'maven'
apply plugin: 'jacoco'
apply plugin: 'idea'
apply plugin: 'nu.studer.jooq'
configurations {
deployerJars
}
sourceCompatibility = '11.0.1'
targetCompatibility = '11.0.1'
repositories {
// Use jcenter for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
mavenCentral()
}
dependencies {
//Spring Dependencies
implementation 'org.springframework.boot:spring-boot-starter-actuator:2.3.0.RELEASE'
implementation 'org.springframework.boot:spring-boot-starter-web:2.3.0.RELEASE'
//postges
jooqRuntime 'org.postgresql:postgresql:42.2.14'
implementation 'org.postgresql:postgresql:42.2.14'
//The gradle-jooq-plugin needs dependencies in a separate configuration.
// It uses the jooqRuntime configuration to detect the needed dependencies, it doesn't use the compile or implementation configuration.
jooqRuntime group: 'org.jooq', name: 'jooq-meta-extensions', version: '3.13.2'
//flyway
compile 'org.flywaydb:flyway-core:6.4.4'
//jooq dependency
implementation 'org.springframework.boot:spring-boot-starter-jooq:2.3.0.RELEASE'
compile 'org.jooq:jooq:3.13.2'
compile 'org.jooq:jooq-meta:3.13.2'
compile 'org.jooq:jooq-codegen:3.13.2'
// This dependency is found on compile classpath of this component.
implementation 'org.apache.commons:commons-lang3:3.9'
}
group = 'com.ringo.tapos.service'
version = '1.0.0-SNAPSHOT'
mainClassName = "com.ringo.tapos.service.Application"
// Use your favourite XML builder to construct the code generation configuration file
// ----------------------------------------------------------------------------------
task generateJooqRepo {
doLast {
def writer = new StringWriter()
def xml = new groovy.xml.MarkupBuilder(writer)
.configuration('xmlns': 'http://www.jooq.org/xsd/jooq-codegen-3.13.0.xsd') {
jdbc() {
driver('org.postgresql.Driver')
url('jdbc:postgresql://localhost:5432/postgres')
user('postgres')
password('postgres')
schema('tapos')
}
generator() {
database() {
}
generate([:]) {
pojos true
daos true
relations true
deprecated false
records true
//immutablePojos false
}
target() {
packageName('com.ringo.tapos.service.repositories')
directory('src/main/java')
}
}
}
GenerationTool.generate(writer.toString())
}
}
You should add postgres driver dependency to jooqRuntime configuration as well
jooqRuntime 'org.postgresql:postgresql:42.2.14'
UPDATE:
You should add postgres driver to your buildscript classpath if you want to use it in gradle script outside of the tasks provided by nu.studer.jooq plugin
buildscript {
dependencies {
classpath 'org.postgresql:postgresql:42.2.14'
}
}

Querydsl Annotation Processor issue after upgrade to Gradle 5

I have a gradle script which generates querydsl classes from Mongo annotated entities. It was working so far, but after upgrade to Gradle 5 I have a problem with:
* What went wrong:
Execution failed for task ':myproject-common:compileQuerydsl'.
Annotation processor 'org.springframework.data.mongodb.repository.support.MongoAnnotationProcessor' not found
Please find my gradle.build script below. Any ideas what could be wrong? I read that there was change in Gradle 5 that annotation processors are not used by default during compilation and annotationProcessor declaration should be added but when I add it to dependencies the same error occurs.
plugins {
id 'org.springframework.boot' version '2.0.4.RELEASE'
id "com.ewerk.gradle.plugins.querydsl" version "1.0.10"
}
repositories {
mavenCentral()
}
apply plugin: 'java'
apply plugin: 'io.spring.dependency-management'
jar {
enabled = true
baseName = 'myproject-common'
version = '0.0.1-SNAPSHOT'
}
// do no package commons into fat jar
bootJar {
enabled = false
}
querydsl {
library = 'com.querydsl:querydsl-apt:4.1.4'
querydslSourcesDir = 'src/main/querydsl'
springDataMongo = true
}
sourceCompatibility = 11.0
targetCompatibility = 11.0
sourceSets {
main {
java {
srcDirs = ['src/main/java', 'src/main/querydsl']
}
}
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.data:spring-data-mongodb")
compile("org.springframework.boot:spring-boot-starter-data-rest")
compile("org.springframework.boot:spring-boot-starter-security")
compile("com.fasterxml.jackson.datatype:jackson-datatype- jsr310:2.8.6")
compile("com.google.guava:guava:23.0")
compile("commons-io:commons-io:2.5")
compile("org.aspectj:aspectjweaver:1.8.9")
compile("org.apache.commons:commons-lang3:3.5")
compile("commons-collections:commons-collections:3.2.2")
compile("org.javamoney:moneta:1.1")
compile("com.fizzed:rocker-runtime:1.2.0")
compile("com.querydsl:querydsl-core:4.1.4")
compile("com.querydsl:querydsl-mongodb:4.1.4")
compile("com.querydsl:querydsl-apt:4.1.4")
compile("com.codepoetics:protonpack:1.15")
testCompile("org.springframework.boot:spring-boot-starter-test")
testCompile("org.assertj:assertj-core:3.7.0")
}
This is my working configuration for JPA without using additional plugins. Gradle 5.3, openjdk 11.0.2.
plugins {
id 'java-library'
}
ext {
springBootVersion = '2.2.0.M1'
queryDslVersion = '4.2.1'
}
dependencies {
api(
"com.querydsl:querydsl-jpa:$queryDslVersion"
)
implementation(
platform("org.springframework.boot:spring-boot-dependencies:$springBootVersion"),
'org.springframework.boot:spring-boot-starter-validation',
'org.springframework.boot:spring-boot-starter-data-jpa',
'org.liquibase:liquibase-core',
'org.postgresql:postgresql'
)
annotationProcessor(
platform("org.springframework.boot:spring-boot-dependencies:$springBootVersion"),
'jakarta.persistence:jakarta.persistence-api',
'jakarta.annotation:jakarta.annotation-api',
"com.querydsl:querydsl-apt:$queryDslVersion:jpa"
)
}
Please pay attention to the annotation processor. It has suffix ":jpa". Probably this is what you missed. To activate the same one for mongodb you should add ":morphia" suffix.
Please also look at these 2 dependencies:
'jakarta.persistence:jakarta.persistence-api'
'jakarta.annotation:jakarta.annotation-api'
This is a workaround for the issue described here:
https://discuss.gradle.org/t/annotationprocessor-querydsl-java-lang-noclassdeffounderror/27107
They should be transitive dependencies of the annotation processor, but they aren't yet. Probably you will have to include some mongo dependencies to annotationProcessor too.
Generated sources are located in \build\generated\sources\annotationProcessor\java\main
I have finally found a workaround. Querydsl's lack of compatibility with Gradle 5 is reported here as a bug: https://github.com/ewerk/gradle-plugins/issues/108
Workaround is to add to gradle script:
compileQuerydsl {
options.annotationProcessorPath = configurations.querydsl
}
I could solve the problem by adding the following two dependencies.
annotationProcessor "com.querydsl:querydsl-apt:4.2.1:jpa"
annotationProcessor 'javax.annotation:javax.annotation-api:1.3.1'
The second dependency was a hidden reason why it not worked for me.
I faced the same issue for Spring Boot Data JPA and Query DSL with Gradle 6.6.1. I tried so many things in my build.gradle file, including some of the suggestions in other responses to this question. I was able to come up with a build.gradle file with a minimal set of additions to the standard build file (standard in the sense of the one generated by https://start.spring.io). Here it is:
plugins {
id 'org.springframework.boot' version '2.4.0'
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
id 'java'
id 'com.ewerk.gradle.plugins.querydsl' version "1.0.10"
}
group = 'org.code'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenCentral()
}
querydsl {
querydslDefault = true
jpa = true
}
configurations {
querydsl.extendsFrom implementation
}
compileQuerydsl {
options.annotationProcessorPath = configurations.querydsl
}
ext {
queryDslVersion = '4.4.0'
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation "com.querydsl:querydsl-jpa:${queryDslVersion}"
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
useJUnitPlatform()
}
The key addition is the Query DSL plugin:
id 'com.ewerk.gradle.plugins.querydsl' version "1.0.10"
However, it is not enough on its own. Other important additions are:
querydsl {
querydslDefault = true
jpa = true
}
configurations {
querydsl.extendsFrom implementation
}
compileQuerydsl {
options.annotationProcessorPath = configurations.querydsl
}
The configuration i am using,it works:
plugins {
id 'org.springframework.boot' version '2.2.5.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
id "com.ewerk.gradle.plugins.querydsl" version "1.0.10"
}
group = 'io.loremipsum'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenCentral()
}
querydsl {
library = 'com.querydsl:querydsl-apt:4.1.4'
querydslSourcesDir = 'src/main/querydsl'
springDataMongo = true
}
sourceSets {
main {
java {
srcDirs = ['src/main/java', 'src/main/querydsl']
}
}
}
// is required when gradle > 5.0
compileQuerydsl {
options.annotationProcessorPath = configurations.querydsl
}
dependencies {
compile 'org.springframework.boot:spring-boot-starter-data-mongodb'
compile 'org.springframework.boot:spring-boot-starter-web'
compile("com.querydsl:querydsl-core:4.1.4")
compile("com.querydsl:querydsl-mongodb:4.1.4")
compile("com.querydsl:querydsl-apt:4.1.4")
compile 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile 'de.flapdoodle.embed:de.flapdoodle.embed.mongo'
}
pay attention to the dependency declaration:
compile 'org.springframework.boot:spring-boot-starter-data-mongodb'
implementation dependency should not be used or will result in an exception:
Annotation processor 'org.springframework.data.mongodb.repository.support.MongoAnnotationProcessor' not found
I see you have compile("com.querydsl:querydsl-apt:4.1.4") in your dependencies. According to the docs
Since implementation details matter for annotation processors, they must be declared separately on the annotation processor path. Gradle ignores annotation processors on the compile classpath.
So, put com.querydsl:querydsl-apt:4.1.4 in the annotationProcessor scope.
BTW, consider switching to api / implementation scopes over compile.

Define Constants in Gradle in Java Project

I am building a Spring Boot project and i want to define some constants in gradle. I used to be an Android developer, and in Android you can define it with buildConfigField. How can I do it in Java project?
Is there a tool that is equivalent to the following?
android {
buildTypes {
debug {
buildConfigField "String", "BASE_URL", "http://debug.server.com"
}
release {
buildConfigField "String", "BASE_URL", "http://release.server.com"
}
}
}
If you are using Gradle as your building tool, you can define Environment Variables inside you build.gradle file, using the following syntax:
buildscript {
repositories {
if (System.getenv("RESOLVE_LOCAL")) {
maven { url "$indexUrl/resolver-grails3" }
} else {
maven { url "$indexLocalUrl/resolver-grails3" }
}
}
dependencies {
classpath 'build-info-extractor:build-info-extractor-gradle:3.1.1'
classpath group: 'org._10ne.gradle', name: 'rest-gradle-plugin, version: '0.3.1'
}
}
apply plugin: 'org.10ne.rest'

Categories