Gradle subproject not recognizing 3rd party dependencies from root build.gradle - java

I have a project like this:
module1
src/
build.gradle
module2
src/
build.gradle
build.gradle
settings.gradle
In the root build.gralde I am defining 3rd party dependencies needed by all submodules. However, in intellij the submodules don't seem to recognize the dependencies and won't compile. I've seen this work in the past and can't figure out what I'm doing wrong
Root build.gradle
plugins {
id 'java'
}
group 'com.XXX'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
allprojects {
repositories {
mavenLocal()
mavenCentral()
jcenter()
}
}
dependencies {
...
}
settings.gradle
rootProject.name = 'XXX'
include 'module1'
include 'module2'
module1 build.gradle
plugins {
id 'java'
}
group 'com.XXX'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8

I realized I just needed to put the dependencies in the root build.gradle within a "subprojects" block and add the java plugin as well.
subprojects {
apply plugin: 'java'
dependencies {
...
}
}

Related

Gradle multimodule project - how to create library with individual module as dependency jar

Below is my multimodule project setup.
Root project 'gradle-lib'
+--- Project ':environment-config1'
+--- Project ':environment-config2'
+--- Project ':environment-api'
\--- Project ':environment-lib'
gradle-lib/build.gradle
plugins {
id "org.springframework.boot" version "2.6.7" apply false
id "io.spring.dependency-management" version "1.0.11.RELEASE" apply false
}
subprojects { final Project subproject ->
apply plugin: 'org.springframework.boot'
apply plugin: 'java'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'jacoco'
apply plugin: 'java-library'
apply plugin: 'maven-publish'
version = rootProject.version
group = rootProject.group
repositories {
mavenLocal()
mavenCentral()
}
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
// No spring boot application
bootJar {
enabled = false
}
jar {
enabled = true
// Remove `plain` postfix from jar file name
archiveClassifier.set("")
}
sourceCompatibility = '1.8'
// Add dependencies that common to all projects
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
environment-config1/build.gradle
dependencies {
implementation project(":environment-api")
}
environment-config2/build.gradle
dependencies {
implementation project(":environment-api")
}
environment-api/build.gradle
// Boiler plate code with some jar dependencies
dependencies {
}
environment-lib/build.gradle
dependencies {
implementation project(":environment-api")
implementation project(":environment-config1")
implementation project(":environment-config2")
}
Whenever I try to build this project with gradlew clean build, these is compileJava task failed at 'environment-lib' which actually uses certain class files from environment-config1 & environment-api projects.
I could suppress the same as below, but what configuration: 'default' will do?
implementation project(":environment-api", configuration: 'default')
implementation project(":environment-config1", configuration: 'default')
implementation project(":environment-config2", configuration: 'default')
How can I create a single jar with other module as individual jar like below. Is it possible or recommended way?
gradle-lib-1.0.0.jar ---> + META-INF
\- MANIFEST.MF
+ //source files of environment-lib project
+ lib
\- environment-api-1.0.0.jar
\- environment-config1-1.0.0.jar
\- environment-config2-1.0.0.jar

org.springframework.boot Configuration with name 'runtime' not found

When trying to run my program using gradle bootRun, the error shows that
Failed to apply plugin 'org.springframework.boot'
Configuration with name 'runtime' not found
The following is my build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.3.RELEASE")
}
}
apply plugin: 'war'
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'application'
jar {
baseName = 'blockchain-demo'
version = '0.0.1'
}
war {
baseName = 'blockchain-demo'
version = '0.0.1'
}
application {
mainClass = 'web.Application'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
compile("org.springframework.boot:spring-boot-devtools")
testCompile("org.springframework.boot:spring-boot-starter-test")
testCompile("junit:junit")
}
I assume you're using Gradle 7. In Gradle 7, the configurations compile, testCompile, runtime and testRuntime have been removed in favor of implementation, testImplementation, runtimeOnly and testRuntimeOnly. That's why Gradle issues
Failed to apply plugin 'org.springframework.boot' Configuration with name 'runtime' not found
To fix the issue, you should use the Gradle version that is supported by the Spring Boot Gradle Plugin version you're using (1.5.3, according to the snippet provided). The system requirements lists Gradle version 2 and 3 as requirement for Spring Boot 1.5.3. Everything thereafter is not supported.
Spring Boot 2.5.0 supports Gradle 6.8, 6.9, or 7.x
See here for a good reference on Gradle 7 configurations: https://docs.spring.io/spring-boot/docs/2.5.0/gradle-plugin/reference/htmlsingle/
This will get you off the ground:
plugins {
id 'org.springframework.boot' version '2.5.0'
}
apply plugin: 'java'
apply plugin: 'io.spring.dependency-management'
dependencies {
implementation('org.springframework.boot:spring-boot-starter-web')
implementation('org.springframework.boot:spring-boot-starter-data-jpa')
}
repositories {
mavenCentral()
}

