How to run my Gradle Project as a JAR from terminal - java

I have a GRADLE application which has the following structure:
It has some dependencies as well (can see it from the image above)
I have built it using ./gradlew clean build
I am trying to run the following from the terminal java -jar build/libs/script-application-1.0-SNAPSHOT.jar which is throwing no main manifest attribute, in script-application-1.0-SNAPSHOT.jar
I have the following in build.gradle
plugins {
id 'java'
}
jar {
manifest {
attributes(
'Main-Class': 'Application'
)
}
}
group 'ScriptApplication'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.9.3'
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.5'
}
Please help. How can I run the jar from terminal? The next step is to run the jar from a remote server.
Edit:
Followed the suggestion from the comment and updated my build.gradle. But I still don't understand where will the jar get generated and how will I run the jar?
plugins {
id 'java'
}
group 'ScriptApplication'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.9.3'
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.5'
}
task fatJar(type: Jar) {
manifest {
attributes 'Main-Class': 'Application'
}
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}

Had to add:
task fatJar(type: Jar) {
manifest {
attributes 'Main-Class': 'Application'
}
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
Then run gradle clean and gradle fatJar.
Then one can run the jar by:
java -jar build/libs/<application>.jar

Related

Gradle Heroku Task 'stage'

I have a problem with deploying gradle project to heroku.
Here is my gradle.build file
plugins {
id 'java'
id 'distribution'
}
group 'com.artek.ej_bot'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
flatDir {
dirs 'libs'
}
mavenCentral()
}
dependencies {
compile files(fileTree(dir: 'libs', includes: ['*.jar']))
compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.6'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
task stage {
dependsOn installDist
}
And I keep getting this error:
It looks like your project does not contain a 'stage' task, which Heroku needs in order to build your app
I run this command heroku config:set GRADLE_TASK="build" in terminal and deploy became successful
here's how I have setup the stage task for my gradle project deployed on Heroku
task stage(type: Copy, dependsOn: ['build', 'clean', 'shadowJar']) {
from 'build/libs/app-0.0.1.jar'
into project.rootDir
rename {
'app.jar'
}
}
stage.mustRunAfter(clean)

NoClassDefFoundError for grizzly HTTP server

I am trying to execute a Grizzly HTTP Server inside a container.
Why i have this error, while running jar:
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/glassfish/grizzly/http/server/HttpServer
All was allright in maven with pom, but i met this error in gradle.
This is my build.gradle:
buildscript {
repositories {
mavenCentral()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "gradle.plugin.com.palantir.gradle.docker:gradle-docker:0.13.0",
}
}
group 'com.mycompany'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'com.palantir.docker'
docker {
name "${project.group}/${jar.baseName}"
files 'build/libs'
buildArgs(['JAR_FILE': "${jar.archiveName}"])
}
jar {
manifest {
attributes 'Main-Class': 'com.mycompany.json_validator.Main'
}
baseName = 'JSON_validator-boot-docker'
version = '0.2.0'
archiveName = 'JSON_validator-boot-docker-0.2.0.jar'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.0'
compile group: 'org.glassfish.jersey.media', name: 'jersey-media-moxy', version: '2.26'
compile group: 'org.glassfish.jersey.media', name: 'jersey-media-multipart', version: '2.7'
compile group: 'org.glassfish.jersey.containers', name: 'jersey-container-grizzly2-http', version: '2.17'
compile group: 'org.glassfish.jersey', name: 'jersey-bom', version: '2.17', ext: 'pom'
}
All in all, i want to build jar for docker.
java.lang.NoClassDefFoundError:
org/glassfish/grizzly/http/server/HttpServer
NoClassDefFoundError indicates definition of the class could be found at the runtime, this could be missing jar/libraries. Make sure, you have included grizzly-http-server-x.x.x.jar and related jars in classpath.

gretty appStart fails with unknown property 'classesDirs'

I'm trying to build a Java Servlet with gradle gretty plugin (gradle version 3.4).
I have a dependency to another project databaseprovider:1.0-SNAPSHOT (includes connection to the database and some spring definitions...).
When I run the task "gradle war" the .war-file is built properly with all dependencies in WEB-INF/lib.
But when I try to start with "gradle appStart" I have following problem:
What went wrong: Execution failed for task ':appStart'. Could not get
unknown property 'classesDirs' for main classes of type
org.gradle.api.internal.tasks.DefaultSourceSetOutput.
Caused by: groovy.lang.MissingPropertyException: Could not get unknown
property 'classesDirs' for main classes of type
org.gradle.api.internal.tasks.DefaultSourceSetOutput.
Without the dependency databaseprovider:1.0-SNAPSHOT the jetty starts without problems:
INFO Jetty 9.2.22.v20170606 started and listening on port 8080
build.gradle:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "org.akhikhl.gretty:gretty:+"
classpath "org.springframework.boot:spring-boot-gradle-plugin:1.4.0.RELEASE"
}
}
apply plugin:'java'
apply plugin: 'eclipse'
apply plugin: 'war'
apply from: 'https://raw.github.com/akhikhl/gretty/master/pluginScripts/gretty.plugin'
repositories {
maven {
url "http://..."
}
mavenLocal()
}
dependencies {
compile('databaseprovider:1.0-SNAPSHOT'){
changing=true
}
}
gretty {
httpPort = 8080
contextPath = '/'
servletContainer = 'jetty9'
}
build.gradle from databaseprovider:
buildscript {
repositories {
maven {
url "http://..."
}
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.0.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'eclipse'
apply plugin: 'spring-boot'
apply from: 'liquibase.gradle'
jar {
baseName = 'databaseprovider'
version = '1.0-SNAPSHOT'
}
repositories {
maven {
url "http://..."
}
mavenLocal()
jcenter()
}
configurations.all {
resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile("org.springframework.boot:spring-boot-starter-data-jpa:1.4.0.RELEASE")
compile group: 'org.springframework.data', name: 'spring-data-jpa', version:'1.10.2.RELEASE'
compile group: 'org.hibernate', name: 'hibernate-c3p0', version: '5.0.9.Final'
testRuntime group: 'com.h2database', name: 'h2', version: '1.4.192'
compile group: 'ch.qos.logback', name: 'logback-classic', version:'1.0.13'
compile group: 'ch.qos.logback', name: 'logback-core', version:'1.0.13'
compile group: 'org.slf4j', name: 'slf4j-api', version:'1.7.5'
compile group: 'org.slf4j', name: 'jcl-over-slf4j', version:'1.7.5'
compile group: 'org.slf4j', name: 'log4j-over-slf4j', version:'1.7.5'
compile group: 'org.reflections', name: 'reflections', version: '0.9.11'
compile (group: 'com.mattbertolini', name: 'liquibase-slf4j', version: "1.2.1")
compile (group: 'org.liquibase', name: 'liquibase-core', version: "3.5.3")
compile group: 'javax.validation', name: 'validation-api', version: '1.1.0.Final'
testCompile("junit:junit")
runtime("mysql:mysql-connector-java:5.1.39")
testCompile("org.springframework:spring-test")
}
I was able to solve the problem by adding the version of gretty:
After I changed the classpath in build.gradle
classpath "org.akhikhl.gretty:gretty:+"
to
classpath "org.akhikhl.gretty:gretty:1.4.2"
(compatible version to gradle 3.4) jetty starts without exceptions.
I found this question by googling for the error message when I encountered this issue. For me, I found my answer in this Github thread. Basically, by upgrading my version of Gradle to 4.8 I was able to resolve the issue.

ClassNotFoundException when building with gradle and trying to use com.google.code.gson

I'm new to Java projects and gradle. I'm trying to use Google's GSON to parse a JSON string.
When I navigate to my-project-name/build/classes/main/
I get the ClassNotFoundException when I run
$ java GetGroupMeMessages
Here is the stack trace (That's what it's called, right?):
Exception in thread "main" java.lang.NoClassDefFoundError: com/google/gson/Gson
at GetGroupMeMessages.main(GetGroupMeMessages.java:59)
Caused by: java.lang.ClassNotFoundException: com.google.gson.Gson
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
My build.gradle file looks like this:
apply plugin: 'java'
apply plugin: 'eclipse'
sourceCompatibility = 1.8
version = '1.0'
jar {
manifest {
attributes 'Implementation-Title': 'gscc-groupme-logs',
'Implementation-Version': version
}
}
repositories {
mavenCentral()
}
dependencies {
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.8.6'
compile group: 'commons-collections', name: 'commons-collections', version: '3.2.2'
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.0'
testCompile group: 'junit', name: 'junit', version: '4.+'
}
test {
systemProperties 'property': 'value'
}
uploadArchives {
repositories {
flatDir {
dirs 'repos'
}
}
}
I'm doing everything from the command line.
I've got my files saved under my-project-name/src/main/java/
Any help is much appreciated! I am stuck.
If you want to run single java file from command line, you need to provide gson jar as classpath using -cp jar_location in your command.
If you want to run build jar:
change dependencies from compile to runtime in your configuration.
dependencies {
runtime group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.8.6'
runtime group: 'commons-collections', name: 'commons-collections', version: '3.2.2'
runtime group: 'com.google.code.gson', name: 'gson', version: '2.8.0'
testCompile group: 'junit', name: 'junit', version: '4.+'
}
Change your jar config like following:
jar {
manifest {
attributes(
'Implementation-Title': 'gscc-groupme-logs',
'Implementation-Version': version
'Class-Path': configurations.compile.collect { it.getName() }.join(' '),
'Main-Class': 'GetGroupMeMessages'
)
}
}
Then after creating the jar run it using java -jar jarname command.

Could not find method suites() for arguments [src/test/resources/testSuite.xml] on project

I have created a project in gradle with testng framework, how to execute test-suite in testng using gradle, please help here is my build.gradle
build.gradle
apply plugin: 'java'
apply plugin: 'eclipse'
sourceCompatibility = 1.8
version = '1.0'
jar {
manifest {
attributes 'Implementation-Title': 'Gradle Quickstart',
'Implementation-Version': version
}
}
repositories {
mavenCentral()
}
dependencies {
compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
testCompile group: 'junit', name: 'junit', version: '4.+'
}
test {
systemProperties 'property': 'value'
}
test {
useTestNG() {
suites 'src/test/resources/testng.xml'
}
}

Categories