Exclude Junit from War; Keep for Unit Tests - java

I have a Java project that builds a war. junit-4.13.2.jar is being included in the war as a transitive dependency. I'd like to exclude the jar from the war, but I still need to have the jar for running tests.
My build.gradle looks like:
dependencies {
...
testCompile group: 'junit', name: 'junit'
...
}
configurations.all {
exclude group: "org.apache.logging.log4j", module: "log4j-core"
}
Using this config, I can still run my tests, but the jar still ends up in the war.

As you already state that its because of transitive dependency from a compile/runtime dependency - the fix would be to exclude junit from the same
implementation ('g:a:v') {
exclude group: 'junit', module: 'junit'
}

Related

NoClassDefFoundError: org/bouncycastle/jce/provider/BouncyCastleProvider

I upgraded the plugin id 'org.springframework.boot' version '2.6.5' which led to the following changes
plugins {
id 'java'
id 'org.springframework.boot' version '2.6.5'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'jacoco'
id 'org.barfuin.gradle.jacocolog' version '1.0.1'
}
bootRun {
args = [
'--spring.config.location=./src/configs.properties',
]
}
springBoot {
mainClass = 'com.company.service.ServiceClass'
}
jar {
// This would avoid creating an additional "-plain" jar
enabled = true
archiveClassifier = ''
// Before the plugin upgrade I wasnt getting manifest attribute related error. Because of one of such errors, I added it the attribute explicitly.
manifest {
attributes 'Main-Class': 'com.company.service.ServiceClass'
}
}
bootJar {
layered {
enabled = true
}
}
dependencies {
testImplementation group: 'junit', name: 'junit', version: '4.12'
implementation 'org.bouncycastle:bcprov-jdk16:1.46'
implementation 'org.immutables:value:2.9.0'
implementation('org.apache.lucene:lucene-core:8.10.0')
implementation('com.fasterxml.jackson.core:jackson-core:2.13.2')
implementation('com.fasterxml.jackson.core:jackson-annotations:2.13.2')
implementation('com.fasterxml.jackson.core:jackson-databind:2.13.2')
implementation('org.apache.poi:poi-ooxml:5.2.2')
implementation('io.netty:netty-codec:4.1.75.Final')
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-cbor:2.13.2'
compileOnly 'org.projectlombok:lombok:1.18.22'
annotationProcessor 'org.projectlombok:lombok:1.18.22'
annotationProcessor 'org.immutables:value:2.9.0'
implementation('org.springframework.boot:spring-boot-starter-web:2.6.5') {
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
exclude group: 'org.slf4j', module: 'slf4j-simple'
exclude group: 'org.slf4j', module: 'slf4j-log4j12'
exclude group: 'org.slf4j', module: 'jcl-over-slf4j'
exclude group: 'ch.qos.logback', module: 'logback-classic'
}
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
exclude group: 'org.slf4j', module: 'slf4j-simple'
exclude group: 'org.slf4j', module: 'slf4j-log4j12'
exclude group: 'org.slf4j', module: 'jcl-over-slf4j'
exclude group: 'ch.qos.logback', module: 'logback-classic'
implementation('org.apache.tomcat.embed:tomcat-embed-core:9.0.54')
}
implementation('org.springframework.boot:spring-boot-starter-security:2.6.5') {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
exclude group: 'org.slf4j', module: 'slf4j-simple'
exclude group: 'org.slf4j', module: 'slf4j-log4j12'
exclude group: 'org.slf4j', module: 'jcl-over-slf4j'
exclude group: 'ch.qos.logback', module: 'logback-classic'
}
implementation('org.keycloak:keycloak-spring-boot-starter:17.0.1') {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
exclude group: 'org.slf4j', module: 'slf4j-simple'
exclude group: 'org.slf4j', module: 'slf4j-log4j12'
exclude group: 'org.slf4j', module: 'jcl-over-slf4j'
exclude group: 'ch.qos.logback', module: 'logback-classic'
}
implementation('org.keycloak.bom:keycloak-adapter-bom:17.0.1')
implementation('io.springfox:springfox-boot-starter:3.0.0')
implementation('io.springfox:springfox-swagger-ui:3.0.0')
}
// some jacoco and checkstyle configs here.
When I run the jar, I get the following error
>> java -jar build/libs/service-1.0-SNAPSHOT.jar
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/bouncycastle/jce/provider/BouncyCastleProvider
at java.lang.Class.getDeclaredMethods0(Native Method)
From the solutions I found on SO, I added the dependency
implementation group: 'org.bouncycastle', name: 'bcprov-jdk16', version: '1.46'
But this doesnt work. Plus I am still using jdk8 and there is no org.bouncycastle for jdk8.
Any suggestions how I can work around this exception?
EDIT:
Running the application with intelliJ doesnt throw this problem. But happens when running from command line.
EDIT:
>> % java -version
openjdk version "1.8.0_292"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_292-b10)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.292-b10, mixed mode)
Full stack trace
% java -jar build/libs/my-service-class.jar
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/bouncycastle/jce/provider/BouncyCastleProvider
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:650)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:632)
Caused by: java.lang.ClassNotFoundException: org.bouncycastle.jce.provider.BouncyCastleProvider
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
... 7 more
Running the application with intelliJ doesnt throw this problem. But
happens when running from command line.
java -jar build/libs/my-service-class.jar
For executing a jar directly from command line, it should contain manifest information. Assuming the aforementioned jar contain the same as you're facing the the other issue(class def not found) so looks like your jar my-service-class.jar is thin jar and doesn't contain the other dependencies that you've added in the project. Two way to solve this problem. Either you can create uber jar i.e jar with all the dependencies included by using available plugins for the same and then execute the command or just add all the required dependencies into some folder and give path to it while runing the above command.

