Could not find method bootJar() for arguments - spring-boot

While building the gradle project I am getting below error-
FAILURE: Build failed with an exception.
Where:
Build file '/Users/vdubey/Documents/microservices/workspace/Promo-Service/build.gradle' line: 30
What went wrong:
A problem occurred evaluating root project 'Promo-Service'.
Could not find method bootJar() for arguments [build_3jq74tz48uic808y18txabjvx$_run_closure1#5c4aa147] on root project 'Promo-Service' of type org.gradle.api.Project.
Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
Get more help at https://help.gradle.org
Any clue why it is failing?

I had this error while following the Spring Boot with Docker guide because my application is using Spring Boot 1.5.10.RELEASE and bootRun was only introduced in 2.0.0.
Luckily, the Spring Boot Docker guide code is in a Github repository, so I was able to navigate back to a pre 2.0.0 version: https://github.com/spring-guides/gs-spring-boot-docker/tree/8933f6efa9a94cf596095658dc0b81986d11a3ec
See the completed build.gradle file for 1.5.10-RELEASE:
// This is used as the docker image prefix (org)
group = 'springio'
jar {
baseName = 'gs-spring-boot-docker'
version = '0.1.0'
}
// tag::task[]
docker {
name "${project.group}/${jar.baseName}"
files jar.archivePath
buildArgs(['JAR_FILE': "${jar.archiveName}"])
}
// end::task[]

Consider checking the presence of gradle plugin for Spring Boot:
https://plugins.gradle.org/plugin/org.springframework.boot
For Gradle 2.1 and later:
plugins {
id "org.springframework.boot" version "2.1.0.RELEASE"
}
For older Gradle versions:
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:2.1.0.RELEASE"
}
}
apply plugin: "org.springframework.boot"

I had this problem when I am following official Spring testing-web guide. They offer initial gradle file as below.
buildscript {
repositories { mavenCentral() }
}
plugins { id "io.spring.dependency-management" version "1.0.4.RELEASE" }
ext { springBootVersion = '2.0.5.RELEASE' }
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
bootJar {
baseName = 'gs-testing-web'
version = '0.1.0'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
testCompile("org.springframework.boot:spring-boot-starter-test")
}
task wrapper(type: Wrapper) {
gradleVersion = '4.6'
}
But when I run the ./gradlew clean command I get the below exception
Could not find method bootJar() for arguments [] on root project '' of type org.gradle.api.Project.
The problem is instead of using plugins { id "io.spring.dependency-management" version "1.0.4.RELEASE" } use id "org.springframework.boot" version "2.1.3.RELEASE". Also don't forget the java and io.spring.dependency-management plugins.
apply plugin: 'java'
apply plugin: 'io.spring.dependency-management'
8.1. Reacting to the Java plugin
When Gradle’s java plugin is applied to a project, the Spring Boot plugin:
Creates a BootJar task named bootJar that will create an executable,
fat jar for the project. The jar will contain everything on the
runtime classpath of the main source set; classes are packaged in
BOOT-INF/classes and jars are packaged in BOOT-INF/lib
8.4. Reacting to the dependency management plugin
When the io.spring.dependency-management plugin is applied to a
project, the Spring Boot plugin will automatically import the
spring-boot-dependencies bom.
I shared valid build.gradle file below which can be a starting point for anyone who have this problem.
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java project to get you started.
* For more details take a look at the Java Quickstart chapter in the Gradle
* user guide available at https://docs.gradle.org/4.6/userguide/tutorial_java_projects.html
*/
plugins {
id "org.springframework.boot" version "2.1.3.RELEASE"
}
apply plugin: 'idea'
apply plugin: 'java'
apply plugin: 'io.spring.dependency-management'
ext {
springBootVersion = '2.1.3.RELEASE'
}
// In this section you declare where to find the dependencies of your project
repositories {
// You can declare any Maven/Ivy/file repository here.
mavenCentral()
}
bootJar {
mainClassName = 'com.softwarelabs.App'
baseName = 'spring-boot-integration-test'
version = '0.1.0'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
// This dependency is found on compile classpath of this component and consumers.
compile 'com.google.guava:guava:23.0'
testCompile("org.springframework.boot:spring-boot-starter-test")
// Use JUnit test framework
testCompile 'junit:junit:4.12'
}
task wrapper(type: Wrapper) {
gradleVersion = '4.6'
}
For more details please check the spring boot gradle plugin documentation getting started part.
Check how the Spring Boot plugin reacts by other plugins.

