Paris Karagiannopoulos
Paris Karagiannopoulos

Reputation: 591

How to get dependency version form parent project in Gradle

I have a Gradle project and I want to create a submodule but I am getting an FAILURE when building the project.

The error message is

Execution failed for task ':child-project:compileJava'
> Could not resolve all files for configuration 'child-project:compileClasspath'.
  > Could not find org.springframework.boot:spring-boot-starter:.
    Required by:
        project :parent-project

This is the parent project build.gradle file:

plugins {
  id 'org.springframework.boot' version '2.3.0.RELEASE'
  id 'io.spring.dependency-management' version '1.0.9.RELEASE'
  id 'java'
}

allprojects {
  apply plugin: 'java'
  group = 'com.test'

  sourceCompatibility = JavaVersion.VERSION_11
  targetCompatibility = JavaVersion.VERSION_11

  repositories {
    //local nexus
  }
}

dependencies {
  implementation 'org.springframework.boot:spring-boot-starter'
  implementation 'org.springframework.boot:spring-boot-starter-actuator'
  implementation 'org.springframework.boot:spring-boot-starter-web'
  //other dependencies
}

This is the child project build.gradle:

plugins {
  id 'java'
}

group = 'com.test'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_11

dependencies {
  compile 'org.springframework.boot:spring-boot-starter'

  testImplementation('org.springframework.boot:spring-boot-starter-test') {
    exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
  }
}

Please help, thanks in advance.

Upvotes: 1

Views: 3616

Answers (1)

Bjørn Vester
Bjørn Vester

Reputation: 7600

In order to be able to specify Spring Boot dependencies without versions, you need to apply the Spring Boot plugin to all modules. Right now you only have it in the parent project, but not the child.

Because applying the plugin will also, by default, disable the normal jar task can create a bootJar instead, you need to change this for libraries:

// Child build file
plugins {
    // Note that there are no versions on the plugins in the child project as this is defined by the ones in the parent
    id 'org.springframework.boot'
    id 'io.spring.dependency-management'
}

bootJar {
  enabled = false
}

jar {
  enabled = true
}

dependencies {
  implementation 'org.springframework.boot:spring-boot-starter'
}

Alternatively, you could also scrap the io.spring.dependency-management plugin (and the org.springframework.boot plugin in the child project), and instead import the Spring Boot BOM as a platform:

// Child build file (alternative)
dependencies {
  // Use 'platform' for making the versions in the BOM a recommendation only, and 'enforcedPlatform' for making them a requirement.
  // Note that you need the version of the BOM, so I recommend putting it in a property.
  implementation enforcedPlatform("org.springframework.boot:spring-boot-dependencies:2.3.0.RELEASE")

  // Here you can leave out the version
  implementation 'org.springframework.boot:spring-boot-starter'
}

I usually go for the latter alternative as this allows me to use normal Gradle semantics. But it is mostly just preference.

(And just a small note to your build script: the compile configuration is deprecated. It is used in the line compile 'org.springframework.boot:spring-boot-starter'. You probably just copy/pasted it from somewhere, but you should replace it with implementation.)

Upvotes: 4

Related Questions