Unable to start spring-boot in jetty and it always defaults to tomcat

Unable to start spring-boot in jetty even with the following configuration, what is not being configured properly
implementation ('org.springframework.boot:spring-boot-starter-web') {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat'
}
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-jetty'
You need to add below code in your build.gradle to exclude Tomcat.
configurations {
compile.exclude module: "spring-boot-starter-tomcat"
}
and then to add Jetty dependecy:
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-jetty'
And this will start your Spring Boot app with Jetty.

How to point gradle to local dependency that can have different paths?

To make it more clear, i have a code in my jar that when run it downloads needed jars to function correctly and it stored them in /wherever/jar/is/located/newFolder/libs/. Now what i have done is added those dependencies that are manually downloaded as compileOnly in my gradle but i dont know how to point gradle to location where the downloaded libs are since it can be different path on each machine that is run.
The dependency in question is io.sentry that i download to libs when the jar runs but how to tell my jar where to find classes if they are in libs folder as i previously stated.
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compileOnly 'org.jetbrains:annotations:15.0'
compileOnly 'io.sentry:sentry:1.7.30'
}
org.jetbrains:annotations and io.sentry:sentry are available publicly online. There's no need to download the artifacts manually. You can simply do:
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compileOnly 'org.jetbrains:annotations:15.0'
compileOnly 'io.sentry:sentry:1.7.30'
}
But if for some reason you must import the artifacts locally, then you can do something like:
repositories {
flatDir {
dirs '/wherever/jar/is/located/newFolder/libs/'
}
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compileOnly 'org.jetbrains:annotations:15.0'
compileOnly 'io.sentry:sentry:1.7.30'
}
Or include them one by one:
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compileOnly files('/wherever/jar/is/located/newFolder/libs/libs/jetbrains.jar')
compileOnly files('/wherever/jar/is/located/newFolder/libs/libs/sentry.jar'
}

org.apache.thrift.TApplicationException: Required field 'client_protocol' is unset

