Gradle kotlin-spring plugin don't open spring classes - gradle

I have following gradle script:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${SPRING_BOOT_VERSION}")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${KOTLIN_VERSION}")
classpath("org.jetbrains.kotlin:kotlin-allopen:${KOTLIN_VERSION}")
}
}
apply plugin: org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapper
apply plugin: org.jetbrains.kotlin.allopen.gradle.SpringGradleSubplugin
apply plugin: org.springframework.boot.gradle.plugin.SpringBootPlugin[code]
It purpose it to serve as base build for all kotlin based services. Here is how I am applying it:
apply from: '../../xxx/xxx.gradle'
Then in this project when I have applied generic build template I have something like this
#SpringBootApplication
open class MainClass
If I remove open it complains that this class shouldn't be final. I would assume that if I use kotlin-spring I don't need to add manually open. I checked this setup against working one from spring initializr and I can't spot the difference. Here is working version
buildscript {
ext {
kotlinVersion = '1.1.4-3'
springBootVersion = '1.5.7.RELEASE'
}
repositories {
mavenCentral()
}
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'
apply plugin: 'org.springframework.boot'
I can't spot the difference here. Thanks in advance for any suggestions.

try
apply {
plugin("kotlin")
plugin("kotlin-spring")
plugin("org.springframework.boot")
}
https://github.com/spring-petclinic/spring-petclinic-kotlin/blob/master/build.gradle.kts

Related

Gradle kotlin in Gradle5.2 Unresolved reference: dependtest

A multi module project with Kotlin source code, which used to work, stops working after upgrading to Gradle 5.2, because the Kotlin classes from the compile project('depend-test') dependency are not found.
Attempted to change plugin version
already viewed https://github.com/gradle/gradle/issues/8980
i defind Test class in project('depend-test')
object Test {
const val test = "123"
}
i want to use Test class in project('test-test')
package com.example.test.controller
import com.example.dependtest.Test
import org.slf4j.LoggerFactory
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
#RestController
#RequestMapping
class TestController {
private val log = LoggerFactory.getLogger(TestController::class.java)
#GetMapping(value = ["/test"])
fun test() {
log.info(Test.test)
}
}
when i want to build project('test-test') to jar where i used gradle bootJar。 I get this error:
> Task :test-test:compileKotlin FAILED
e: /Users/houshuai/Documents/dev/demo/test/test-test/src/main/kotlin/com/example/test/controller/TestController.kt: (3, 20): Unresolved reference: dependtest
e: /Users/houshuai/Documents/dev/demo/test/test-test/src/main/kotlin/com/example/test/controller/TestController.kt: (22, 18): Unresolved reference: Test
Expected Behavior
The Kotlin classes in the compile project('depend-test') dependency should be found.
Current Behavior
The Kotlin classes in the compile project('depend-test') dependency are not found:
Try adding this to your build.gradle file
bootJar {
enabled = false
}
jar {
enabled = true
}
Just in case someone else comes across this problem.
I created two modules, test-test and depend-test.
The depend-test project is test-test 's dependency.
I tried to call the parameters of depend-test, but it failed to compile and package.
Env
gradle-5.2.1
Kotlin 1.3.31
Springboot 2.1.4
java 1.8
step one
Edit settings.gradle
rootProject.name = 'demo'
include ":depend-test"
include ":test-test"
project(":depend-test").projectDir = file("depend/depend-test")
project(":test-test").projectDir = file("test/test-test")
I used the 1.3.31 version of the kotlin plug-in. The build.gradle file reads as follows
buildscript {
ext {
kotlinVersion = '1.3.31'
}
repositories {
mavenCentral()
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/'}
maven { url "https://plugins.gradle.org/m2/" }
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlinVersion"
}
}
plugins {
id 'org.springframework.boot' version '2.1.4.RELEASE'
id 'org.jetbrains.kotlin.jvm' version '1.2.71'
id 'org.jetbrains.kotlin.plugin.spring' version '1.2.71'
}
allprojects {
apply plugin: 'idea'
apply plugin: 'kotlin'
repositories {
mavenCentral()
}
}
subprojects {
apply plugin: 'kotlin'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: "application"
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
repositories {
mavenLocal()
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/'}
maven { url "https://plugins.gradle.org/m2/" }
mavenCentral()
jcenter()
maven { url "http://repo.spring.io/snapshot" }
maven { url "http://repo.spring.io/milestone" }
maven { url 'http://maven.springframework.org/release' }
maven { url 'http://maven.springframework.org/milestone' }
}
version = '1.0'
apply plugin: 'io.spring.dependency-management'
group = 'com.mutil.test'
sourceCompatibility = '1.8'
compileKotlin {
kotlinOptions {
freeCompilerArgs = ['-Xjsr305=strict']
jvmTarget = '1.8'
}
}
compileTestKotlin {
kotlinOptions {
freeCompilerArgs = ['-Xjsr305=strict']
jvmTarget = '1.8'
}
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'com.fasterxml.jackson.module:jackson-module-kotlin'
implementation 'org.jetbrains.kotlin:kotlin-reflect'
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
}
dependencies {
subprojects.forEach {
archives(it)
}
}
repositories {
mavenCentral()
}
step two
build jar for test-test project ,I used two ways, but the results were the same.
terminal use cmd is ./gradlew :test-test:bootJar
user IDEA gradle tool
result
The class file written by kotlin in the submodule cannot be found.
I do not know if the lack of necessary plug-ins caused the failure to package properly.

Gradle doesn't copy html files into executed jar

build.gradle
buildscript {
ext {
springBootVersion = '2.0.6.RELEASE'
springRestDocsVersion = '2.0.2.RELEASE'
gradleDockerVersion = '1.2'
asciidoctorGradlePluginVersion = '1.5.9.2'
}
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("org.asciidoctor:asciidoctor-gradle-plugin:${asciidoctorGradlePluginVersion}")
classpath("se.transmode.gradle:gradle-docker:${gradleDockerVersion}")
}
}
plugins {
id "org.springframework.boot" version "2.0.6.RELEASE"
id "org.asciidoctor.convert" version "1.5.9.2"
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'maven'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'org.asciidoctor.convert'
apply plugin: 'application'
apply plugin: 'docker'
apply from: 'local.gradle'
mainClassName = 'class.path.package.BootApplication'
version = '0.0.3a'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
// Standard spring boot packages
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.cloud:spring-cloud-starter-config')
compile('org.springframework.boot:spring-boot-starter-jersey')
compile('org.springframework.boot:spring-boot-starter-security')
compile('org.springframework.boot:spring-boot-starter-validation')
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client')
compile('org.postgresql:postgresql')
compile('biz.paluch.redis:lettuce:4.4.6.Final')
compile('org.pacesys:openstack4j-core:3.1.0')
compile('org.pacesys.openstack4j.connectors:openstack4j-httpclient:3.1.0')
// Dozer object mapping
compile('net.sf.dozer:dozer:5.5.1')
// Sengrid library
compile('com.sendgrid:sendgrid-java:4.2.1')
compile('org.freemarker:freemarker:2.3.28')
// Cloudant
compile('com.cloudant:cloudant-client:2.9.0')
compile('com.squareup.okhttp3:okhttp-urlconnection:3.4.2')
compile('org.json:json:20171018')
compile('javax.validation:validation-api:2.0.1.Final')
//Swagger2
compile('io.springfox:springfox-swagger2:2.9.2')
compile('io.springfox:springfox-swagger-ui:2.9.2')
runtime('org.springframework.boot:spring-boot-devtools')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('org.springframework.security:spring-security-test')
testCompile("org.springframework.restdocs:spring-restdocs-mockmvc")
testCompile("org.asciidoctor:asciidoctor-gradle-plugin:1.5.9.2")
asciidoctor('org.springframework.restdocs:spring-restdocs-asciidoctor:2.0.2.RELEASE')
}
processResources {
// expand(project.properties)
}
ext {
springCloudVersion = 'Finchley.RELEASE'
snippetsDir = file('build/generated-snippets')
// springRestDocsVersion = '2.0.2.RELEASE'
}
ext['spring-restdocs.version'] = '${springRestDocsVersion}'
test {
outputs.dir snippetsDir
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
mavenBom("org.springframework.boot:spring-boot-dependencies:${springBootVersion}")
}
}
asciidoctor {
// attributes "snippets": snippetsDir
inputs.dir snippetsDir
dependsOn test
}
jar {
dependsOn asciidoctor
from ('${asciidoctor.outputDir}/html5') {
into 'static/apidocs'
}
}
my problem actually in this part:
jar {
dependsOn asciidoctor
from ('${asciidoctor.outputDir}/html5') {
into 'static/apidocs'
}
}
executed jar doesn't copy index.html file executed from asciidoctor into jar and jar executed only without this file
So how i can copy build/asciidoc/html5/index.html from build
folder into executed jar?
Also if i need to use bootJar task so how i can make it include all jar needed into /BOOT-INF/lib/
Your configuration is almost right, but you are customising the wrong task. A Spring Boot fat jar is built with the bootJar task rather than the jar task.
You need to replace jar with bootJar in your build.gradle. This will result in it looking the same as the following example:
bootJar {
dependsOn asciidoctor
from ("${asciidoctor.outputDir}/html5") {
into 'static/docs'
}
}
This is the configuration that's described in the REST Docs documentation.

