How to use Kotlin with a Bukkit/Spigot plugin and Gradle - java

I am trying to use Kotlin for a Spigot plugin (for version 1.8.8), as I find Kotlin a lot more efficient to use. But, whenever I try and run the plugin, I get this error:
Caused by: java.lang.ClassNotFoundException: kotlin.jvm.internal.Intrinsics
I have looked online, and it seems I need to shade the Kotlin jar file into my plugin, could anyone advise me how I would do that?
UPDATE After trying what was suggested below I still get the same error.
Here is my build.gradle:
plugins {
id 'java'
id 'org.jetbrains.kotlin.jvm' version '1.3.31'
id 'com.github.johnrengelman.shadow' version '4.0.2'
}
group 'me.graphicalcake95'
version '1.0.0'
sourceCompatibility = 1.8
repositories {
mavenCentral()
maven {
url 'https://hub.spigotmc.org/nexus/content/repositories/snapshots/'
}
}
shadowJar {
baseName = 'shadow'
classifier = null
version = null
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compileOnly 'org.bukkit:bukkit:1.8.8-R0.1-SNAPSHOT'
shadow "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
}
compileKotlin {
kotlinOptions {
jvmTarget = "1.8"
}
}
compileTestKotlin {
kotlinOptions {
jvmTarget = "1.8"
}
}

You indeed need to Shadow the Kotlin standard library into your plugin. Since you are using Gradle it can do this by using the Shadow Plugin which is similar to Shade but Shade is only for Maven so it won't be useful for you.
Using the Shadow plugin is straightforward. First you apply the plugin itself:
plugins {
id 'com.github.johnrengelman.shadow' version '5.0.0'
}
then you can configure Shadow:
shadowJar {
baseName = 'shadow'
classifier = null
version = null
}
This will create a shadow.jar in your build folder when you build the project. After applying the shadow plugin you can shadow dependencies to be included in the fat jar:
dependencies {
shadow "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
}

Install this plugin on your spigot server
download the jar and place it in the plugins folder,
then follow the documentation

Related

IntelliJ: Unable to use class from jar dependency

I am trying to test setting up a minimal project, building and exporting it as a jar, and using that project as a dependency in another. I am using Spring for this. I have two projects, makeajar and useajar. In makeajar, I set up a simple project with a Java class called Dinner. I want to import the jar for this project as part of the library in useajar, and from useajar instantiate a Dinner object.
However, the issue I'm getting is that I can't import the Dinner class. When I do, the import shows red:
Is there something I'm doing wrong in this process?
Things I've already tried:
Invalidating caches and restarting
Creating a fresh project
Adding makeajar as a module dependency (note: This was already there, and also appeared in the Project Structure -> Libraries)
How I produce the jar file from makeajar:
Go to project root directory and run ./gradlew build
Things I'm noticing:
Although makeajar appears in my module dependencies and libraries, it does not appear in the "External Libraries" folder in the useajar project.
I can gradle refresh fine, and if I comment out the attempts to import the Dinner object, I build.
I can easily get various popular dependencies (i.e. Jackson, guava) from MavenCentral and use these right out of the box. The issue is only occurring with my makeajar dependency.
makeajar build.gradle:
plugins {
id 'org.springframework.boot' version '2.2.12.BUILD-SNAPSHOT'
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
id 'java'
}
group = 'com.makingajar'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenCentral()
maven { url 'https://repo.spring.io/milestone' }
maven { url 'https://repo.spring.io/snapshot' }
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
useajar build.gradle:
plugins {
id 'org.springframework.boot' version '2.2.12.BUILD-SNAPSHOT'
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
id 'java'
id 'idea'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
apply plugin: 'idea'
repositories {
mavenCentral()
maven { url 'https://repo.spring.io/milestone' }
maven { url 'https://repo.spring.io/snapshot' }
mavenLocal {
flatDir {
dirs projectDir.toString() + '/libs'
}
}
}
allprojects {
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
implementation 'com.makingajar:makeajar2:0.0.1-SNAPSHOT'
implementation 'com.fasterxml.jackson.core:jackson-core:2.11.3'
}
}
test {
useJUnitPlatform()
}
IntelliJ version 2020.1.4
Gradle version 6.6.1

