How to exclude a jar from gradle - java

I try to exclude a jar from gradle build but how to do that for my project part I am not clear.Below are the dependencies I have to exclude only geronimo-javamail_1.4_spec/1.7.1 jar because which gives error when I try to send mail.Please give your guidance to solve this.
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-batch")
//compile("org.springframework.boot:spring-boot-devtools")
compile('org.apache.commons':'commons-lang3':'3.5'){
exclude module: 'geronimo'
}
compile group: 'org.apache.cxf', name: 'cxf-spring-boot-starter-jaxws', version: '3.1.10'
compile group: 'org.apache.cxf', name: 'cxf-rt-ws-security', version: '3.1.10'
testCompile('org.springframework.boot:spring-boot-starter-test')
}
update:exclusion is not working

First of all, there is an issue with this statement:
compile('org.apache.commons':'commons-lang3':'3.5')
If you want to use the colon notation for a dependency, it has to be all in one String, like so:
compile('org.apache.commons:commons-lang3:3.5')
Also, you should probably use the full module name: module: 'geronimo-javamail_1.4_spec'.
Finally, geronimo-javamail_1.4_spec is a transitive dependency of more than one dependency in this setup, so you should exclude it everywhere where necessary one by one, or exclude it altogether like so:
configurations {
all*.exclude module: 'geronimo-javamail_1.4_spec'
}
This should be added in your build.gradle file at the same level as the dependencies{} section, not within it, so the final code would look like this:
configurations {
all*.exclude module: 'geronimo-javamail_1.4_spec'
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-batch")
//compile("org.springframework.boot:spring-boot-devtools")
compile('org.apache.commons:commons-lang3:3.5')
compile group: 'org.apache.cxf', name: 'cxf-spring-boot-starter-jaxws', version: '3.1.10'
compile group: 'org.apache.cxf', name: 'cxf-rt-ws-security', version: '3.1.10'
testCompile('org.springframework.boot:spring-boot-starter-test')
}

Related

Micronaut: Graalvm: Native image tracing agent not working