Gradle Multimodule Project unable to resolve class of depending sub module

The application works fine in Eclipse. But I am not able to build the application via gradle (gradle clean build)
The structure is like:
financial-calculator (parent)
library
api
api wants to use classes of library
My parent's settings.gradle file:
rootProject.name = 'financial-calculator'
include 'library'
include 'api'
rootProject.children.each { it.name = rootProject.name + '-' + it.name }
The parent build.gradle file:
buildscript {
ext {
springBootVersion = '2.0.3.RELEASE'
}
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
classpath "o.s.b:spring-boot-gradle-plugin:${springBootVersion}"
}
}
allprojects {
apply plugin: 'eclipse'
apply plugin: 'maven'
apply plugin: 'maven-publish'
group = 'net.hemisoft.financial.calculator'
version = "0.1.0-SNAPSHOT"
}
subprojects {
apply plugin: 'groovy'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
sourceCompatibility = JavaVersion.VERSION_1_8
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
compile 'o.c.g:groovy'
testCompile 'o.s.b:spring-boot-starter-test'
}
dependencyManagement {
imports { mavenBom("o.s.b:spring-boot-dependencies:${springBootVersion}") }
}
}
project(rootProject.name + '-library') {
dependencies {
compile 'o.s.b:spring-boot-starter'
}
}
project(rootProject.name + '-api') {
dependencies {
compile project (':' + rootProject.name + '-library')
compile 'o.s.b:spring-boot-starter-web'
}
}
The complete small project you will find under link
Thanks for your hints.

