In build.gradle I am building WAR for tomcat and wildfly by single script. Wildfly has provided dependency to "javax.mail:mail:1.4.7'. But tomcat is missing this jar. So I have always added this jar to ${CATALINA_HOME}/lib/ . Currently I am trying to migrate from both of them to Amazon AWS Elastic Beanstalk and I don't want to mess with ${CATALINA_HOME}/lib/. How to write universal gradle script for wildfly with:
dependencies {
....
providedCompile group: 'javax.mail', name: 'mail', version: '1.4.7'
providedCompile group: 'javax.activation', name: 'activation', version: '1.1.1'
...
}
and for tomcat with:
dependencies {
...
compile group: 'javax.mail', name: 'mail', version: '1.4.7'
compile group: 'javax.activation', name: 'activation', version: '1.1.1'
...
}
I am not an expert with gradle.
#RaGe solved my problem. Code below is an ultimate solution as "42" number.
configurations {
tomcatLibs
}
dependencies {
...
providedCompile group: 'javax.mail', name: 'mail', version: '1.4.7' //provided for wildfly
providedCompile group: 'javax.activation', name: 'activation', version: '1.1.1' //provided for wildfly
tomcatLibs group: 'javax.mail', name: 'mail', version: '1.4.7' //only for tomcat
tomcatLibs group: 'javax.activation', name: 'activation', version: '1.1.1' //only for tomcat
providedCompile group: 'javax.mail', name: 'javax.mail-api', version: '1.5.0'
....
}
//default war for wildfly
war {
....
}
task createTomcatWar(type: War, dependsOn: classes) {
archiveName = 'app-tomcat.war';
classpath configurations.tomcatLibs // adds a configuration to the WEB-INF/lib dir
}
....
Add an additional config to hold the dependency who's scope changes:
configurations {
optLibs
}
add the dependency to the config just created:
dependencies{
...
optLibs 'javax.mail:mail:1.4.7'
optLibs 'javax.activation:activation:1.1.1'
compile 'foo'
runtime 'bar'
...
providedCompile.extendsFrom(optLibs)
}
Now for a task that builds a war with optLibas as compile:\
task createTomcatWar(type: War, dependsOn: classes) {
baseName = 'app-wildfly'
destinationDir = file("$buildDir/dist")
classpath = configurations.optLibs //This should include optLibs jars in WEB-INF/lib
}
The standard war task builds a war without the optLibs included, so that can be your wildfly war, you don't need another explicit task. If you want your custom task to run automatically everytime you build, you can also add to the rootlevel of your build.gradle:
assemble.dependsOn createTomcatWar
Related
I have setup a gradle multi project in java. The build was successful but when I tried to run the JAR, a java.lang.NoClassDefFoundError was thrown.
Here is my project structure:
.gradle/
authserver/
build/
src/main/java/fr/evywell/robserver/auth/
Main.java
build/
common/
build/
src/main/java/fr/evywell/robserver/common/
gradle/
build.gradle
settings.gradle
And then my configuration:
settings.gradle (IN ROOT DIR)
rootProject.name = "robserver"
include 'authserver'
include 'common'
build.gradle (IN ROOT DIR)
subprojects {
apply plugin: 'java'
repositories {
mavenCentral()
}
}
build.gradle (IN common DIR)
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile group: 'io.netty', name: 'netty-all', version: '4.1.24.Final'
compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.16'
compile group: 'org.javassist', name: 'javassist', version: '3.25.0-GA'
compile group: 'com.jsoniter', name: 'jsoniter', version: '0.9.23'
}
build.gradle (IN authserver DIR)
apply plugin: 'application'
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile group: 'io.netty', name: 'netty-all', version: '4.1.24.Final'
compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.16'
compile group: 'org.javassist', name: 'javassist', version: '3.25.0-GA'
compile group: 'com.jsoniter', name: 'jsoniter', version: '0.9.23'
compile project (':common')
}
mainClassName = 'fr.evywell.robserver.auth.Main'
jar {
manifest {
attributes 'Main-Class': 'fr.evywell.robserver.auth.Main'
}
}
Error message:
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: fr/evywell/robserver/common/network/Server
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: fr.evywell.robserver.common.network.Server
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more
The error message says that it cant find a class in my common package.
I don't know why because I used compile project (':common') in my authserver gradle configuration
Here is the command: java -jar authserver/build/libs/authserver.jar
Thank you for your help and have a nice day !
For me worked next thing: in kotlin I haved spring boot plugin applied to all subprojects {} in root build.gradle
Instead of this I've removed
apply(plugin = "org.springframework.boot") from root build.gradle and applied this line for each subproject instead of common module
I think the issue is that the JAR you are creating does not include the class files generated from compiling the common module. Try ammending the jar task of authserver module so that you include the output of compiling the common module:
jar {
from project.sourceSets.main.allSource
from project(":common").sourceSets.main.java.output
manifest {
attributes 'Main-Class': 'fr.evywell.robserver.auth.Main'
}
}
I used the shadow plugin to build a "fat jar". I don't know if it's a good practice...
Thank you for your help !
// authserver/build.gradle
plugins {
id 'com.github.johnrengelman.shadow' version '5.1.0'
id 'java'
}
apply plugin: 'application'
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile group: 'io.netty', name: 'netty-all', version: '4.1.24.Final'
compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.16'
compile group: 'org.javassist', name: 'javassist', version: '3.25.0-GA'
compile group: 'com.jsoniter', name: 'jsoniter', version: '0.9.23'
compile project (':common')
}
mainClassName = 'fr.evywell.robserver.auth.Main'
jar {
manifest {
attributes 'Main-Class': 'fr.evywell.robserver.auth.Main'
}
}
I'm facing this error while running the project. I'm unable to know the cause of the error and unable to find a solution online as well.
This project is running over another laptop without any error but when I extracted the zip and tried to run it, found it throwing error.
Please help me figure this out. Thanks
Error
Execution failed for task ':Application.main()'.
Process 'command '/home/jamshaid/Documents/idea-IC-192.5728.98/jbr/bin/java'' finished with non-zero exit value 1
StackTrace
2019-08-11 09:57:06,589 2269 [main] INFO com.techno.homes.Application - No active profile set, falling back to default profiles: default
2019-08-11 09:57:06,628 2308 [main] ERROR o.s.boot.SpringApplication - Application run failed
java.lang.AbstractMethodError: Receiver class org.springframework.cloud.bootstrap.BootstrapApplicationListener$CloseContextOnFailureApplicationListener does not define or inherit an implementation of the resolved method abstract getOrder()I of interface org.springframework.core.Ordered.
at org.springframework.core.OrderComparator.findOrder(OrderComparator.java:142)
at org.springframework.core.annotation.AnnotationAwareOrderComparator.findOrder(AnnotationAwareOrderComparator.java:65)
at org.springframework.core.OrderComparator.getOrder(OrderComparator.java:125)
at org.springframework.core.OrderComparator.getOrder(OrderComparator.java:113)
at org.springframework.core.OrderComparator.doCompare(OrderComparator.java:82)
at org.springframework.core.OrderComparator.compare(OrderComparator.java:68)
at java.base/java.util.TimSort.countRunAndMakeAscending(TimSort.java:360)
at java.base/java.util.TimSort.sort(TimSort.java:220)
at java.base/java.util.Arrays.sort(Arrays.java:1515)
at java.base/java.util.ArrayList.sort(ArrayList.java:1749)
at org.springframework.boot.SpringApplication.asUnmodifiableOrderedSet(SpringApplication.java:1325)
at org.springframework.boot.SpringApplication.getListeners(SpringApplication.java:1234)
at org.springframework.boot.context.event.EventPublishingRunListener.contextLoaded(EventPublishingRunListener.java:85)
at org.springframework.boot.SpringApplicationRunListeners.contextLoaded(SpringApplicationRunListeners.java:66)
at org.springframework.boot.SpringApplication.prepareContext(SpringApplication.java:394)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:328)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1258)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246)
at com.techno.homes.Application.main(Application.java:24)
Gradle File
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'org.springframework.boot'
apply plugin: 'application'
apply plugin: 'docker'
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'}
buildscript {
project.ext {
springBootVersion = '2.0.4.RELEASE'
jarName = 'recipe'
versionName = '1.0.0'
gradleDockerVersion = '1.2'
swagger2version = '2.9.2'
}
repositories {
jcenter()
maven { url "https://repo.maven.apache.org/maven2" }
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle- plugin:${project.springBootVersion}"
classpath "se.transmode.gradle:gradle-docker:${project.gradleDockerVersion}"
}
}
task createWrapper(type: Wrapper) {
gradleVersion = '4.4.1'
}
// Used by the Docker gradle plugin, group refers to the account under which the docker image is created
group = 'com.techno.homes'
mainClassName = 'com.techno.homes.Application'
sourceCompatibility = 11
targetCompatibility = 11
repositories {
mavenCentral()
jcenter()
}
ext {
springCloudVersion = 'Greenwich.SR2'
}
dependencies {
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: '2.0.4.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator', version: '2.0.4.RELEASE'
compile group: 'org.hibernate.validator', name: 'hibernate-validator', version: '6.0.16.Final'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web-services', version: '2.0.4.RELEASE'
compile group: 'ma.glasnost.orika', name: 'orika-core', version: '1.4.6'
compile group: 'org.hamcrest', name: 'hamcrest-library', version: '1.3'
compile group: 'junit', name: 'junit', version: '4.12'
compile group: 'org.springframework.boot', name: 'spring-boot-test-autoconfigure', version: '2.0.4.RELEASE'
compile group: 'org.springframework', name: 'spring-test', version: '5.1.8.RELEASE'
compile(group: 'org.hibernate', name: 'hibernate-entitymanager', version: '5.4.3.Final')
compile(group: 'org.hibernate', name: 'hibernate-core', version: '5.2.17.Final')
compile group: 'org.springframework.boot', name: 'spring-boot-test'
compile group: 'org.mockito', name: 'mockito-core', version: '2.15.0'
compile group: 'info.cukes', name: 'cucumber-java', version: '1.2.4'
compile group: 'info.cukes', name: 'cucumber-core', version: '1.2.4'
compile group: 'info.cukes', name: 'cucumber-junit', version: '1.2.4'
compile group: 'com.google.code.findbugs', name: 'jsr305', version: '2.0.1'
compile group: 'com.spotify', name: 'docker-maven-plugin', version: '1.2.0'
compile group: 'org.projectlombok', name: 'lombok', version: '1.18.8'
runtime group: 'mysql', name: 'mysql-connector-java', version: '5.1.46'
testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test'
testCompile group: 'com.github.tomakehurst', name: 'wiremock', version: '1.58'
testCompile group: 'info.cukes', name: 'cucumber-spring', version: '1.2.5'
// Basic Spring boot with config client
// compile('org.springframework.cloud:spring-cloud-starter-config')
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-actuator")
// Spring OAuth2 security
compile("org.springframework.boot:spring-boot-starter-security")
compile("org.springframework.security.oauth:spring-security-oauth2")
// compile("org.springframework.security:spring-security-jwt")
compile group: 'org.springframework.security', name: 'spring-security-jwt', version: '1.0.10.RELEASE'
// Eureka client
// compile('org.springframework.cloud:spring-cloud-starter-eureka')
compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-netflix-eureka-client', version: '2.0.0.RELEASE'
// Zipkin tracing
//compile('org.springframework.cloud:spring-cloud-starter-zipkin')
compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-zipkin', version: '2.1.2.RELEASE'
// Swagger for API testing
compile("io.springfox:springfox-swagger2:${swagger2version}")
compile("io.springfox:springfox-swagger-ui:${swagger2version}")
compile group: 'org.springframework.security', name: 'spring-security-core', version: '5.1.5.RELEASE'
compile group: 'org.springframework.security', name: 'spring-security-config', version: '5.1.5.RELEASE'
compile group: 'org.springframework.security', name: 'spring-security-web', version: '5.1.5.RELEASE'
compile group: 'org.springframework.security.oauth', name: 'spring-security-oauth2', version: '2.3.6.RELEASE'
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.5'
//camel
compile group: 'org.apache.camel', name: 'camel-spring-boot', version: '2.15.1'
compile group: 'org.apache.camel', name: 'camel-pulsar', version: '2.24.1'
compile group: 'org.apache.camel', name: 'camel-core', version: '2.24.1'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.9.3'
}
jar {
baseName = "${project.jarName}"
version = "${project.versionName}"
enabled = true
manifest {
attributes 'Main-Class': 'com.techno.homes.Application'
}
}
Allication
#Configuration
#EnableJpaAuditing
#EnableJpaRepositories("com.techno.homes.repositories")
#SpringBootApplication
#EnableEurekaClient
#EnableResourceServer
#EnableSwagger2
#EnableOAuth2Client
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Your Spring Boot version is too old for your Spring Cloud version:
https://github.com/spring-cloud/spring-cloud-commons/issues/552
Spring Cloud Greenwich (2.1.x) is not compatible with spring boot 2.0.x and spring framework 5.0.x. Either update spring boot or downgrade spring cloud to Finchley.
So please update Spring Boot to 2.1.x
PS: searching on 'BootstrapApplicationListener$CloseContextOnFailureApplicationListener' would have lead you there straight away :-)
Ok - did some testing here.
Created empty SpringBoot application from start.spring.io.
Imported the project into IntelliJ.
I then loaded your gradle file contents. Also started by removing all the # imports in the main (Application) class - just kept #SpringBootApplication.
Removed all the other classes (including tests).
Had same error.
Started by removing all the gradle dependencies for Swagger and Hibernate, etc. Those are not required to get a bare-bones SpringBoot app running.
Removing this line solved to error (not throwing the error anymore):
compile("org.springframework.security.oauth:spring-security-oauth2")
So something needs to be configured if you want to do Spring OAuth2 ... for you to find.
Suggest you add small building blocks at a time - you have a lot of stuff in the SpringBoot main app annotated. Spring will auto-configure defaults for all it can, but you might run into similar problems.
Add one annotation - write unit test class that will verify basic functionality of that specific annotation. Then enable the next one, write unit test.... unit all are working with basic test coverage.
This is because of spring and cloud version not compatible.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath />
</parent>
The version of the parent should be an upgrade one or the same as cloud version.
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-oauth2</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
I have created a relatively new project with 4 modules inside one project (created with intelij). However, I have now got to the stage where I want to import one module (:domain) from another (app-updater). The issue I'm facing is when I do this from within intelij it gives me errors in the "Build: Sync" tab. This is strange since my module app-updater is able to reference to module domain when I add the line "compile project(':domain')" into build.gradle and when I remove the line I get issues with the class being missing so the build: sync seems to be working it's just running it from inside intelij seems to give an error saying "Project with path ':domain' could not be found in root project 'app-updater'." I have attached my gradle config below, hopefully I have given enough info but if not feel free to ask for more, thanks for any help.
Main project settings.gradle file
rootProject.name = 'rGuide'
include 'domain'
include 'service'
include 'app-updater'
include 'app-rest'
:domain build.gradle
group 'domain'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
// https://mvnrepository.com
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.0.1'
compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jsr310', version: '2.9.9'
compile group: 'commons-lang', name: 'commons-lang', version: '2.6'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
app-updater build.gradle file
group 'app-updater'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
mavenCentral()
google()
maven {
url 'https://repo.spring.io/plugins-release/'
}
maven {
url 'https://repo.spring.io/milestone'
}
}
dependencies {
compile project(':domain')
// https://mvnrepository.com
compile group: 'net.ser1', name: 'gozirra-client', version: '0.4.1'
compile group: 'io.netty', name: 'netty-all', version: '4.1.36.Final'
compile group: 'io.projectreactor.netty', name: 'reactor-netty', version: '0.9.0.M2'
compile group: 'org.springframework.integration', name: 'spring-integration-stomp', version: '5.1.6.RELEASE'
compile group: 'org.springframework.integration', name: 'spring-integration-core', version: '5.1.6.RELEASE'
compile group: 'org.springframework', name: 'spring-context', version: '5.1.6.RELEASE'
compile group: 'org.springframework', name: 'spring-web', version: '5.1.6.RELEASE'
compile group: 'org.springframework', name: 'spring-websocket', version: '5.1.6.RELEASE'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.9.9'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
TLDR - Trying to import domain into app-updater within intelij, it "works fine" and seems to import domain however when I do this with the Build: sync tab in intelij it gives an error saying "Project with path ':domain' could not be found in root project 'app-updater'.". Thanks for any help!!
My byild.gradle file
Able to export jar with JAVA 8. when I configure to JAVA 11 exported jar doesn't contain external jars
// Apply the java-library plugin to add support for Java Library
apply plugin: 'java-library'
// In this section you declare where to find the dependencies of your project
repositories {
jcenter()
}
configurations
{
all*.exclude group: 'org.slf4j', module: 'slf4j-log4j12' //by both name and group
}
dependencies {
// This dependency is exported to consumers, that is to say found on their compile classpath.
api 'org.apache.commons:commons-math3:3.6.1'
// This dependency is used internally, and not exposed to consumers on their own compile classpath.
implementation 'com.google.guava:guava:21.0'
// Use JUnit test framework
testImplementation 'junit:junit:4.12'
compile group: 'com.microsoft', name: 'sqljdbc4', version: '3.0'
compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.15'
compile group: 'com.ibatis', name: 'ibatis2-common', version: '2.1.7.597'
compile group: 'org.apache.commons', name: 'commons-io', version: '1.3.2'
compile group: 'org.yaml', name: 'snakeyaml', version: '1.21'
compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1'
compile group: 'org.json', name: 'json', version: '20171018'
compile group: 'com.itextpdf', name: 'itextpdf', version: '5.5.13'
compile group: 'com.itextpdf.tool', name: 'xmlworker', version: '5.5.13'
testCompile group: 'org.slf4j', name: 'slf4j-simple', version: '1.6.2'
}
add this below lines into your build.gradle
jar { manifest {attributes "Main-Class": "your main class" } from {configurations.compile.collect { it.isDirectory()? it : zipTree(it) } }
then do a clean/build,
the jar created in \build\libs folder is your runnable jar
I try to run kotlin spring webapp via heroku run bash. I run then:
java -cp ./build/libs/pss_kotlin-1.0-SNAPSHOT.jar pl.makzyt.pss_kotlin.MainKt
Then I get this error:
Exception in thread "main" java.lang.NoClassDefFoundError: kotlin/jvm/internal/Intrinsics
at pl.makzyt.pss_kotlin.MainKt.main(Main.kt)
Caused by: java.lang.ClassNotFoundException: kotlin.jvm.internal.Intrinsics
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
I know I could run this if I get kotlin-runtime.jar. Is there possibility to obtain it with some command or should I download it manually?
EDIT
Here are my build.gradle dependencies:
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
compile group: 'org.springframework.boot',
name: 'spring-boot-autoconfigure',
version: '2.0.0.RELEASE'
compile group: 'org.springframework.boot',
name: 'spring-boot-starter-data-jpa',
version: '2.0.0.RELEASE'
compile group: 'org.springframework.boot',
name: 'spring-boot-starter-web',
version: '2.0.0.RELEASE'
compile group: 'org.postgresql',
name: 'postgresql',
version: '42.2.2'
compile group: 'org.hibernate',
name: 'hibernate-core',
version: '5.2.12.Final'
compile group: 'org.hibernate',
name: 'hibernate-entitymanager',
version: '5.2.12.Final'
compile group: 'javax.xml.bind',
name: 'jaxb-api',
version: '2.1'
compile group: 'org.springframework.boot',
name: 'spring-boot-starter-thymeleaf',
version: '2.0.0.RELEASE'
compile group: 'nz.net.ultraq.thymeleaf',
name: 'thymeleaf-layout-dialect',
version: '2.3.0'
}
If your spring boot app is configured correctly (spring boot plugin is applied in build.gradle) you just need to run the jar.
java -jar yourjar.jar
You don’t need to use the -cp option because Spring Boot repackages all dependencies in the jar that’s why you get a class not found error.
I'm not sure why you're getting this error. But you might want to try generating a new Spring + Kotlin app from https://start.spring.io and copying the Maven dependencies/plugins into your pom.xml.