I have a multi-module gradle project using proto to generate the DTOs. The root project has two different modules, inventory, and inventory-api. The *-api module is the one that has all the .proto definitions. inventory has a dependency on inventory-api
Here is the root gradle file
plugins {
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
}
subprojects {
group = 'com.yasinbee.apifirst'
version = '0.0.1-SNAPSHOT'
apply plugin: 'idea'
apply plugin: 'java'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'java-library'
repositories {
jcenter()
}
dependencyManagement {
imports {
mavenBom("org.springframework.boot:spring-boot-dependencies:2.1.9.RELEASE")
}
}
compileJava {
sourceCompatibility = 11
targetCompatibility = 11
}
}
Here is the inventory gradle file
plugins {
id 'org.springframework.boot' version '2.3.4.RELEASE'
}
dependencies {
implementation project (':inventory-api')
implementation "org.springframework.boot:spring-boot-starter-data-jpa"
implementation "org.springframework.boot:spring-boot-starter-jdbc"
implementation "org.springframework.boot:spring-boot-starter-web"
implementation "org.mapstruct:mapstruct:1.4.0.Final"
annotationProcessor 'org.mapstruct:mapstruct-processor:1.4.0.Final'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'com.h2database:h2'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
And the inventory-api gradle file that has all the proto stuff
apply plugin: 'java'
apply plugin: 'java-library'
apply plugin: 'idea'
apply plugin: 'com.google.protobuf'
dependencies {
implementation 'com.google.protobuf:protobuf-java:3.11.0'
}
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.13'
}
}
protobuf {
protoc {
artifact = 'com.google.protobuf:protoc:3.10.1'
}
}
I am able to use the generated proto files within the inventory-api module. However, when I am trying to access the generated files from the inventory module, I am getting a compilation error.
It seems that the generated proto files are not added to the source sets of the inventory app. How can I add the generated source files of *-api modules into other modules that depends on them?
The issue was resolved by changing the build.gradle file for inventory-api module from
dependencies {
implementation 'com.google.protobuf:protobuf-java:3.11.0'
}
to
dependencies {
api 'com.google.protobuf:protobuf-java:3.11.0'
}
Related
I have this gradle config:
buildscript {
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 'mygroup'
version '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
...
}
bootJar {
mainClassName = 'mypakeg.Application'
archiveName = 'my-server.jar'
}
sourceSets.configureEach { sourceSet ->
tasks.named(sourceSet.compileJavaTaskName).configure {
options.annotationProcessorGeneratedSourcesDirectory = file("$projectDir/generated/sources/java")
}
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-data-jpa")
implementation ('org.hibernate:hibernate-jpamodelgen')
annotationProcessor ('org.hibernate:hibernate-jpamodelgen')
implementation('org.projectlombok:lombok')
annotationProcessor 'org.projectlombok:lombok'
}
After build I have folder generated/sources/java with generated files. But Intellij IDE not see this classes. I try click rigth button and marck this folder as root generated classes. But It not helpet becaus:
I have separated module my_server and my_server_main and all classes generated in my_server but my code in my_server_main and if I add dependency to my_server_main I have circular dependency.
How can I generate classes and set path to them?
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')
}
So I'm just playing (experimenting) with gradle and spring-boot.
When I follow the hello world I easily get the project running which kind of makes me happy.
Now I'd like to have a structured project; For this I'm using Intellij Community (not sure if relevant). I have the following structure.
/Project
- build.gradle
- settings.gradle (only includes Services)
/Project/Services/
- build.gradle
- settings.gradle (only includes MyService)
/Project/Services/MyServices
- build.gradle
Now I can share some of my build.gradle files but I"m trying random things I find on the internet. My problem is that the spring boot classes are not available at MyService. The following directory structure inside the Myservice is standard Java (/src/main/java )
I'm trying to put the dependencies & versions in my main build.gradle if possible. Can someone point out what I'm doing wrong.
Currently I've only used gradle for simple android development work.
/Project/build.gradle
group 'nl.msegers.project'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'idea'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
}
/Project/Services/build.gradle
group 'nl.msegers.project.services'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.2.RELEASE")
}
}
/Project/Services/MyService/build.gradle
group 'nl.msegers.project.services.myservice'
version parent.version
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
jar {
baseName = 'Navigation'
version = parent.version
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
To have every project to inherit the dependencies from the root, you could use the allprojects script block on your root project like:
allprojects {
dependencies {
compile group: 'commons-collections', name: 'commons-collections', version: '3.+'
...
}
}
I've been scouring the web for an hour or so to get a structure like this working. In the meanwhile I've rebuild my project from scratch as well (Though I did not have much code anyway).
My code currently allows simple Spring boot sub projects without too much redundant configuration, and they can pretty easily compile the Libraries project, however this can use some work.
What I've got now is a structure like this.
/Project
/Project/Libraries
/Project/Libraries/Models
/Project/Services/
/Project/Services/Service
With the only notable build.gradle files (others are nearly empty):
/Project/Services/
group 'nl.msegers.project.services'
version parent.version
buildscript {
ext {
springBootVersion = "1.5.2.RELEASE"
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
subprojects {
buildscript {
ext {
springBootVersion = "1.5.2.RELEASE"
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
dependencies {
compile("org.springframework.boot:spring-boot-starter-web") {
exclude module: "spring-boot-starter-tomcat"
}
compile("org.springframework.boot:spring-boot-starter-jetty")
compile("org.springframework.boot:spring-boot-starter-actuator")
compile project(':Libraries:Models')
testCompile 'junit:junit:4.12'
}
task wrapper(type: Wrapper) {
gradleVersion = '3.1'
}
}
/Project/Services/MyService
group 'nl.msegers.project.services.myservice'
version parent.version
jar {
baseName = 'myservice'
version = parent.version
}
repositories {
mavenCentral()
}
When I build a project in console I have no service of type styled text output factory available in project scope service. I have also a file pom.xml .I don't know what I do it wrong
This is my build.gradle :
import java.sql.Wrapper
buildscript {
ext {
springBootVersion = '1.2.5.RELEASE'
}
repositories {
maven { url "http://repo.spring.io/libs-milestone" }
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("io.spring.gradle:dependency-management-plugin:0.5.1.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse-wtp'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'war'
war {
baseName = 'springboot'
version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.7
targetCompatibility = 1.7
repositories {
mavenCentral()
maven { url "http://repo.spring.io/libs-milestone" }
}
configurations {
providedRuntime
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("javax.servlet:jstl:1.2")
runtime("mysql:mysql-connector-java")
compile("org.springframework.boot:spring-boot-starter-jdbc")
// https://mvnrepository.com/artifact/javax.el/el-api
compile group: 'javax.el', name: 'el-api', version: '2.2.1-b04'
compile ("org.springframework.boot:spring-boot-starter-web")
providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
providedRuntime 'org.apache.tomcat.embed:tomcat-embed-jasper'
}
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.7'
}
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
I believe the problem is a function of the versions of Gradle and Spring's dependency-management-plugin that are in use.
See the original report from the Spring guys in this Bug in Gradle 2.14-rc1 - No service of type StyledTextOutputFactory report. Gradle moved the StyledTextOutputFactory to an internal package at some point (for the 3.0 release), which broke dependency-management-plugin 0.5.x.
This dependency-management-plugin issue details their making changes to address this in their 0.6.0 release.
I see your build script references Gradle 2.3...but I'm wondering if that is accurate.
I think it boils down to either use Gradle 2.x with dependency-management-plugin 0.5.x or use Gradle 3.x with dependency-management-plugin 0.6.x.
Good luck.
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.