Jar with dependencies spring boot gradle

I was trying to make a jar with dependencies because I was getting a NoClassDefFoundError when starting the jar with java -Dspring.config.location=myProperties -jar myJar, after a lot of searching I found that I can achieve this using the following solution in the jar block:
from{
configurations.runtimeClasspath.collect {it.isDirectory() ? it : zipTree(it)}
}
And all good with this except for the amount of time when building the jar (1 minute aprox), and according with this answer: Gradle: Build 'fat jar' with Spring Boot Dependencies I don't need to create an additional task, is enough with the bootRepackage but I'm getting the error that I mentioned above with bootRepackage and I don't understand why.
This is the content of my build.gradle and I'm using spring boot 1.5.15:
/*
* This file was generated by the Gradle 'init' task.
*/
buildscript {
ext.springBootVersion = '1.5.15.RELEASE'
ext.managementVersion = '1.0.6.RELEASE'
ext.axis2Version = '1.7.9'
repositories {
mavenCentral()
mavenLocal()
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
//classpath "io.spring.gradle:dependency-management-plugin:${managementVersion}"
//classpath "com.intershop.gradle.wsdl:wsdl-gradle-plugin:2.0.1"
}
}
plugins {
id 'java'
id 'maven'
id 'org.springframework.boot' version '1.5.15.RELEASE'
id 'io.spring.dependency-management' version '1.0.6.RELEASE'
}
configurations{
implementation{
exclude group: 'javax.servlet', module: 'servlet-api'
}
}
dependencies {
implementation "org.springframework.boot:spring-boot-starter:${springBootVersion}"
implementation 'org.springframework.integration:spring-integration-mongodb'
implementation 'org.springframework.integration:spring-integration-amqp'
testImplementation "org.springframework.boot:spring-boot-starter-test:${springBootVersion}"
testImplementation 'org.mockito:mockito-core:2.6.1'
implementation 'org.apache.ws.commons.axiom:axiom-jaxb:1.2.20'
implementation('org.apache.axis2:axis2-kernel:1.7.9'){
exclude group: 'javax.servlet', module: 'servlet-api'
//The exclude above don't work
}
implementation "org.apache.axis2:axis2-kernel:${axis2Version}"
implementation "org.apache.axis2:axis2-wsdl2code-maven-plugin:${axis2Version}"
implementation "org.apache.axis2:axis2-transport-http:${axis2Version}"
}
wsdl {
axis2 {
genNameAxis2 {
//someAxis2Tasks
}
}
}
wsdl2java {
//someWsdlTasks
}
wsdl2javaExt {
cxfVersion = "3.2.1"
}
jar {
manifest{
attributes ('Main-Class': 'dummy.Application')
}
from{
configurations.runtimeClasspath.collect {it.isDirectory() ? it : zipTree(it)}
}
archiveBaseName = 'projectName'
archiveVersion = '1.0.0'
}
bootRepackage{
mainClass = 'dummy.Application'
//classifier = 'boot' I'm getting an error with this argument
}
repositories {
mavenLocal()
}
group = 'dummy.group'
version = '1.0.0-SNAPSHOT'
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
Thank you in advance.
I have this same problem after upgrading to Gradle 5 and using 'implementation' instead of 'compile' for my dependencies.
Gradle built a main jar with no sub-project jars or dependencies (no BOOT-INF/lib directory at all). Changing 'implementation' back to 'compile' in the parent project only fixed the problem (with no other changes).
So, apparently, the Spring Boot 1.5.9 Gradle plugin does not work with the new implementation configuration. Note that Spring Boot 2 and the new bootJar task work fine, this issue is only with the old bootRepackage and the new implementation configuration.

IntelliJ Plugin using Gradle gives java.lang.NoClassDefFoundError

