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
Related
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
It was working fine when external dependencies were in the separate lib folder, but due to change in requirements jar should contain all external dependencies in it, not in a separate folder.
Can anyone Guide me how can I execute the jar file
here is my Gradle file
apply plugin: 'java-library-distribution'
group = 'com.MyCompany'
version = '0.0.1-SNAPSHOT'
description = "data-purge-service"
sourceCompatibility = 1.8
targetCompatibility = 1.8
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
dependencies {
compile group: 'commons-configuration', name: 'commons-configuration', version:'1.6'
compile group: 'log4j', name: 'log4j', version:'1.2.17'
compile group: 'log4j', name: 'apache-log4j-extras', version:'1.2.17'
compile group: 'org.quartz-scheduler', name: 'quartz', version:'2.2.1'
compile group: 'org.json', name: 'json', version:'20151123'
compile group: 'com.google.inject', name: 'guice', version:'3.0'
compile group: 'commons-codec', name: 'commons-codec', version:'1.10'
compile group: 'org.apache.velocity', name: 'velocity', version:'1.7'
compile group: 'javax.mail', name: 'mail', version:'1.4'
compile group: 'org.jsoup', name: 'jsoup', version:'1.8.3'
runtime group: 'mysql', name: 'mysql-connector-java', version:'6.0.6'
testCompile group: 'junit', name: 'junit', version:'3.8.1'
}
jar {
manifest {
attributes "Main-Class": "com.MyCompany.datapurge.service.App"
}
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}
sourceSets {
main {
output.resourcesDir = "build/libs/resources"
}
}
}
When I execute generated jar it is unable to locate dependencies.
What is the way to execute it?
For my case, with gradle version 5 , dependencies were only added when i used configurations.runtimeClasspath instead configurations.compile, as posted in many places.
Here the full task example:
task BuildJar(type: Jar) {
manifest {
attributes(
'Main-Class': 'org.mycompany.mainclass'
)
}
classifier = 'all'
baseName = 'MyJar'
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
{
exclude "META-INF/*.SF"
exclude "META-INF/*.DSA"
exclude "META-INF/*.RSA"
}
with jar
}
It will create a single jar with everything.
To run the task do:
gradle task BuildJar //use gradew.bat for windows
It will place the jar at ./build/libs
To run just do:
java -jar MyJar.jar
Use Java Decompile to analise the jar.
"In this tutorial, we will show you how to use Gradle build tool to create a single Jar file with dependencies."
I have a project where I am using both Kotlin and Java code. My main Kotlin code (including the #SpringBootApplication main class) are in src/main/kotlin, but there is some legacy client library code which is in src/main/java. I also have some tests in src/test/kotlin.
In IntelliJ I can run the tests without issue, but when I run the test using gradle test I get errors Unresolved reference and cannot access class **. I am unsure why this is happening. The errors related to code in the Java classes.
My kotlin code and tests are in a slightly different package to the client library code; my main code is in a package called com.sky.vision.playlistapi and the client library is in a package called com.sky.nifty. I wonder if this could be the root of the problem?
My build.gradle file is here:
buildscript {
ext {
kotlinVersion = '1.2.10'
}
}
plugins {
id 'java'
id 'org.springframework.boot' version '1.5.9.RELEASE'
id 'org.jetbrains.kotlin.jvm' version'1.2.10'
id 'org.jetbrains.kotlin.plugin.allopen' version '1.2.10'
id 'org.jetbrains.kotlin.plugin.spring' version '1.2.10'
id 'org.jetbrains.kotlin.plugin.jpa' version '1.2.10'
id 'org.jetbrains.kotlin.plugin.noarg' version '1.2.10'
}
group = 'com.sky.vision'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile(group: 'org.springframework.boot', name: 'spring-boot-starter')
compile(group: 'org.springframework.boot', name: 'spring-boot-starter-web')
compile(group: 'org.springframework.boot', name: 'spring-boot-starter-security')
compile(group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa')
compile(group: 'org.jetbrains.kotlin', name: 'kotlin-stdlib-jdk8', version: kotlinVersion)
compile(group: 'org.jetbrains.kotlin', name: 'kotlin-reflect', version: kotlinVersion)
compile(group: 'org.apache.commons', name: 'commons-lang3', version: '3.4')
compile(group: 'com.fasterxml.jackson.core', name: 'jackson-core')
compile(group: 'com.fasterxml.jackson.core', name: 'jackson-databind')
compile(group: 'org.reflections', name: 'reflections', version: '0.9.9')
compile(group: 'org.aspectj', name: 'aspectjweaver')
compile(group: 'ch.qos.logback', name: 'logback-classic', version: '1.1.7')
testCompile(group: 'org.springframework.boot', name: 'spring-boot-starter-test')
testCompile(group: 'org.jetbrains.kotlin', name: 'kotlin-test')
testCompile(group: 'org.jetbrains.kotlin', name: 'kotlin-test-junit')
runtime(group: 'org.postgresql', name: 'postgresql', version: '9.4-1201-jdbc41')
}
And here is my project layout:
If anyone is able to help me understand the problem I would be grateful.
It turns out the problem was that I had some .java classes inside src/main/kotlin package. Once I moved these under src/main/java package the problem was resolved.
I use gretty to easily run a dev server and webapp-runner for deployment to heroku.
The following is my gradle.build:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'org.akhikhl.gretty:gretty:+'
}
}
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'eclipse-wtp'
apply plugin: 'idea'
apply plugin: 'org.akhikhl.gretty'
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
compile 'org.springframework:spring-webmvc:4.3.10.RELEASE'
compile 'org.springframework:spring-orm:4.3.10.RELEASE'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.0'
compile group: 'org.hibernate', name: 'hibernate-core', version: '5.2.10.Final'
compile group: 'org.apache.commons', name: 'commons-dbcp2', version: '2.1.1'
compile group: 'mysql', name: 'mysql-connector-java', version: '6.0.6'
compile group: 'org.hibernate.validator', name: 'hibernate-validator', version: '6.0.1.Final'
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.0'
compile group: 'org.postgresql', name: 'postgresql', version: '42.1.4'
compile 'com.github.jsimone:webapp-runner:8.5.11.3'
}
gretty {
httpPort = 8080
servletContainer = 'jetty9'
contextPath = '/'
}
eclipse {
wtp {
component {
contextPath = '/'
}
}
}
///////// Tasks for deployment to heroku
task stage() {
dependsOn clean, war
}
war.mustRunAfter clean
task copyToLib(type: Copy) {
dependsOn war
into "$buildDir/server"
from(configurations.compile) {
include "webapp-runner*"
}
}
stage.dependsOn(copyToLib)
If I remove webapp-runner everything runs fine, but with it I get the following error when trying to start gretty:
java.lang.IllegalStateException: Duplicate fragment name: org_apache_jasper for jar
Not an expert but I figure its something to do with the fact that both gretty and webapp-runner download similar files and that's causing a clash?
Would really appreciate some info on this. How do I get past this? Is there a better way to have a dev server + be able to deploy to heroku? (maybe use webapp-runner for both?)
I recommend running locally the same way as Heroku runs your app, with these commands:
$ ./gradlew stage
$ heroku local web
If you want to use gretty for development, you'll need to exclude webapp-runner from your dev build (maybe with a stageDev task), and exclude gretty from your stage build.
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'
}
}
}