Cannot keep dependency version in gradle.properties

I need to keep versions of dependencies in gradle.properties.
gradle.properties:
springBootVersion = '2.1.9.RELEASE'
build.gradle:
plugins {
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'org.springframework.boot' version '2.1.9.RELEASE'
}
subprojects {
repositories {
mavenCentral()
}
configurations.all {
resolutionStrategy {
failOnVersionConflict()
}
}
apply plugin: 'java'
apply plugin: 'io.spring.dependency-management'
dependencyManagement {
dependencies {
dependency "org.springframework.boot:spring-boot-starter-web:${springBootVersion}"
dependency "org.springframework.boot:spring-boot-starter-test:${springBootVersion}"
}
}
sourceCompatibility = 11
targetCompatibility = 11
tasks.withType(JavaCompile){
options.encoding = 'UTF-8'
}
}
settings.gradle:
include 'api'
api/build.gradle:
apply plugin: 'org.springframework.boot'
archivesBaseName = 'phone-gift-processing-api'
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
Output of ./gradlew clean build:
> Task :api:compileJava FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':api:compileJava'.
> Could not resolve all files for configuration ':api:compileClasspath'.
> Could not find org.springframework.boot:spring-boot-starter-web:'2.1.9.RELEASE'.
Required by:
project :api
I.e., Gradle uses springBootVersion, but fails to bind the dependency for some reason.
When I replace ${springBootVersion} by ${springBootVersion}, build is successful.
You have the format wrong in gradle.properties. Instead of this:
springBootVersion = '2.1.9.RELEASE'
Try this:
springBootVersion=2.1.9.RELEASE
(The spaces are optional, but the important part is that you should not have the quotation marks around the value.)

Testing with Gradle

Testing fails.
How fix exception?
Here is stacktrace and code:
Could not determine the dependencies of task ':test'.
Configuration with name 'default' not found.
My parent settings.gradle
rootProject.name = 'opti'
include 'web'
build.gradle
allprojects {
repositories {
mavenCentral()
}
group 'com.opti'
version '1.0'
apply plugin: 'java'
apply plugin: 'groovy'
dependencies {
testCompile 'org.codehaus.groovy:groovy-all:2.3.11',
'org.spockframework:spock-maven:0.7-groovy-2.0'
}
}
Tested module settings.gradle
rootProject.name = 'web'
include 'web'
build.gradle
group 'com.opti'
version '1.0'
apply plugin: 'groovy'
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
compile(project(':web'))
}
#Artur check that first:
https://docs.gradle.org/current/userguide/java_plugin.html
It seems that gradle could not find default configuration for web project.
You could also check if running this command helps
gradle :opti:test
or
gradle :web:test

How to fix this gradle dependency resolution error?

I am new to gradle and I am trying to use it for an existing java project.
I have created settings.gradle file in my root directory:
include ':datamodel'
include ':active'
include ':client'
include ':zero'
include ':toall'
and build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
mavenCentral()
}
dependencies {
}
}
The build.gradle in datamodel looks like this:
apply plugin: 'java'
sourceSets {
main.java.srcDirs = ['src/main/java']
main.resources.srcDirs = ['src/main/java']
test.java.srcDirs = ['tests/java']
test.resources.srcDirs = ['tests/resources']
}
dependencies {
compile 'com.google.protobuf:protobuf-java:2.5.+'
}
When I run gradle build (version: 2.0) I get this failure
FAILURE: Build failed with an exception.
* What went wrong:
Could not resolve all dependencies for configuration ':datamodel:compile'.
> Could not find any version that matches com.google.protobuf:protobuf-java:2.5.+.
Required by:
MyRoot:datamodel:unspecified
How can I fix this?
Add
repositories {
mavenCentral()
}
to your build script. Not as a part of buildscript closure where you can define where to look for dependencies required to execute your build script. You can do it in your top-level build.gradle using
allprojects {
repositories {
mavenCentral()
}
}
Try this
compile 'com.google.protobuf:protobuf-java:2.5.0'
More info here
The maven repo looks like
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>2.5.0</version>
</dependency>
You can also try and add jcenter() to dependencies like this
dependencies {
mavenCentral()
jcenter()
}
which will allow to search not only in repo1.maven.org

Categories

Resources