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.
Related
I am trying to add open api documentation to springboot base REST api project that I have.
It works fine when run locally inside intelliJ IDE or with gradlew run/bootRun
But when the project is packaged as a fat jar using gradlew shadow plugin 'com.github.johnrengelman.shadow' and run on command line as java -jar build/libs/Application-0.0.1-SNAPSHOT-all.jar it fails with ApplicationContextException
Exception stacktrace:
Caused by: org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getWebServerFactory(ServletWebServerApplicationContext.java:205) ~[Application-0.0.1-SNAPSHOT-all.jar:?]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:177) ~[Application-0.0.1-SNAPSHOT-all.jar:?]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:158) ~[Application-0.0.1-SNAPSHOT-all.jar:?]
... 9 more
build.gradle
plugins {
id 'org.springframework.boot' version '2.3.4.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'application'
id 'com.github.johnrengelman.shadow' version "6.0.0"
}
application {
mainClassName = 'com.example.Application'
executableDir = 'lib/'
}
dependencies {
implementation 'org.springdoc:springdoc-openapi-ui:1.5.12'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation files( "lib/$JarV2" )
compileOnly 'org.projectlombok:lombok:1.18.22'
annotationProcessor 'org.projectlombok:lombok:1.18.22'
implementation 'org.slf4j:slf4j-api:1.7.32'
implementation 'org.springframework.boot:spring-boot-starter-validation:2.3.3.RELEASE'
implementation 'org.springframework.boot:spring-boot-starter-log4j2:2.4.2'
implementation 'org.apache.logging.log4j:log4j-slf4j-impl:2.14.0'
implementation 'org.apache.logging.log4j:log4j-api:2.14.0'
implementation 'org.apache.logging.log4j:log4j-core:2.14.0'
implementation 'org.slf4j:jul-to-slf4j:1.7.30'
implementation 'commons-lang:commons-lang:2.6'
implementation "com.moandjiezana.toml:toml4j:0.7.2"
implementation "io.jsonwebtoken:jjwt-api:$jwtVersion"
runtimeOnly "io.jsonwebtoken:jjwt-impl:$jwtVersion"
runtimeOnly "io.jsonwebtoken:jjwt-jackson:$jwtVersion"
implementation 'com.auth0:java-jwt:3.18.1'
implementation 'com.auth0:jwks-rsa:0.19.0'
testImplementation('org.springframework.boot:spring-boot-starter-test')
}
Without the openapi dependency
implementation 'org.springdoc:springdoc-openapi-ui:1.5.12'
standalone run java -jar build/libs/Application-0.0.1-SNAPSHOT-all.jar works absolutely fine.
Is there some dependency conflict between open api and springboot libs ?
Turns out to be known issue and can be solved by including following in build.gradle
import com.github.jengelman.gradle.plugins.shadow.transformers.*
shadowJar {
zip64 true
exclude 'META-INF/*.SF'
exclude 'META-INF/*.DSA'
exclude 'META-INF/*.RSA'
// Required for Spring
mergeServiceFiles()
transform(AppendingTransformer) { resource = 'reference.conf' }
transform(AppendingTransformer) { resource = 'META-INF/spring.handlers' }
transform(AppendingTransformer) { resource = 'META-INF/spring.schemas' }
transform(AppendingTransformer) { resource = 'META-INF/spring.tooling' }
transform(PropertiesFileTransformer) {
paths = ['META-INF/spring.factories' ]
mergeStrategy = "append"
}
}
source: https://github.com/spring-projects/spring-boot/issues/1828
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
I am trying to build simple hello world spring-boot project with gradle and groovy.
When I run 'gradle clean build', I am getting below error.
So far I tried,
Even though it is a gradle project, I updated the maven version to latest(3.6.3). Just to be a safer side.
I re-installed my intelliJ IDE
I added maven url as "https:" in build.gradle
After above steps, still I am getting below error. I am not sure what else I am missing.
Though I have given 'https:' url in gradle build file, I am not sure from where it is taking the non-secure url(http).
Task :compileJava FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':compileJava'.
> Could not resolve all dependencies for configuration ':detachedConfiguration1'.
> Could not resolve org.springframework.boot:spring-boot-dependencies:2.1.1.RELEASE.
Required by:
project :
> Could not resolve org.springframework.boot:spring-boot-dependencies:2.1.1.RELEASE.
> Could not get resource 'http://repo1.maven.org/maven2/org/springframework/boot/spring-boot-dependencies/2.1.1.RELEASE/spring-boot-dependencies-2.1.1.RELEASE.pom'.
> Could not HEAD 'http://repo1.maven.org/maven2/org/springframework/boot/spring-boot-dependencies/2.1.1.RELEASE/spring-boot-dependencies-2.1.1.RELEASE.pom'. Received status code 501 from server: HTTPS Required
And my gradle build file looks:
plugins {
id 'org.springframework.boot' version '2.3.1.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
id 'war'
}
group = 'com.own.test'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenLocal()
maven {
url = 'https://repo.maven.apache.org/maven2'
}
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
Finally, I figured out where to update the maven url to "https://*" for gradle projects.
$ vi ~/.gradle/init.gradle
then I updated the maven url to secure protocol ("https://...")
allprojects {
repositories {
mavenLocal()
// Any Organization/Company specific repo url goes here
maven {
url "https://repo1.maven.org/maven2"
}
}
}
After above steps, it worked for me and able to build the spring-boot application successfully.
Using start.spring.io, this is the content of the build.gradle generated there:
plugins {
id 'org.springframework.boot' version '2.3.1.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'groovy'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.codehaus.groovy:groovy'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
Note the major differences:
groovy is actually there as plugin and as dependency
the maven repo is secure. As the error message states, you have use
the https endpoint here
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
If I follow the instruction in the grpc-java readme and I use maven, the protobuf generated files appear in the target directory and are subsequently in the classpath for me to extend etc. However, when I use gradle, the generated classes appear in the build directory and are absent from the classpath. I'm fairly new to gradle so I'm not really sure why it's behaving so differently.
My build.gradle file
apply plugin: 'java'
apply plugin: 'com.google.protobuf'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.5'
}
}
group 'co.example'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile 'io.grpc:grpc-netty-shaded:1.15.1'
compile 'io.grpc:grpc-protobuf:1.15.1'
compile 'io.grpc:grpc-stub:1.15.1'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:3.5.1-1"
}
//noinspection GroovyAssignabilityCheck
plugins {
grpc {
artifact = 'io.grpc:protoc-gen-grpc-java:1.15.1'
}
}
generateProtoTasks {
all()*.plugins {
grpc {}
}
}
}
It looks like for gradle (if you want to use the generated stubs/server interfaces) in the project where your proto files live in (i.e. the project is not just for generating jars and publishing them) then you'll need to add generatedFilesBaseDir to your build.gradle file:
protobuf {
generatedFilesBaseDir = "$projectDir/src/main/java/generated"
...
}
Once you've done this, the stubs should be in your classpath.
public class SomeServer extends MyProtoClassGrpc.PDFExtractImplBase {}