I had this error while building a repository I had downloaded.
I was able to resolve the same by modifying my build.gradle to include a buildscript dependency for spring-boot-gradle-plugin and apply org.springframework.boot as a plugin
buildscript {
ext {
springBootVersion = '<Spring boot version>'
}
repositories {
...<my repository config>...
}
// These are gradle build dependencies and not application requirements
dependencies {
...<other dependencies>...
classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
}
}
apply plugin: 'org.springframework.boot'

Related

How to avoid repeating dependency versions in a multi-module Gradle project?

There's a sample Spring Boot project here that contains two modules.
The build.gradle for one of the modules looks like this:
buildscript {
ext { springBootVersion = '2.1.4.RELEASE' }
repositories { mavenCentral() }
dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") }
}
plugins {
id "io.spring.dependency-management" version "1.0.5.RELEASE"
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
bootJar {
baseName = 'gs-multi-module-application'
version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
repositories { mavenCentral() }
dependencies {
compile('org.springframework.boot:spring-boot-starter-actuator')
compile('org.springframework.boot:spring-boot-starter-web')
compile project(':library')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
The other module's build.gradle looks like this:
buildscript {
repositories { mavenCentral() }
}
plugins { id "io.spring.dependency-management" version "1.0.5.RELEASE" }
ext { springBootVersion = '2.1.4.RELEASE' }
apply plugin: 'java'
apply plugin: 'eclipse'
jar {
baseName = 'gs-multi-module-library'
version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
repositories { mavenCentral() }
dependencies {
compile('org.springframework.boot:spring-boot-starter')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
dependencyManagement {
imports { mavenBom("org.springframework.boot:spring-boot-dependencies:${springBootVersion}") }
}
The springBootVersion = '2.1.4.RELEASE' is declared in both modules. For a 2 module project that might not be a problem, but if my project had 10 modules and I wanted to make sure that all modules always depend on the same version of Spring Boot, it would be inconvenient and error-prone to repeat this version in every module.
Similarly, I might want to add a dependency on commons-io to both of these modules, and ensure they both always depend on the same version of commons-io.
How can I avoid repeating the version numbers in each and every build.gradle file?
See this Gradle documentation : a good practice in Gradle is to configure subprojects which share common traits in a single place, for example in the root project's build script (or using custom plugins)
EDIT the solution proposed here is no longer considered as good practice from Gradle team (the link above does not even mention subproject bloc in latests Gradle version doc); thank you #buggy for the warning .
In your example taken from Spring boot documentation, this pattern could be applied to centralize Spring boot and other common dependencies versions in a single place, but you could go further and also configure other common traits (Java plugin configuration, repositoties, etc..)
Here is how I would re-write the Spring example to make it cleaner and DRY:
Root project
/**
* Add Springboot plugin into build script classpath (without applying it)
* This is this only place where you need to define the Springboot version.
*
* See https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/html/#managing-dependencies-using-in-isolation
*/
plugins {
id "org.springframework.boot" version "2.1.4.RELEASE" apply false
}
// Set version for dependencies share between subprojects
ext {
commonsIoVersion = "2.6"
}
subprojects {
// common config for all Java subprojects
apply plugin: "java"
apply plugin: "eclipse"
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
// apply Spring Boot's dependency management plugin
apply plugin: "io.spring.dependency-management"
}
Library sub-project
// no need for additional plugins
jar {
baseName = 'gs-multi-module-library'
version = '0.0.1-SNAPSHOT'
}
dependencies {
implementation('org.springframework.boot:spring-boot-starter')
implementation "commons-io:commons-io:${commonsIoVersion}"
testCompile('org.springframework.boot:spring-boot-starter-test')
}
dependencyManagement {
imports {
mavenBom org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES
}
}
Application sub-project
plugins {
id "org.springframework.boot"
}
bootJar {
baseName = 'gs-multi-module-application'
version = '0.0.1-SNAPSHOT'
}
dependencies {
implementation project(':library')
implementation ('org.springframework.boot:spring-boot-starter-actuator')
implementation ('org.springframework.boot:spring-boot-starter-web')
implementation "commons-io:commons-io:${commonsIoVersion}"
// could also be configured in root project.
testCompile('org.springframework.boot:spring-boot-starter-test')
}
Notes
this solution uses the new plugins {} DSL only (no need for old buildscript block)
version of the io.spring.dependency-management should not be configured explicitly, it will be inherit from Spring boot plugin
You can move the ext{} block to a new file and reference it in your project's build.gradle file via the apply from: statement.
// project/versions.gradle
ext {
springBootVersion = '2.1.4.RELEASE'
}
// project/build.gradle
buildscript {
apply from: 'versions.gradle'
}
// module/build.gradle
dependencies {
implementation "some.dependency:dependency:$springBootVersion"
}
Now you only have to define your dependency versions in one place.
Typically, a project will have a project-level build.gradle file in addition to module-specific build.gradle files. However, the repo you shared is missing the project-level build script. This is why the ext{} block is defined in each module's build script. This is likely not optimal, and I recommend looking at other repos to see how different developers tackled this issue.
There's a Gradle consistent versions plugin that will handle this:
https://github.com/palantir/gradle-consistent-versions
After you set this plugin up in the root build.gradle file, you will create a versions.props file like this, to use the example from the documentation.
com.fasterxml.jackson.*:jackson-* = 2.9.6
com.google.guava:guava = 21.0
com.squareup.okhttp3:okhttp = 3.12.0
junit:junit = 4.12
org.assertj:* = 3.10.0
There's also a versions.locks file, which is auto-generated by ./gradlew --write-locks. You can then specify version-less dependencies in your build.gradle files.

gradle error Could not find method dependencyManagement()

Below is my build.gradle
buildscript {
ext {
springBootVersion = '2.0.0.M3'
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'org.springframework.boot'
apply plugin: 'maven-publish'
dependencyManagement {
imports {
mavenBom 'org.springframework.cloud:spring-cloud-starter-parent:Brixton.SR7'
}
}
dependencies {
compile("org.springframework.cloud:spring-cloud-starter-eureka")
compile "org.elasticsearch:elasticsearch:5.5.0"
testCompile('org.springframework.boot:spring-boot-starter-test')
}
I was using gradle 2.14 and got the below error
> Failed to apply plugin [id 'org.springframework.boot']
> Spring Boot plugin requires Gradle 3.4 or later. The current version is Gra
dle 2.14
Then I upgraded gradle to 3.4 as suggested in the error message.
Now I get the below error
Could not find method dependencyManagement() for arguments [build_79bcact4bkf1
sckkod1j3zl7l$_run_closure1#4a2d71c9] on root project 'myproject'
of type org.gradle.api.Project.
Is the method dependencyManagement() no longer available in gradle 3.4 ?
If anybody is aware of the alternate method to be used in gradle 3.4 , kindly revert
To use this DSL you have to provide the dependency-management-plugin:
buildscript {
repositories {
maven {
jcenter() //or mavenCentral()
}
}
dependencies {
classpath "io.spring.gradle:dependency-management-plugin:1.0.3.RELEASE"
}
}
apply plugin: "io.spring.dependency-management"
Or you can use:
plugins {
id "io.spring.dependency-management" version "1.0.3.RELEASE"
}
More details here.
For me the fix was replacing the distributionUrl in the gradle-wrapper.properties with:
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip
and updating the dependencies in the build.gradle file to:
dependencies { classpath "com.android.tools.build:gradle:7.0.4" }
In Gradle 7 this error is also caused by importing a BOM using:
dependencyManagement {
imports {
mavenBom "tech.jhipster:jhipster-dependencies:${jhipsterDependenciesVersion}"
}
}
In Gradle 7 you need to import your BOM in the following way:
implementation platform("tech.jhipster:jhipster-dependencies:${jhipsterDependenciesVersion}")

How do I create an an executable jar in Spring Boot?

I have a Spring MVC project using Spring Boot 1.5.2 with Gradle Buildship in Spring Tool Suite.
How do I create a JAR file from my source only which will run in another server and download the required dependencies there?
How do I create a fat JAR with all of the source files and the dependencies?
My gradle.build file is:
buildscript {
ext {
springBootVersion = '1.5.2.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'application'
mainClassName = "com.jtv.elastic.mvc.ElasticSpringApplication"
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
You should use bootRepackage task for gradle. More information here and here

Using spring-boot Gradle plugin but getting spring boot version from dependency management

I have a Gradle setup where I currently need to hard code the spring-boot version. Is it possible to do this with dependency management instead? The issue occurs in my project module that contains the actual code. There I need a call to :
springBoot {
requiresUnpack = ["com.example:util"]
}
In order to do that I need to apply the spring-boot plugin. For that plugin to be available I need to have a dependency to "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}". And for that I need to specify the springBootVersion hard coded...
This is my parent gradle file:
buildscript {
ext {
springBootVersion = '1.4.0.RELEASE'
}
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
classpath 'io.spring.gradle:dependency-management-plugin:0.6.0.RELEASE'
}
}
plugins {
id "io.spring.dependency-management" version "0.6.0.RELEASE"
}
group 'test'
repositories {
mavenCentral()
}
allprojects {
apply plugin: 'java'
apply plugin: 'io.spring.dependency-management'
dependencyManagement {
imports {
// Note that these two BOM's are not always easy to combine, always define cloud first and try to find versions with same version of spring-boot when upgrading.
mavenBom 'org.springframework.cloud:spring-cloud-starter-parent:Brixton.SR5'
mavenBom 'io.spring.platform:platform-bom:2.0.7.RELEASE'
}
}
}
and this is my module gradle file:
group = "test"
apply plugin: 'spring-boot'
apply plugin: 'io.spring.dependency-management'
sourceCompatibility = 1.8
targetCompatibility = 1.8
// Temporary fix for jersey
springBoot {
requiresUnpack = ["com.example:util"]
}
If I remove the dependency to spring-boot-gradle-plugin in parent gradle file, then I cannot use the apply plugin: 'spring-boot' in the module gradle file and without that the gradle DLS method springBoot { is not defined...
Can I achieve this without having to hard code springBootVersion = '1.4.0.RELEASE'in my parent gradle file?

Spring Boot Gradle add Native library failed (java.lang.UnsatisfiedLinkError)

I am trying to add a native library( .dll file) to a basic spring gradle project.
I have tried many different settings and all of them didn't work in a basic java project , I have successfully ran that dll file by adding VM argument: java.library.path gradle
here's the script:
buildscript {
ext {
springBootVersion = '1.2.5.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
jar {
baseName = 'messaging'
version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-aop")
compile("org.springframework.boot:spring-boot-starter-amqp")
compile("org.springframework:spring-messaging")
compile('javax.inject:javax.inject:1')
compile('com.google.guava:guava:11.0.2')
compile('org.codehaus.jackson:jackson-core-asl:1.1.0')
compile('org.codehaus.jackson:jackson-mapper-asl:1.9.13')
testCompile("org.springframework.boot:spring-boot-starter-test")
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
I have tried:
add VM arguments on eclipse
add jvm properties
add gradle system property ( can not succeeded to add that parameter)
I couldn't understand why but when I follow the steps below, it worked:
1. I changed the java package name
2. rebuild the project with gradle
3. run the project

Resources