Yes, I know, there are many question about similar problems, but I haven't found an answer for me.
I want to host a telegram bot written in Kotlin on Heroku.
My gradle file:
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.3.41'
id 'distribution'
}
jar {
baseName = 'mybot'
version = '0.1'
}
group 'ru.ilagent.mybot'
version '0.1'
repositories {
mavenCentral()
maven { url "https://jitpack.io" }
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
implementation 'io.github.seik.kotlin-telegram-bot:telegram:0.3.4'
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
task stage {
dependsOn installDist
}
I follow the instruction https://devcenter.heroku.com/articles/getting-started-with-kotlin#deploy-the-app , also I've added stage and jar. I can push my bot to heroku and call ps:scale successfully. But nothing works. When I'm finding out logs, I see the error: "Unable to access jarfile build/libs/mybot-0.1.jar" . When I call the jar task locally, jar-file is created with a path mentioned in the log.
Also I've created Procfile
web: java -jar build/libs/mybot-0.1.jar
Something went wrong (. Please, help!
That's working code:
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.3.41'
}
version '0.1'
apply plugin: 'application'
sourceCompatibility = 1.8
repositories {
mavenCentral()
maven { url "https://jitpack.io" }
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
implementation 'io.github.seik.kotlin-telegram-bot:telegram:0.3.4'
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
mainClassName = 'ru.ilagent.mybot.MainKt'
task stage {
dependsOn installDist
}
and Procfile
web: build/install/mybot/bin/mybot
Hope that be helpful for anybody)
Related
I have a sample project with the following hierearhy:
Sample (root)
-- model (simple jar)
-- api (springboot jar)
I want to publish both generated jars: plain jar & bootJar to my localRepository.
gradlew clean build -xTest publishToMavenLocal
However, the following error occures:
* What went wrong:
Execution failed for task ':api:publishMavenJavaPublicationToMavenLocal'.
> Failed to publish publication 'mavenJava' to repository 'mavenLocal'
> Artifact api.jar wasn't produced by this build.
The root build.gradle is a follows:
plugins {
id 'java'
id "org.springframework.boot" version "2.2.5.RELEASE" apply false
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
}
group 'sample'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
ext {
artifactVersion = version
springBootVersion = "2.2.5.RELEASE"
}
allprojects {
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'maven'
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
repositories {
mavenCentral()
jcenter()
}
}
subprojects {
apply plugin: "io.spring.dependency-management"
apply plugin: "maven-publish"
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
dependencyManagement {
imports {
mavenBom "org.springframework.boot:spring-boot-dependencies:${springBootVersion}"
}
}
dependencies {
implementation "org.springframework.boot:spring-boot-dependencies:${springBootVersion}"
}
publishing {
publications {
mavenJava(MavenPublication) {
groupId project.group
artifactId project.name
version project.version
from components.java
}
}
}
}
api build.gradle
apply plugin: 'org.springframework.boot'
dependencies {
compile project(":model")
implementation "org.springframework.boot:spring-boot-starter-web"
}
bootJar {
}
Adding bootJava task to api build.gradle allowes to publish the bootJar directly from api module, but the root publish task remains broken.
publishing {
publications {
bootJava(MavenPublication) {
artifact bootJar
}
}
}
I've tried almost every solution from docs & google, but none seem to work.
Can anyone explain, what is misconfigured?
Gradle version: 6.3
As stated by gradle documentation here:
Starting from Gradle 6.2, Gradle performs a sanity check before uploading, to make sure you don’t upload stale files (files produced by another build). This introduces a problem with Spring Boot applications which are uploaded using the components.java component
More explanation is available in the link above.
They propose the following workaround that I personally tried and worked for me :
configure the outgoing configurations
configurations {
[apiElements, runtimeElements].each {
it.outgoing.artifacts.removeIf { it.buildDependencies.getDependencies(null).contains(jar) }
it.outgoing.artifact(bootJar)
}
}
here after the configuration from my build.gradle:
....
apply plugin: 'maven-publish'
...
configurations {
[apiElements, runtimeElements].each {
it.outgoing.artifacts.removeIf { it.buildDependencies.getDependencies(null).contains(jar) }
it.outgoing.artifact(bootJar)
}
....
}
publishing {
publications {
myPublication(MavenPublication) {
groupId groupId
artifactId artifactId
version version
from components.java
versionMapping {
usage('java-api') {
fromResolutionOf('runtimeClasspath')
}
usage('java-runtime') {
fromResolutionResult()
}
}
}
}
repositories {
maven {
url azureRepoUrl
name azureRepoName
credentials {
username azureRepoUserName
password azureRepoAccessToken
}
}
}
}
Excerpt from
Starting from Gradle 6.2, the main jar task is disabled by the Spring Boot application, and the component expects it to be present. Because the bootJar task uses the same file as the main jar task by default, previous releases of Gradle would either:
publish a stale bootJar artifact
or fail if the bootJar task hasn’t been called previously
To simple workaround would be configuring the outgoing configurations. For multi-module Gradle project, place the below configuration in the service module(spring boot module).
dependencies {
.....
}
configurations {
[apiElements, runtimeElements].each {
it.outgoing.artifacts.removeIf {
it.buildDependencies.getDependencies(null).contains(jar)
}
it.outgoing.artifact(bootJar)
}
}
Note: There is no need for changing anything with artifactory task if it was configured correctly. This working solution has been tested with Gradle 6.4.1.
Don't try the alternate suggestion that they provided, because classifier attribute is deprecated in recent versions, also altering the bootJar task with custom configuration would result in improper uber jar construction, and if you extract the generated jar distributive, you could find the missing BOOT-INF directory and necessary META-INF/MANIFEST.MF values.
jar {
enabled = true
}
bootJar {
classifier = 'application'
}
Update:
From Spring Boot 2.5.0, jar task generates an additional jar archive which ends with -plain.jar. It may break someone's build if they have used some patterns like *.jar to copy the build archive, hence, to restrict the additional jar creation, the following jar task configuration code snippet should be used.
jar {
enabled = false
}
I could get this worked by just adding artifact bootJar in the publishing task as shown below and with out adding any configurations as suggested in the gradle documentation. I believe this could be working same as their first workaround in the documentation. Tested with gradle 6.5.1
publishing {
publications {
mavenJava(MavenPublication) {
artifact bootJar
artifact sourceJar {
classifier "sources"
}
}
}
}
project.tasks.publish.dependsOn bootJar
According to the 'Gradle' documentation under,
https://docs.gradle.org/current/userguide/upgrading_version_6.html#publishing_spring_boot_applications
Just add the following to the build.gradle file
jar {
enabled = true
}
bootJar {
classifier = 'application'
}
If you are using gradle kotlin dsl add the equivalent in your build.gradle. It worked for me
configurations {
val elements = listOf(apiElements, runtimeElements)
elements.forEach { element ->
element.get().outgoing.artifacts.removeIf { it -> it.buildDependencies.getDependencies(null).contains(tasks.jar.get())}
element.get().outgoing.artifact(tasks.bootJar.get())
}
}
For Spring Boot 2.5.0+, this configurations works for publishing the embedded jar, its sources and javadoc:
plugins {
id 'maven-publish'
id 'java-library'
}
jar {
enabled = false
}
java {
withSourcesJar()
withJavadocJar()
}
publishing {
publications {
publication(MavenPublication) {
artifact bootJar
from components.java
}
}
}
Im trying to get Proguard to work but Im still new to Gradle.
My build gradle.kts haves an error (Unresolved reference: proguard), I cant create a proguard Task:
plugins {
id("com.github.johnrengelman.shadow") version "5.2.0"
java
kotlin("jvm") version "1.3.61"
}
group = "*...*"
version = "*...*"
repositories {
mavenCentral()
jcenter()
}
dependencies {
implementation(kotlin("stdlib-jdk8"))
//*...*
implementation("net.sf.proguard","proguard-gradle","6.2.2") //is this correct?
}
configure<JavaPluginConvention> {
sourceCompatibility = JavaVersion.VERSION_1_8
}
tasks {
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
jar{
manifest {
attributes["Main-Class"] = "*...*"
}
}
shadowJar{
archiveBaseName.set("*...*")
archiveClassifier.set("")
archiveVersion.set("")
}
register<proguard.gradle.ProGuardTask>("myProguardTask") { //Unresolved reference: proguard
}
}
This is not an Android Project
Because Stackoverflow wants me to write more than just code: Im planing to somehow link the proguard output to the shadowjar task. If you know how to do it Im also interested to that (and I could not try it myself because of this problem).
You declared a dependency of proguard in project rather than for Gradle itself.
Move the dependency to the buildscript block:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'net.sf.proguard:proguard-gradle:6.2.2'
}
}
Then you should be able to create your task.
Alternatively, you can declare the repository in settings.gradle.kts:
pluginManagement {
repositories {
jcenter()
}
}
which will trim down the buildscript block in build.gradle.kts:
buildscript {
dependencies {
classpath("net.sf.proguard:proguard-gradle:6.2.2")
}
}
Could you please help me to find a proper way to rename built artifact with Gradle 5 Kotlin DSL
I created a Gradle 5.5.1 Spring Boot 2 project based on Kotlin DSL.
After executing gradle build the built artifact is inside $buildDir/libs folder.
How can I rename it? Let's say I want to give a simple name - app.jar
plugins {
id("java")
id("idea")
id("org.springframework.boot") version "2.1.5.RELEASE"
id("io.spring.dependency-management") version "1.0.8.RELEASE"
}
group = "com.hbv"
version = "1.0.0-SNAPSHOT"
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.1.5.RELEASE")
}
}
the<DependencyManagementExtension>().apply {
imports {
mavenBom(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES)
}
}
java {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
implementation(platform("org.springframework.cloud:spring-cloud-dependencies:Greenwich.RELEASE"))
implementation("org.springframework.cloud:spring-cloud-starter-config")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-actuator")
implementation("org.springframework.boot:spring-boot-starter-security")
testImplementation("com.h2database", "h2")
testImplementation("org.springframework.boot", "spring-boot-starter-test")
testImplementation("org.springframework.security", "spring-security-test")
}```
Configure the bootJar task which is generating the jar and is a gradle jar task and set its archiveFileName property:
tasks {
bootJar {
archiveFileName.set("app.jar")
}
}
define archiveBaseName in jar task
tasks.withType<Jar> {
archiveBaseName.set("app")
manifest {
attributes["Main-Class"] = application.mainClass
attributes["Implementation-Version"] = archiveVersion
attributes["Implementation-Title"] = "Test for App"
}
}
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.
I'm trying to build a plugin for Intellij but I'm getting a java.lang.NoClassDefFoundError at runtime every time my code point to a class in another module or to an external library.
Everything works fine in my tests and in the sandbox via runIde.
I also managed to reproduce the error by creating a new project with just an action and a module with a class and an empty method.
root gradle:
buildscript {
ext.kotlin_version = '1.2.31'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
plugins {
id 'org.jetbrains.intellij' version '0.3.12'
}
group 'test'
version '1.0-SNAPSHOT'
apply plugin: 'kotlin'
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
compile project(':testmodule')
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
intellij {
version '2018.1.6'
}
patchPluginXml {
changeNotes """
Add change notes here.
most HTML tags may be used"""
}
action:
package action
import com.intellij.openapi.actionSystem.*
import packages.OtherModuleClass
class TestAction : AnAction() {
override fun actionPerformed(e: AnActionEvent?) {
OtherModuleClass().otherModuleMethod()
}
}
other module class:
package packages
class OtherModuleClass {
fun otherModuleMethod() {}
}
I found out what my problem was.
I was installing in my IDE the jar in build/libs instead of the zip in build/distributions.
If someone else is looking for a solution for similar problem, I fixed it by adding the java plugin in build.gradle
apply plugin "java"