I'm trying a vanilla IntelliJ plugin which uses a shared module.
It seems the plugin build has found my src directory, but does not include the classes of my shared module. How do I tell gradle to include my shared library classes in the built plugin?
I tried adding a compile dependency, but no java.lang.NoClassDefFoundError when running in the IDE.
Gradle build file:
plugins {
id 'java'
id 'org.jetbrains.intellij' version '0.3.12'
}
group 'com.prosc'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile files('out/production/MySharedLibrary')
}
intellij {
version '2018.2.3'
}
the documentation for org.jetbrains.intellij hints for a pluginName:
intellij {
version 'IC-2018.2.3'
plugins = ['coverage', 'org.intellij.plugins.markdown:8.5.0.20160208']
pluginName 'MySharedLibrary'
}
build.gradle.kts is similar:
val sdkVersion = System.getProperty("idea.version", "IC-2018.2.3")
val pluginName = "MySharedLibrary"
configure<IntelliJPluginExtension> {
version = sdkVersion
pluginName = pluginName
setPlugins(
"android",
"CheckStyle-IDEA:5.3.1",
"copyright",
"CSS",
"gradle",
"Groovy",
"JavaScriptLanguage",
"junit",
"properties",
"uml"
)
}
Don't use Gradle. Use IntelliJ modules instead.

Is there any way of making IntelliJ IDEA recognizing Dagger 2 generated classes in a Java project?