I have been developing MS in Micronaut, which I want to run as Graalvm native image on AWS Lambda. I have few dependencies like Jackson ObjectMapper, Apache POI, etc. in the project which does use reflection framework. But, Graalvm doesn't support reflection framework out of the box. So, we need to provide the configuration for the classes which are using reflection framework. To generate such configuration, I am trying to use Graalvm tracing agent, but it doesn't seems to be working.
It generates the config JSON files successfully.
But these config JSONs doesn't contain the classes from 3rd party libs which are added in build.gradle.
I have below parameter in gradle.properties file:
org.gradle.jvmargs=-agentlib:native-image-agent=config-output-dir=<path to dir>
Then, I run the project using gradle's run task from the IntelliJ, and do execute all the methods which actually use these libs (thus all those libs' code gets executed).
Once the execution is stopped, I find few config files generated on the dir path mentioned, but those files contains only classes related to Gradle, Jetbrains and few others. I cannot find any classes related to 3rd party libs which I am using and which are mentioned in build.gradle.
What should be the correct way to generate the reflection configuration JSON file for Micronaut Graalvm native image?
build.gradle:
plugins {
id("com.github.johnrengelman.shadow") version "7.1.2"
id("io.micronaut.application") version "3.5.1"
}
version = "0.1"
group = "com.base.package"
repositories {
mavenCentral()
}
ext {
orgJsonVersion = "20220320"
poiVersion = "3.9"
apacheCommons = "3.12.0"
googleGson = "2.9.1"
springValidation = "2.7.3"
eclipseLink = "3.0.3"
servletApi = "2.5"
velocity = "1.7"
velocityTools = "2.0"
kafkaClient = "2.1.1"
googleGuava = "27.0-jre"
}
dependencies {
annotationProcessor('io.micronaut:micronaut-inject-java')
annotationProcessor("io.micronaut.data:micronaut-data-processor")
annotationProcessor("io.micronaut:micronaut-http-validation")
implementation("io.micronaut:micronaut-http-client")
implementation("io.micronaut:micronaut-jackson-databind")
implementation("jakarta.annotation:jakarta.annotation-api")
runtimeOnly("ch.qos.logback:logback-classic")
// runtimeOnly('org.slf4j:log4j-over-slf4j:2.0.2')
implementation("io.micronaut:micronaut-validation")
implementation("io.micronaut.data:micronaut-data-hibernate-jpa")
implementation("io.micronaut.sql:micronaut-jdbc-hikari")
compileOnly("io.micronaut.sql:micronaut-hibernate-jpa")
runtimeOnly("mysql:mysql-connector-java")
compileOnly("org.graalvm.nativeimage:svm")
// compileOnly group: 'io.micronaut', name: 'micronaut-http-server-netty'
implementation("io.micronaut.aws:micronaut-function-aws")
implementation("io.micronaut.aws:micronaut-function-aws-custom-runtime")
implementation group: 'org.json', name: 'json', version: "${orgJsonVersion}"
implementation group: 'org.apache.poi', name: 'poi', version: "${poiVersion}"
implementation group: 'org.apache.poi', name: 'poi-ooxml', version: "${poiVersion}"
implementation group: 'org.apache.commons', name: 'commons-lang3', version: "${apacheCommons}"
implementation group: 'com.google.code.gson', name: 'gson', version: "${googleGson}"
// implementation group: 'org.springframework.boot', name: 'spring-boot-starter-validation', version: "${springValidation}"
implementation group: 'org.eclipse.persistence', name: 'eclipselink', version: "${eclipseLink}"
// compileOnly group: 'javax.servlet', name: 'servlet-api', version: "${servletApi}"
implementation("org.apache.velocity:velocity:${velocity}") {
exclude group: 'log4j', module: 'log4j'
}
implementation("org.apache.velocity:velocity-tools:${velocityTools}") {
exclude group: 'log4j', module: 'log4j'
}
implementation group: 'org.apache.kafka', name: 'kafka-clients', version: "${kafkaClient}"
implementation group: 'com.google.guava', name: 'guava', version: "${googleGuava}"
// implementation group: 'org.hibernate', name: 'hibernate-core', version: '5.6.9.Final'
implementation group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-hibernate5', version: '2.8.3'
implementation group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jsr310', version: '2.8.3'
}
application {
mainClass.set("com.base.package.MainApplication")
}
shadowJar {
mergeServiceFiles()
}
java {
sourceCompatibility = JavaVersion.toVersion("11")
targetCompatibility = JavaVersion.toVersion("11")
}
graalvmNative.toolchainDetection = false
micronaut {
runtime("lambda_provided")
testRuntime("junit5")
processing {
incremental(true)
annotations("com.base.package.*")
}
}
tasks.named("dockerfileNative") {
args(
"-XX:MaximumHeapSizePercent=80",
"-Dio.netty.allocator.numDirectArenas=0",
"-Dio.netty.noPreferDirect=true"
)
}
graalvmNative.binaries.all {
buildArgs.add("--no-server -J-Xmx12g -J-Xms4g -O0")
}
Since this is something you rarely do I usually run the command directly against the fat jar.
Here is how I do it.
Run ./gradlew build. This creates the fat jar in builds/libs
Goto build/libs
Run java -jar -agentlib:native-image-agent=config-output-dir=META-INF/native-image <your jar name>-all.jar

Apache Camel Timer Nullpointer exception at Fat Jar

