in spring-boot web-app,
when i use #PastOrPresent STS shows me this error :
(PastOrPresent cannot be resolved to a type)
but org.springframework.boot:spring-boot-starter-validation entry is already among the dependencies.
Gradle file
buildscript {
ext {
springBootVersion = '1.5.9.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
group = 'com.domain'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
compileJava.options.encoding = "UTF-8"
compileTestJava.options.encoding = "UTF-8"
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-actuator')
compile('org.springframework.boot:spring-boot-actuator-docs')
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-security')
compile('org.springframework.boot:spring-boot-starter-validation')
compile('org.springframework.boot:spring-boot-starter-web')
runtime('org.springframework.boot:spring-boot-devtools')
runtime('org.postgresql:postgresql')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('org.springframework.security:spring-security-test')
}
UPDATE of Gradle file
...
compile('org.springframework.boot:spring-boot-starter-validation'){
exclude group: 'org.hibernate', module: 'hibernate-validator:5.3.6.Final'
}
compile 'org.hibernate:hibernate-validator:6.0.7.Final'
...
| \--- org.hibernate:hibernate-validator:5.3.6.Final -> 6.0.7.Final
| \--- org.hibernate.validator:hibernate-validator:6.0.7.Final
| +--- javax.validation:validation-api:2.0.1.Final -> 1.1.0.Final
| +--- org.jboss.logging:jboss-logging:3.3.0.Final -> 3.3.1.Final
| \--- com.fasterxml:classmate:1.3.1 -> 1.3.4
have i some thing else to do ? while inside the entity i still have the same error. with new one : The type NotEmpty is deprecated (as shows the doc here)
The problem is that #PastOrPresent constraint is a part of Bean Validation 2.0 (see here), while the Spring Boot is still using Hibernate Validator 5.3 by default (see the dependencies here), which is BeanValidation 1.1. If you'd like to use this new constraint you would need to have an explicit dependency for Hibernate Validator 6.0 and exclude 5.4 version from your current dependencies.
Related
I am using eclipse 2018, gradle 5.2.1, buildship 3.0.1.
My config looks like:
I try to create spring boot 2 according to building-spring-boot-2-projects-with-gradle
The build.gradle is:
plugins {
id 'java'
id 'com.gradle.build-scan' version '2.1'
id 'org.springframework.boot' version '2.1.3.RELEASE'
}
repositories {
jCenter()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-dependencies:2.1.3.RELEASE'
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
components {
withModule('org.springframework:spring-beans') {
allVariants {
withDependencyConstraints {
// Need to patch constraints because snakeyaml is an optional dependency
it.findAll { it.name == 'snakeyaml' }.each { it.version { strictly '1.23' } }
}
}
}
}
}
buildScan {
// always accept the terms of service
termsOfServiceUrl = 'https://gradle.com/terms-of-service'
termsOfServiceAgree = 'yes'
// always publish a build scan
publishAlways()
}
bootJar {
mainClassName = 'gt4.App'
}
However, after I save build.gradle, the Project and External Dependencies disappear, and the spring boot jars are not downloaded too.
What I was wrong?
If I create spring boot project with gradle by Spring Tool Suite 4, the generated build.gradle is:
plugins {
id 'org.springframework.boot' version '2.1.3.RELEASE'
id 'java'
}
apply plugin: 'io.spring.dependency-management'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
then it works.
Do I use spring-boot-dependencies wrong?
try to deleting the local Repository and build project again to download dependancy.
Delete .m2 or .gradle folder and then Rebuild your project.
a)On a Windows machine, the .gradle or .m2 path will be:
c:\Users\username\.m2 or c:\Users\username\.m2
b)On linux machine, the .gradle or .m2 path will be:
USER_HOME/.m2/
I am using the kotlin-gradle-plugin, configuring it as follows in build.gradle:
buildscript {
ext {
springBootVersion = '2.1.2.RELEASE'
kotlinVersion = '1.3.11'
}
repositories {
mavenLocal()
mavenCentral()
maven { url "http://repo.spring.io/plugins-release" }
maven { url "https://plugins.gradle.org/m2/" }
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}"
classpath "org.jetbrains.kotlin:kotlin-allopen:${kotlinVersion}"
}
apply plugin: 'kotlin'
apply plugin: 'kotlin-spring'
When I run gradle dependencies I get the following kotlinCompilerClasspath among other things:
kotlinCompilerClasspath
\--- org.jetbrains.kotlin:kotlin-compiler-embeddable:1.3.11
+--- org.jetbrains.kotlin:kotlin-stdlib:1.3.11
| +--- org.jetbrains.kotlin:kotlin-stdlib-common:1.3.11
| \--- org.jetbrains:annotations:13.0
+--- org.jetbrains.kotlin:kotlin-script-runtime:1.3.11
\--- org.jetbrains.kotlin:kotlin-reflect:1.3.11
\--- org.jetbrains.kotlin:kotlin-stdlib:1.3.11 (*)
When I disabled transitive dependencies in my build.gradle as follows:
configurations {
all {
transitive = false
resolutionStrategy {
// fail eagerly on version conflict (includes transitive dependencies)
// e.g. multiple different versions of the same dependency (group and name are equal)
failOnVersionConflict()
}
}
}
Now when I run gradle dependencies I get the following kotlinCompilerClasspath
kotlinCompilerClasspath
\--- org.jetbrains.kotlin:kotlin-compiler-embeddable:1.3.11
How can I add dependencies to the above kotlinCompilerClasspath while still using transitive = false configuration?
For example, I have tried adding the dependency org.jetbrains.kotlin:kotlin-stdlib to the regular dependencies block and to the buildscript block but still the above kotlinCompilerClasspath does not change.
Gradle version
gradle --version
------------------------------------------------------------
Gradle 5.1.1
------------------------------------------------------------
Build time: 2019-01-10 23:05:02 UTC
Revision: 3c9abb645fb83932c44e8610642393ad62116807
Kotlin DSL: 1.1.1
Kotlin: 1.3.11
Groovy: 2.5.4
Ant: Apache Ant(TM) version 1.9.13 compiled on July 10 2018
JVM: 1.8.0_191 (Oracle Corporation 25.191-b12)
OS: Mac OS X 10.14.2 x86_64
UPDATE
As recommended by hotkey below, I filtered out kotlin from the configuration, as follows.
configurations {
matching { !it.name.toLowerCase().contains('kotlin') }.all {
transitive = false
resolutionStrategy {
// fail eagerly on version conflict (includes transitive dependencies)
// e.g. multiple different versions of the same dependency (group and name are equal)
failOnVersionConflict()
}
}
}
I would recommend not to disable transitive dependencies for the kotlinCompilerClasspath configuration in the first place, the Kotlin Gradle plugin expects that configuration to pull the transitive dependencies of the Kotlin compiler as well:
configurations {
matching { it.name != 'kotlinCompilerClasspath' }.all {
transitive = false
/* ... */
}
}
If you still want to disable transitive dependencies for all configurations, you can instead manually add those dependencies which were reported as transitive for the kotlin-compiler-embeddable module:
dependencies {
kotlinCompilerClasspath 'org.jetbrains.kotlin:kotlin-stdlib'
kotlinCompilerClasspath 'org.jetbrains.kotlin:kotlin-script-runtime'
kotlinCompilerClasspath 'org.jetbrains.kotlin:kotlin-reflect'
kotlinCompilerClasspath 'org.jetbrains:annotations:13.0'
}
I'm trying to make a custom standalone Kotlin plugin for Gradle and am trying to apply that to another project, but am getting a Plugin with id 'my.id.license-tools' not found error when trying to apply it to another project.
This is the structure of the plugin's project:
license-tools
+--src
+--main
| +--java
| |
| +--kotlin
| | +--my.id
| | +--LicenseTools.kt
| |
| +--resources
| +--META-INF.gradle-plugins
| +--my.id.license-tools.properties
+--test
+--build.gradle
This is my build.gradle file:
buildscript {
ext.kotlin_version = '1.2.21'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
group 'my.id'
version '0.1'
apply plugin: 'java'
apply plugin: 'kotlin'
apply plugin: 'idea'
apply plugin: 'maven'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
compile gradleApi()
testCompile group: 'junit', name: 'junit', version: '4.12'
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
Then we have the Plugin class LicenseTools.kt, which I've held very simple to make sure the error didn't come from there:
package my.id
import org.gradle.api.Plugin
import org.gradle.api.Project
open class LicenseTools: Plugin<Project> {
override fun apply(project: Project?) {
println("License-Tools plugin has been applied")
}
}
And lastly my.id.license-tools.properties:
implementation-class=my.id.LicenseToolsKt
I've also tried
implementation-class=my.id.LicenseTools
After building everything with ./gradlew clean build I've run ./gradlew install to install the whole thing to the local Maven repository, which it did successfully.
Then I went ahead and added the following to my other project's buildscript block:
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "my.id:license-tools:0.1"
}
And after that added
apply plugin: 'my.id.license-tools'
After trying out many different things, like changing to the new plugins block or making sure the artifacts get correctly installed to the local maven repository I can't find the root of the problem, since according to the many guides I've read through, this should be the correct way to go about making a standalone plugin.
you probably figured by now, but your my.id.license-tools.properties should be named my.id.properties
the content should then be implementation-class=my.id.LicenseTools
then the classpath should be named
classpath "my.id:license-tools:0.1" -> classpath "my.id:LicenseTools:0.1"
and
apply plugin: 'my.id.license-tools' -> apply plugin: 'my.id.LicenseTools'
so that it always is:
package: my.id
name: LicenseTools
I haven't actually tested all of that, though.
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?
What does a correct Gradle configuration look like in a multi-module project that uses the Gradle plugins spring-boot-dependencies and spring-boot?
I have the following project setup:
parent
|
+ build.gradle
|
+ alpha
| |
| + build.gradle
|
+ beta
| |
| + build.gradle
The parent module contains common project configuration.
The alpha module is a module in which I would like to import dependencies using versions numbers specified in the spring-boot-dependencies bom, but the result of the is a standard jar.
The beta module is a module that depends on alpha and the result is an executable Spring Boot jar file (including all dependencies). Consequently, this project needs both the spring-boot-dependencies as well as the spring-boot plugin.
In order to keep the Gradle files DRY, I have extracted common module scripts to the parent's build.gradle file.
Attempts to execute $ gradle build using the project configuration below results in:
> Plugin with id 'io.spring.dependency-management' not found.
parent gradle.build
allprojects {
group = "com.example"
version '0.0.1-SNAPSHOT'
ext {
dependencyManagementPluginVersion = '0.5.3.RELEASE'
springBootVersion = '1.3.0.RC1'
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
}
subprojects {
sourceCompatibility = 1.8
targetCompatibility = 1.8
buildscript {
repositories {
jcenter()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
dependencies {
classpath("io.spring.gradle:dependency-management-plugin:${dependencyManagementPluginVersion}")
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'io.spring.dependency-management'
dependencyManagement {
imports {
mavenBom("org.springframework.boot:spring-boot-dependencies:${springBootVersion}")
// mavenBom("org.springframework.boot:spring-boot-starter-parent:${springBootVersion}")
}
}
}
alpha build.gradle
dependencies {
compile('org.springframework:spring-web')
}
beta gradle.build
apply plugin: 'spring-boot'
dependencies {
compile project(':alpha')
compile('org.springframework.boot:spring-boot-starter')
compile('org.springframework.boot:spring-boot-starter-web')
}
Comments:
The behavior of the spring-boot plugin was changed in Spring Boot 1.3.0.M1
Gradle version: 2.8
Spring Boot version 1.3.0.RC1
It turns out that parent/build.gradle should be rearranged in the following way:
buildscript {
ext {
dependencyManagementPluginVersion = '0.5.3.RELEASE'
springBootVersion = '1.3.0.RC1'
}
repositories {
jcenter()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
dependencies {
classpath("io.spring.gradle:dependency-management-plugin:${dependencyManagementPluginVersion}")
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
allprojects {
group = "com.example"
version '0.0.1-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
}
subprojects {
sourceCompatibility = 1.8
targetCompatibility = 1.8
apply plugin: 'io.spring.dependency-management'
dependencyManagement {
imports {
mavenBom("org.springframework.boot:spring-boot-dependencies:${springBootVersion}")
// mavenBom("org.springframework.boot:spring-boot-starter-parent:${springBootVersion}")
}
}
}
The problem lies in the fact that buildscript block for subprojects was indeed configured well but... in a wrong place. This subprojects block relates to subprojects but it will be evaluated in the script it was declared, and there were no dependencies declared for the plugin it was trying to apply.