Cannot resolve external dependency org.springframework.boot:spring-boot-starter: because no repositories are defined

I have a multibuild project and I am currently just setting it up. Each module naturally each has a gradle.build file that only contains the following:
dependencies {
}
In the main build.gradle file I have want is needed for every module. However when I do a gradle build I get a error saying:
Cannot resolve external dependency org.springframework.boot:spring-boot-starter: because no repositories
are defined. Required by:
project :
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'io.spring.dependency-management'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
buildscript {
ext {
springBootVersion = '2.0.0.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
sourceSets.all { ext.purpose = null }
// Everything in subprojects are applied to all modules
subprojects {
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
version = '0.0.1-SNAPSHOT'
test {
useTestNG()
testLogging.showStandardStreams = true
beforeTest { descriptor ->
logger.lifecycle("Running test: " + descriptor)
}
// listen to standard out and standard error of the test JVM(s)
onOutput { descriptor, event ->
logger.lifecycle("Test: " + descriptor + " produced standard out/err: " + event.message )
}
}
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
repositories {
jcenter()
mavenCentral()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
compile('org.springframework.boot:spring-boot-starter')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
}
dependencies {
compile('org.springframework.boot:spring-boot-starter')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
Advice
You've defined repositories for subprojects only, but you have to define it in the root project too, because you have a dependencies block there:
dependencies {
compile('org.springframework.boot:spring-boot-starter')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
In your case you can do it by declaring repositories once more out of the subprojects closure:
repositories {
jcenter()
mavenCentral()
maven {
url "https://plugins.gradle.org/m2/"
}
}
subprojects {
...
}
Or you can define it for all projects:
allprojects {
repositories {
jcenter()
mavenCentral()
maven {
url "https://plugins.gradle.org/m2/"
}
}
}
in that case, you don't need o declare it in subprojects closure
I fixed in Intellij IDEA with these steps.
Go to Settings->Gradle
Under 'Use Gradle from' provide the correct version of Gradle
Gradle JVM I selected as local JAVA_HOME

Plugin with id 'org.sonarqube' not found

I am trying to implement sonar with gradle for code-coverage measure for my project.
we are using gradle-4.0.1 and sonarqube-6.4 .
when I run gradle sonarqube from command line I get this error-
Plugin with id 'org.sonarqube' not found.
I tried few code changes but no luck, please help.
My build.gradle file is as below-
buildscript {
ext {
springBootVersion = '1.5.4.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'org.sonarqube'
apply plugin: "jacoco"
apply plugin: "java"
apply plugin: "war"
apply plugin: "org.springframework.boot"
sonarqube {
properties {
property "sonar.projectName","Spring4WebService Code Coverage Demo"
property "sonar.projectKey", "org.sonarqubeJacocoCodeCoverage"
property "sonar.reportPath" , "${project.buildDir}/jacoco/test.exec"
}
}
test{
ignoreFailures = true
}
ext {
jacocoVersion = '0.7.6.201602180812'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
sourceSets {
main.java.srcDir "src/main/java"
test.java.srcDir "src/test/java"
}
springBoot {
mainClass = "com.concretepage.config.WebAppInitializer"
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web','com.fasterxml.jackson.core:jackson-databind')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
jacoco{
toolVersion = "${jacocoVersion}"
}
jacocoTestReport {
reports{
html.enabled=true
xml.enabled=true
csv.enabled=true
}
}
Just like the 'org.springframework.boot' plugin, the 'org.sonarqube' plugin does not belong to Gradle. It is a third-party plugin, so you need to add it as a buildscript dependency:
buildscript {
ext {
springBootVersion = '1.5.4.RELEASE'
}
repositories {
mavenCentral()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.5"
}
}
Now apply plugin: 'org.sonarqube' should work fine.
In my case it looks like:
plugins {
id 'groovy'
id 'application'
id 'org.sonarqube' version '3.0'
}
repositories {
mavenCentral()
}
sonarqube {
properties {
property "sonar.host.url", "http://sonarqube:9000"
property "sonar.sources", "src"
}
}
tasks['sonarqube'].dependsOn test
Using the plugins DSL specifying a full version (e.g., id "org.sonarqube" version "3.5.0.2730" instead of id "org.sonarqube" version "3.5.0") in the plugins section of build.gradle resolved this issue for me.
Here are examples for plugins DSL and legacy plugin application: https://plugins.gradle.org/plugin/org.sonarqube

Resources