I'm trying to implement a timer using Camel 3.5 at Gradle project with OpenJDK8 as next
from("timer://watchexpiration?fixedRate=true&period=600000&delay=0")...
But, after build the fat jar using ./gradlew build and run as java -jar build/libs/app.jar
I receive the next error at console
Caused by: org.apache.camel.ResolveEndpointFailedException: Failed to resolve endpoint: timer://watchexpiration?delay=0&fixedRate=true&period=600000 due to: Error binding property (delay=0) with name: delay on bean: timer://watchexpiration?delay=0&fixedRate=true&period=600000 with value: 0
at org.apache.camel.impl.engine.AbstractCamelContext.doGetEndpoint(AbstractCamelContext.java:888)
at org.apache.camel.impl.engine.AbstractCamelContext.getEndpoint(AbstractCamelContext.java:777)
at org.apache.camel.support.CamelContextHelper.getMandatoryEndpoint(CamelContextHelper.java:58)
at org.apache.camel.reifier.AbstractReifier.resolveEndpoint(AbstractReifier.java:177)
at org.apache.camel.reifier.RouteReifier.doCreateRoute(RouteReifier.java:250)
at org.apache.camel.reifier.RouteReifier.createRoute(RouteReifier.java:112)
But If I run using ./gradlew run then works fine as I expected.
I don't want to use any frameworks for this project. I feel this is just a config issue or something is wrong with my configuration I guess.
How can I fix it?
build.gradle
plugins {
id 'java'
id 'application'
id 'com.github.sherter.google-java-format' version '0.8'
}
repositories {
jcenter()
}
dependencies {
implementation 'com.google.guava:guava:29.0-jre'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.6.2'
// Camel
compile group: 'org.apache.camel', name: 'camel-core', version: '3.5.0'
compile group: 'org.apache.camel', name: 'camel-file', version: '3.5.0'
compile group: 'org.apache.camel', name: 'camel-file-watch', version: '3.5.0'
compile group: 'org.apache.camel', name: 'camel-xstream', version: '3.5.0'
compile group: 'org.apache.camel', name: 'camel-gson', version: '3.5.0'
compile group: 'org.apache.camel', name: 'camel-rest', version: '3.5.0'
compile group: 'org.apache.camel', name: 'camel-servlet', version: '3.5.0'
compile group: 'org.apache.camel', name: 'camel-http', version: '3.5.0'
compile group: 'org.apache.camel', name: 'camel-jackson', version: '3.5.0'
compile group: 'org.apache.camel', name: 'camel-quartz', version: '3.5.0'
compile group: 'org.apache.camel', name: 'camel-timer', version: '3.5.0'
compile group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.30'
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.30'
// Dev Libs
compileOnly("org.projectlombok:lombok:1.18.12")
annotationProcessor("org.projectlombok:lombok:1.18.12")
compile group: 'org.apache.commons', name: 'commons-csv', version: '1.4'
}
application {
mainClassName = 'com.eip.App'
}
configurations {
// configuration that holds jars to include in the jar
extraLibs
}
jar {
manifest {
attributes(
'Main-Class': 'com.beam.agent.App'
)
}
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
}
test {
useJUnitPlatform()
}
googleJavaFormat {
exclude '**/App.java'
}
Shadowing jars can be tricky, because you need to handle duplicate entries. In Apache Camel there are many META-INF service files, which are getting overwritten with your simple jar approach. Use com.github.johnrengelman.shadow which is allowing you to customize the merging process.
plugins {
id 'java'
id 'application'
id 'com.github.sherter.google-java-format' version '0.8'
id "com.github.johnrengelman.shadow" version "6.0.0" // Added plugin
}
repositories {
jcenter()
}
dependencies {
// ...
}
application {
mainClassName = 'com.eip.App'
}
// Removed jar step
test {
useJUnitPlatform()
}
googleJavaFormat {
exclude '**/App.java'
}
// Added shadow plugin configuration
shadowJar {
mergeServiceFiles() // Tell plugin to merge duplicate service files
manifest {
attributes 'Main-Class': 'com.eip.App'
}
}
apply plugin: 'com.github.johnrengelman.shadow'
Shaded executable jar will have suffix -all.jar
java -jar build/libs/app-all.jar

gradle exclude transient dependency from project

