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.
Related
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
Full error is in this image as I am running on virtual machine in cloud which is access via video feed so I cannot copy and paste.This is a fresh install of ubuntu where I have only installed JDK and nothing else so unaware if other setup needs to be done
https://imgur.com/a/egJ3d
It is a spring boot application.
My build.gradle
group 'com.haughon.daniel'
version '1.0-SNAPSHOT'
buildscript {
repositories{
mavenCentral()
}
dependencies {
classpath 'org.springframework.boot:spring-boot-gradle-plugin:1.5.2.RELEASE'
}
}
// Apply the Spring Boot plugin
apply plugin: 'spring-boot'
// Apply the Java plugin (expects src/main/java to be source folder)
apply plugin: 'java'
apply plugin: 'idea'
// Specify the location where our dependencies will be found
repositories {
mavenCentral()
}
jar {
manifest {
attributes 'Main-Class': 'haughton.dvdstore.Application'
attributes 'addClasspath': 'true'
}
}
// Specify dependencies
dependencies {
compile 'org.hashids:hashids:1.0.1'
compile 'org.springframework.boot:spring-boot-starter-thymeleaf'
compile 'org.springframework:spring-orm:4.3.7.RELEASE'
compile 'org.hibernate:hibernate-core:5.2.9.Final'
compile 'org.hibernate:hibernate-entitymanager:5.0.6.Final'
compile 'org.apache.tomcat:tomcat-dbcp:8.0.30'
compile 'org.springframework.boot:spring-boot-starter-security'
compile('org.thymeleaf.extras:thymeleaf-extras-springsecurity4')
//compile 'org.thymeleaf.extras:thymeleaf-extras-springsecurity:3.0.2.RELEASE'
compile group: 'org.springframework.data', name: 'spring-data-jpa', version: '1.11.1.RELEASE'
// https://mvnrepository.com/artifact/com.google.code.gson/gson
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.0'
// https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core
compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.8.8'
runtime 'com.h2database:h2'
runtime 'javax.transaction:jta:1.1'
runtime 'org.aspectj:aspectjweaver:1.8.7'
testCompile 'org.springframework.boot:spring-boot-starter-test'
}
This is because your dependencies are not included in one jar file.
use ./gradlew clean build
please see this post:
java.lang.NoClassDefFoundError: when trying to run jar
I've been looking for a solution for this for 4 days now with no success,
first doing the normal Gradle build task produced a tiny 7kB jar files which obviously didn't work, then i added the jar configuration for the main class:
jar {
manifest {
attributes 'Main-Class': 'Main'
}
}
It's 2MB now!
Still not good enough,
then i found the fatJar/uberJar/shadow tasks, said to be synonymous to the same thing so i tried clean then fatJar tasks on another small RMI app that will run on the server and it worked great, copied it to the main app, it produces and 60MB jar file which looks good but it won't work for some reason,
The app runs perfectly from my IDE (IntelliJIDEA) but the jar produced just does nothing.
So my last option is to consult experts so here we go:
This's the result of the clean task:
6:11:10 PM: Executing external task 'clean'...
:clean
BUILD SUCCESSFUL
Total time: 0.774 secs
6:11:11 PM: External task execution finished
'clean'.
This's the result of the fatJar task:
6:13:13 PM: Executing external task 'fatJar'...
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
:compileJava
:processResources
:classes
:fatJar
BUILD SUCCESSFUL
Total time: 24.831 secs
6:13:37 PM: External task execution finished 'fatJar'.
This's my build.gradle file
group 'Yasmeena'
version '1.0-SNAPSHOT'
apply plugin: 'java'
compileJava.options.encoding = 'UTF-8'
sourceCompatibility = 1.8
jar {
manifest {
attributes 'Main-Class': 'Main'
}
}
task fatJar(type: Jar) {
manifest {
attributes 'Implementation-Title': 'Gradle Jar',
'Implementation-Version': version,
'Main-Class': 'Main'
}
baseName = project.name + '-all'
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
allprojects {
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
dependencies {
compile 'com.mchange:c3p0:0.9.5.2'
compile 'commons-beanutils:commons-beanutils:1.9.3'
// compile 'org.apache.commons:commons-collections3:2.2-SNAPSHOT'
compile 'commons-digester:commons-digester:2.1'
compile 'commons-logging:commons-logging:1.2'
compile 'commons-validator:commons-validator:1.6'
compile 'org.controlsfx:controlsfx:8.40.13'
compile 'com.jfoenix:jfoenix:1.7.0'
compile 'com.mchange:mchange-commons-java:0.2.11'
compile 'com.github.PlusHaze:TrayNotification:-SNAPSHOT'
compile 'com.google.firebase:firebase-admin:5.3.0'
compile 'org.jetbrains:annotations:15.0'
compile 'org.slf4j:slf4j-simple:1.7.25'
compile group: 'org.postgresql', name: 'postgresql', version: '42.1.4'
compile group: 'com.impossibl.pgjdbc-ng', name: 'pgjdbc-ng', version: '0.6'
compile group: 'org.eclipse.jdt.core.compiler', name: 'ecj', version: '4.6.1'
compile group: 'de.jensd', name: 'fontawesomefx', version: '8.9'
compile group: 'commons-collections', name: 'commons-collections', version: '3.2.2'
compile group: 'com.lowagie', name: 'itext', version: '2.1.7'
compile group: 'org.olap4j', name: 'olap4j', version: '1.2.0'
compile fileTree(dir: 'lib', include: 'ReportUtilities.jar')
compile('net.sf.jasperreports:jasperreports:6.4.1') {
exclude group: 'com.itextpdf'
}
runtime 'com.itextpdf:itextpdf:5.5.0'
runtime 'com.itextpdf:itext-pdfa:5.5.0'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
my Main.java class which holds my main method exists directly in the main -> java directory
This's my settings.gradle file:
rootProject.name = 'Yasmeena'
My main method is a simple method that launches the JavaFX application:
public static void main(String[] args) {
launch(args);
}
This's my project structure
UPDATE:
Current state of build.gradle file:
group 'Yasmeena'
version '1.0-SNAPSHOT'
apply plugin: 'java'
compileJava.options.encoding = 'UTF-8'
sourceCompatibility = 1.8
jar {
manifest {
attributes 'Main-Class': 'activities.Main'
}
}
allprojects {
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
dependencies {
compile 'com.mchange:c3p0:0.9.5.2'
compile 'commons-beanutils:commons-beanutils:1.9.3'
compile 'commons-digester:commons-digester:2.1'
compile 'commons-logging:commons-logging:1.2'
compile 'commons-validator:commons-validator:1.6'
compile 'org.controlsfx:controlsfx:8.40.13'
compile 'com.jfoenix:jfoenix:1.7.0'
compile 'com.mchange:mchange-commons-java:0.2.11'
compile 'com.github.PlusHaze:TrayNotification:-SNAPSHOT'
compile 'com.google.firebase:firebase-admin:5.3.0'
compile 'org.jetbrains:annotations:15.0'
compile 'org.slf4j:slf4j-simple:1.7.25'
compile group: 'org.postgresql', name: 'postgresql', version: '42.1.4'
compile group: 'com.impossibl.pgjdbc-ng', name: 'pgjdbc-ng', version: '0.6'
compile group: 'org.eclipse.jdt.core.compiler', name: 'ecj', version: '4.6.1'
compile group: 'de.jensd', name: 'fontawesomefx', version: '8.9'
compile group: 'commons-collections', name: 'commons-collections', version: '3.2.2'
compile group: 'com.lowagie', name: 'itext', version: '2.1.7'
compile group: 'org.olap4j', name: 'olap4j', version: '1.2.0'
compile fileTree(dir: 'lib', include: 'ReportUtilities.jar')
compile('net.sf.jasperreports:jasperreports:6.4.1') {
exclude group: 'com.itextpdf'
}
runtime 'com.itextpdf:itextpdf:5.5.0'
runtime 'com.itextpdf:itext-pdfa:5.5.0'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'eu.appsatori:gradle-fatjar-plugin:0.3'
}
}
task fatJar(type: Jar) {
manifest {
attributes 'Implementation-Title': 'Gradle Jar',
'Implementation-Version': version,
'Main-Class': 'activities.Main',
'Class-Path': ". ${configurations.compile.collect { it.getName() }.join(' ')}"
}
baseName = project.name + '-all'
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
Current state of project structure
Inside the jar file there's an activities folder which contains a Main.class file
UPDATE:
jar file -> META-INF -> MANIFEST.MF
Manifest-Version: 1.0
Implementation-Title: Gradle Jar
Implementation-Version: 1.0-SNAPSHOT
Class-Path: . ReportUtilities.jar c3p0-0.9.5.2.jar commons-beanutils-1
.9.3.jar commons-digester-2.1.jar commons-logging-1.2.jar commons-val
idator-1.6.jar controlsfx-8.40.13.jar jfoenix-1.7.0.jar mchange-commo
ns-java-0.2.11.jar TrayNotification--SNAPSHOT.jar firebase-admin-5.3.
0.jar annotations-15.0.jar slf4j-simple-1.7.25.jar postgresql-42.1.4.
jar pgjdbc-ng-0.6.jar ecj-4.6.1.jar fontawesomefx-8.9.jar commons-col
lections-3.2.2.jar itext-2.1.7.jar olap4j-1.2.0.jar jasperreports-6.4
.1.jar google-api-client-1.22.0.jar google-api-client-gson-1.22.0.jar
google-http-client-1.22.0.jar json-20160810.jar guava-20.0.jar googl
e-cloud-storage-1.2.1.jar slf4j-api-1.7.25.jar netty-all-4.0.32.Final
.jar bcmail-jdk14-138.jar bcprov-jdk14-138.jar xercesImpl-2.11.0.jar
jcommon-1.0.23.jar jfreechart-1.0.19.jar castor-xml-1.3.3.jar jackson
-core-2.1.4.jar jackson-databind-2.1.4.jar jackson-annotations-2.1.4.
jar lucene-core-4.5.1.jar lucene-analyzers-common-4.5.1.jar lucene-qu
eryparser-4.5.1.jar core-3.2.1.jar icu4j-57.1.jar google-oauth-client
-1.22.0.jar google-http-client-jackson2-1.22.0.jar google-http-client
-gson-1.22.0.jar httpclient-4.0.1.jar google-cloud-core-1.2.1.jar goo
gle-cloud-core-http-1.2.1.jar google-api-services-storage-v1-rev100-1
.22.0.jar castor-core-1.3.3.jar commons-lang-2.6.jar javax.inject-1.j
ar stax-1.2.0.jar stax-api-1.0-2.jar lucene-queries-4.5.1.jar lucene-
sandbox-4.5.1.jar guava-jdk5-17.0.jar httpcore-4.0.1.jar commons-code
c-1.3.jar joda-time-2.9.2.jar api-common-1.1.0.jar gax-1.4.1.jar prot
obuf-java-util-3.3.0.jar proto-google-common-protos-0.1.12.jar proto-
google-iam-v1-0.1.12.jar google-auth-library-credentials-0.7.0.jar go
ogle-auth-library-oauth2-http-0.7.0.jar google-http-client-appengine-
1.21.0.jar google-http-client-jackson-1.21.0.jar stax-api-1.0.1.jar j
akarta-regexp-1.4.jar auto-value-1.2.jar threetenbp-1.3.3.jar protobu
f-java-3.3.0.jar jackson-core-asl-1.9.11.jar bctsp-jdk14-1.38.jar bcp
rov-jdk14-1.38.jar bcmail-jdk14-1.38.jar xml-apis-1.4.01.jar jsr305-3
.0.0.jar gson-2.7.jar
Main-Class: activities.Main
As per the comments here is what was tried.
Making sure a main class is set
Making sure the main class has the same full class name as the one defined
Checking the manifest is valid
Checking the class exists
JavaFX may need a plugin
In the end the last one fixed the issue this was the plugin that fixed the issue.
This was the last comment in the chain:
#jrtapsell Well, this's embarrassing but i'v found the problem, i was unaware that JavaFX apps need a special plugin for Gradle, Using the FiberFox Plugin solved my issue, Thanks for your efforts and may you post answers so that i could upvote you if you like
I have problems with configure my build.gradle for download jar and pom from svn repository. For example, i have url:
https://svn.code.sf.net/p/springframework/svn/repos/repo-ext/javax/xml/crypto/xmldsig/1.0/
and i want did like this compile group: 'com.sun.xml.wss', name: 'xws-security', version: '3.0'
Also, manual download is wrong way.
UPD this is build.gradle file of backend project
apply plugin: 'java'
dependencies {
compile group: 'org.glassfish.metro', name: 'wssx-api', version: '2.1.1-b09'
compile group: 'com.sun.xml.wss', name: 'xws-security', version: '3.0'
compile project(':pp-backend')
}
If you're not downloading from maven central (which is configured in Gradle by default), you should configure the repository to download from using the 'repositories' closure:
repositories {
mavenCentral()
maven {
url 'https://svn.code.sf.net/p/springframework/svn/repos/repo-ext/'
}
}
Then, in the 'dependencies' closure, just add:
compile group: 'javax.xml.crypto', name: 'xmldsig', version: '1.0'
See more info in Dependency Management Basics
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'
}
}
}