Context
I have started a personal project in java with Gradle as the build system and I want to use Dagger 2 as a DI. The main reason of doing that is to get used to that library and be able to use it easily in bigger projects.
What have I tried
I've managed to make the Google sample runs on IntelliJ IDEA
Problem
IntelliJ IDEA keeps telling me that it cannot resolve the generated class (in this case DaggerCoffeeApp_Coffee). It's a bit annoying not to know if the written code is correct (specially when you are learning to use Dagger 2).
All java classes are the same as the Google sample. Here is my build.gradle file:
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile 'com.google.dagger:dagger:2.0.1'
compile 'com.google.dagger:dagger-compiler:2.0.1'
}
Question
Is there any way to make IntelliJ IDEA recognize DaggerCoffeeApp_Coffee as a generated class (and so make it possible to go to its implementation by `ctrl + left click)?
Simplest way I found:
Add idea plugin and add Dagger2 dependency like below:
plugins {
id "net.ltgt.apt" version "0.10"
}
apply plugin: 'java'
apply plugin: 'idea'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile 'com.google.dagger:dagger:2.11'
apt 'com.google.dagger:dagger-compiler:2.11'
}
Turn on Annotation Processing for IntelliJ: Go to Settings and search for Annotation Processors, check Enable annotation processing like below image:
Finally I made it!
I had to add the apt and the idea plugin so right now my build.gradle file look like this:
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "net.ltgt.gradle:gradle-apt-plugin:0.4"
}
}
apply plugin: "net.ltgt.apt"
apply plugin: 'java'
apply plugin: 'idea'
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile 'com.google.dagger:dagger:2.0.1'
apt 'com.google.dagger:dagger-compiler:2.0.1'
}
you must manually enable the annotation processing in IntelliJ.
From: Settings --> Build, Execution, Deployment --> Compiler --> Annotation Processors --> Enable annotation processing and Obtain processors from project classpath
then rebuild the project and you will find the generated classes in the project.
Please note that I have used this solution in a (java) android project.
I'm using version 2017.3.3 of IntelliJ IDEA, version 0.14 of the net.ltgt.apt plugin and version 2.14.1 of Dagger and as well as applying the idea plugin in the build.gradle file (as in Pelocho's answer) I found I also had to tell IntelliJ where it can find the sources generated by Dagger, as follows:
apply plugin: 'idea'
idea {
module {
sourceDirs += file("$buildDir/generated/source/apt/main")
testSourceDirs += file("$buildDir/generated/source/apt/test")
}
}
This is what I had to do in order to get Idea to work with Dagger2 and gradle.
Turn on annotation processing as shown in the answers above.
Add the following to the build.gradle file in order for Idea to see the generated classes as sources.
sourceDirs += file("$projectDir/out/production/classes/generated/")
Here's the full listing of my build.gradle
plugins {
id 'java'
id 'idea'
id "net.ltgt.apt" version "0.10"
}
idea {
module {
sourceDirs += file("$projectDir/out/production/classes/generated/")
}
}
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.dagger:dagger:2.16'
apt 'com.google.dagger:dagger-compiler:2.16'
}
sourceCompatibility = 1.8
Also, I had to add the following gradle task (to my build.gradle file) to clear out my out directory. When I moved some files around and Dagger2 regenerated the source files, the out directory wasn't being cleared out :(. I also included this task in my run configuration, so that it gets triggered before I rebuild my project.
task clearOutFolder(type: Delete) {
delete 'out'
}
Here's the solution that worked for me:
File -> Project Structure -> (select your project under list of modules) -> Open 'Dependencies' tab
Then, click on green '+' sign, select 'JARs or directory' and select 'build/classes/main' folder.
Another solution would be to link folder with build class files using 'dependencies' block inside build.gradle:
https://stackoverflow.com/a/22769015/5761849
Using IntelliJ IDEA 2019.1 and Gradle 5.4.1, this seems to be enough:
plugins {
id 'java'
}
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testImplementation group: 'junit', name: 'junit', version: '4.12'
implementation 'com.google.dagger:dagger:2.23.1'
annotationProcessor 'com.google.dagger:dagger-compiler:2.23.1'
}
I don't know the minimal versions for which this solution works, though.
I had a similar problem, I could not find out the cause for a long time.
Just launched and the result surprised me.
Intellij Idea 2018.3.6 -
build.gradle:
plugins {
id "java"
}
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile 'com.google.dagger:dagger:2.11'
apt 'com.google.dagger:dagger-compiler:2.11'
}
The following worked for me on IntelliJ 2021.3.3 (UE)
plugins {
id 'java'
id 'idea'
id("com.github.johnrengelman.shadow") version "7.1.2"
}
idea {
module {
sourceDirs += file("$projectDir/build/generated/sources/annotationProcessor/java/main")
testSourceDirs += file("$projectDir/build/generated/sources/annotationProcessor/java/test")
}
}
group 'com.codigomorsa'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
annotationProcessor 'com.google.dagger:dagger-compiler:2.44'
implementation 'com.google.code.gson:gson:2.9.1'
implementation 'com.google.dagger:dagger:2.44'
testAnnotationProcessor 'com.google.dagger:dagger-compiler:2.44'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.0'
}
test {
useJUnitPlatform()
}

Gradle with IntelliJ not loading PostgreSQL JDBC as a dependency

I am trying to setup IntelliJ alongwith Gradle and JOOQ for my next project. As of now, this is how my Gradle file looks like:
apply plugin: 'java'
apply plugin: 'jooq'
sourceCompatibility = 1.5
version = '1.0'
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
}
dependencies {
compile 'org.jooq:jooq:3.1.0'
compile 'com.google.guava:guava:14.0'
compile 'postgresql:postgresql:9.1-901-1.jdbc4'
}
buildscript {
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
classpath 'postgresql:postgresql:9.1-901-1.jdbc4'
classpath 'com.github.ben-manes:gradle-jooq-plugin:0.5'
}
}
jooq {
... snip ...
}
And this is how my external dependencies (in IntelliJ) show up:
.
Somehow, Gradle is downloading and IntelliJ is recognizing the jooq and guava as part of my dependencies, but postgresql does not show up. So, while doing this works (using Guava, a dependency loaded from Gradle):
List<String> stringList = Lists.newArrayList();
This fails with a ClassNotFoundException:
Class.forName("org.postgresql.Driver").newInstance();
While doing a ./gradlew build, I have seen gradle output the fact that it did download thr postgresql-9.1-901 jar from Maven Central, but I don't know where it keeps it. Any help is greatly appreciated.
Apparently, I really need to RTFM. I hadn't refreshed the dependencies from the Gradle tool window in IntelliJ after making changes to the Gradle script. Got it from here: https://www.jetbrains.com/idea/webhelp/synchronizing-changes-in-gradle-project-and-intellij-idea-project.html

Categories

Resources