In my build.gradle I have the following snipped:
dependencies {
compile project(':utils')
compile (project(':cache')) {
// excludings because of dropwizard conflicts
exclude group: 'org.glassfish.jersey.core'
exclude group: 'javax.ws.rs'
exclude group: 'com.codahale.metrics'
}
but in fact the artifacts are not excluded I still end up with different jersey and metrics in my classpath. Ony if I put this in my cache/build.gradle it will compile and run.
configurations {
all*.exclude group: 'com.codahale.metrics'
all*.exclude group: 'org.glassfish.jersey.core'
}
But then my cache project is broken because of missing dependencies which is not what I wanted.
I've had issues with this in the past, what worked was when I included module: explicitly along with the group. like so:
exclude group: 'commons-math3', module: 'commons-math3'
If you use configurations to exclude, you don't have to do it on all*, you can do it on just local project compile as :
compile.exclude group: 'commons-math3', module: 'commons-math3'

How can I get a project in netbeans using the gradle plugin to find native libraries?

I am trying to figure out gradle and the netbeans plugin. I have easily been able to put together simple projects, but when trying to convert a project that uses JOGL to gradle I have run into problems. I can get it to compile, but when I try to get it to run, it does not. I assume it is not finding the native libraries.
Here is my build.gradle:
apply plugin: 'java'
apply plugin: 'application'
sourceCompatibility = '1.6'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
// NetBeans will automatically add "run" and "debug" tasks relying on the
// "mainClass" property. You may however define the property prior executing
// tasks by passing a "-PmainClass=<QUALIFIED_CLASS_NAME>" argument.
//
// Note however, that you may define your own "run" and "debug" task if you
// prefer. In this case NetBeans will not add these tasks but you may rely on
// your own implementation.
if (!hasProperty('mainClass')) {
ext.mainClass = 'com.protocase.viewer.JDesigner'
}
repositories {
mavenCentral()
maven {
url "https://repository.apache.org/content/repositories/snapshots/"
}
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.10'
compile files('../logger/dist/logger.jar')
compile files('../postalker/build/libs/postalker.jar')
compile group: 'org.yaml', name: 'snakeyaml', version: '1.15+'
compile group: 'commons-io', name: 'commons-io', version: '2.4+'
compile group: 'gov.nist.math', name: 'jama', version: '1.0.3+'
compile group: 'org.apache.commons', name: 'commons-imaging', version: '1.0-SNAPSHOT+'
compile group: 'com.itextpdf', name: 'itextpdf', version: '5.5.5+'
compile files('/home/vextorspace/.netbeans/8.0.2/jogl-runtime/jogl.jar')
compile files('~/.netbeans/8.0.2/jogl-runtime/jogl.jar-natives-linux-amd64/libjogl_awt.so'\
, '~/.netbeans/8.0.2/jogl-runtime/jogl.jar-natives-linux-amd64/libjogl_cg.so', '~/.netbeans/8.\
0.2/jogl-runtime/jogl.jar-natives-linux-amd64/libjogl.so')
runtime fileTree(dir: '~/.netbeans/8.0.2/jogl-runtime/jogl.jar-natives-linux-amd64/', incl\
ude: '*.so')
compile files('~/.netbeans/8.0.2/gluegen-runtime/gluegen-rt.jar')
runtime files('~/.netbeans/8.0.2/gluegen-runtime/gluegen-rt.jar-natives-linux-amd64/libglu\
egen-rt.so')
compile files('libs/toxiclibscore.jar')
compile group: 'org.apache.commons', name: 'commons-math3', version: '3.5+'
compile group: 'commons-codec', name: 'commons-codec', version: '1.6'
compile group: 'commons-logging', name: 'commons-logging', version: '1.1.3'
compile group: 'org.apache.httpcomponents', name: 'httpcore', version: '4.3.3'
compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.3.6'
compile group: 'org.apache.httpcomponents', name: 'httpmime', version: '4.3.6'
}
but when I try to run I get
Executing: gradle run
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: com/sun/gluegen/runtime/DynamicLookupHelper
at java.lang.ClassLoader.defineClass1(Native Method)
Any help would be much appreciated!
Thank you
Avoid tilde character in paths in your build script. It is interpreted by bash when you use it on command line and Gradle knows nothing about it.

Gradle build multi modules project with transitive dependencies

I have problem with building my simple app. It contains 3 modules, ejb, rest and ear in which rest should be included. To achieve it I wrote build.gradle as one below. But I still have problems. Built ear looks like:
ear-1.0.ear
|--ejbs-1.0.jar
|--rest-1.0.jar
|--lib
|--ejbs-1.0.jar
|--other libs..
As you can see, I have here duplicated ejbs-1.0.jar. It's no something I want so I have tried to work this around. I have tried 2 approaches I found on web but neither of them worked. First one (comments with label 1) excluded all rest dependencies from going into lib dir. Second (label 2) did the same but also included rest-1.0.jar into lib, making it even worse.
Now I have no idea how to write my build.gradle so it puts jars made from subprojects in root dir, and their dependencies in lib dir. I have also tried to write something like comments with label 3, but this makes script fail. Is there easy way to excluded it like that with similar syntax?
project(":ejbs") {
apply plugin: "java"
dependencies {
compile group: 'javax.ejb', name: 'javax.ejb-api', version: '3.2'
compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.2'
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.2'
}
}
project(":rest") {
apply plugin: "java"
dependencies {
compile project(':ejbs')
compile group: 'javax.ejb', name: 'javax.ejb-api', version: '3.2'
compile group: 'javax.ws.rs', name: 'jsr311-api', version: '0.11'
compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.2'
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.2'
}
}
project(":ear") {
apply plugin: "java"
apply plugin: "ear"
dependencies {
//1: def nonTransitive = {transitive = false}
deploy project(":ejbs")
deploy project(":rest")
earlib project(path:":ejbs", configuration:"compile")
earlib project(path:":rest", configuration:"compile")//1: , nonTransitive
//2: add('earlib', project(':rest')) {
// transitive = false
//}
//3: earlib project(path:":rest", configuration:"compile") {
// exlude project(':ejbs')
//}
}
}
I finally found a solution. I made it this way:
project(":ear") {
apply plugin: "java"
apply plugin: "ear"
dependencies {
deploy project(":ejbs")
deploy project(":rest")
earlib project(path:":ejbs", configuration:"compile")
earlib(project(path:":rest", configuration:"compile")) {
exclude module: 'ejbs'
}
}
}

Categories