I'm trying to connect to HiveServer2 and run queries from my java application. When I run on my localserver(Hive version: 2.1.1). It works perfectly with the following dependencies:
compile('org.apache.hadoop:hadoop-core:1.2.1')
compile ('org.apache.hive:hive-jdbc:2.1.0'){
exclude group: 'org.eclipse.jetty.aggregate', module: '*'
exclude group: "org.slf4j", module: "slf4j-log4j12"
exclude group: "log4j", module: "log4j" }
compile ('org.apache.hadoop:hadoop-common:3.0.0'){
exclude group: "org.slf4j", module: "slf4j-log4j12"
exclude group: "log4j", module: "log4j"
}
But when I run on a remote server(Hive version: 0.13.1-SNAPSHOT). It throws the below mentioned error. I got to know that, the issue is with hive-jdbc, hadoop-core and hadoop-common versions. Can anyone let me know which versions of those dependencies I need to be using for Hive 0.13.1-SNAPSHOT
SEVERE: A child container failed during start
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Tomcat].StandardHost[localhost].TomcatEmbeddedContext[]]
After a few trail and errors I fixed the version issue. The issue is hive-jdbc:2.1.0 is not backward compatible with Hive 0.13.1-SNAPSHOT, we need to use hive-jdbc:0.13.1 to make it work. Below code works with no doubt.
compile('org.apache.hadoop:hadoop-core:1.2.1')
compile ('org.apache.hive:hive-jdbc:0.13.1'){
exclude group: 'org.eclipse.jetty.aggregate', module: '*'
exclude group: "org.slf4j", module: "slf4j-log4j12"
exclude group: "log4j", module: "log4j" }
compile ('org.apache.hadoop:hadoop-common:3.0.0'){
exclude group: "org.slf4j", module: "slf4j-log4j12"
exclude group: "log4j", module: "log4j"
}

Error:com.android.dex.DexException: Multiple dex files define Ledu/umd/cs/findbugs/annotations/NonNull;

My module dependencies:
configurations.all {
resolutionStrategy {
force 'junit:junit:4.12'
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-
core:2.2.2', {
exclude group: 'com.android.support', module: 'support-
annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
provided 'org.projectlombok:lombok:1.16.16'
annotationProcessor "org.projectlombok:lombok:1.16.16"
// For DL4J
compile 'org.deeplearning4j:deeplearning4j-core:0.9.1'
compile 'org.deeplearning4j:deeplearning4j-zoo:0.9.1'
compile 'org.nd4j:nd4j-native:0.9.1'
compile 'org.nd4j:nd4j-native:0.9.1:android-arm'
compile 'org.bytedeco.javacpp-presets:openblas:0.2.19-1.3:android-arm'
compile project(':dlbenchmark')
}
dlbenchmark dependencies are as below:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-
core:2.2.2', {
exclude group: 'com.android.support', module: 'support-
annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
testCompile 'junit:junit:4.12'
}
Whole project gradle fileļ¼š
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
google()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
dependencies {
}
and when building dl4j module in Android Studio I got an error:
Error:java.lang.RuntimeException: java.lang.RuntimeException:
com.android.builder.dexing.DexArchiveMergerException: Unable to merge
dex
Error:com.android.dex.DexException: Multiple dex files define
Ledu/umd/cs/findbugs/annotations/NonNull;
How can I fix this?
As the error message says there are multiple definitions for some classes.
I faced the same problem and was able to solve it by looking at the dependency tree.
If you run
./gradlew app:dependencies
in the Android studio terminal, you will get a dependency tree. You will find
com.google.code.findbugs:annotations:2.0.1
This library is required for testing and need not be packed with your apk. You can exclude it from your dependencies like this:
compile('org.nd4j:nd4j-native:0.9.1') {
exclude group: 'com.google.code.findbugs', module: 'annotations'
}
compile('org.nd4j:nd4j-native:0.9.1:android-x86')
{
exclude group: 'com.google.code.findbugs', module: 'annotations'
}
compile('org.nd4j:nd4j-native:0.9.1:android-arm')
{
exclude group: 'com.google.code.findbugs', module: 'annotations'
}
Now try to build and see if it works.

Categories