0

I have a GRADLE application which has the following structure:

my project 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
}
User_Targaryen
  • 4,125
  • 4
  • 30
  • 51
  • See in example https://stackoverflow.com/questions/4871656/using-gradle-to-build-a-jar-with-dependencies – Daniele Aug 28 '19 at 14:07
  • Possible duplicate of [Using Gradle to build a jar with dependencies](https://stackoverflow.com/questions/4871656/using-gradle-to-build-a-jar-with-dependencies) – Daniele Aug 28 '19 at 14:08
  • The jar is getting created in the build directory. But I am getting the same manifest error on running the jar. Please help – User_Targaryen Aug 28 '19 at 16:50

1 Answers1

0

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

User_Targaryen
  • 4,125
  • 4
  • 30
  • 51
  • This looks basically like the `application` Gradle plugin: https://docs.gradle.org/current/userguide/application_plugin.html – Cisco Aug 28 '19 at 19:44