how do I get the jar task to set the Main-Class header from the mainClassName variable?
thufir#dur:~/NetBeansProjects/props$
thufir#dur:~/NetBeansProjects/props$ gradle clean assembleDist;java -jar build/libs/props.jar
BUILD SUCCESSFUL in 1s
6 actionable tasks: 6 executed
Publishing build scan...
https://gradle.com/s/qwirajeu4guhq
no main manifest attribute, in build/libs/props.jar
thufir#dur:~/NetBeansProjects/props$
thufir#dur:~/NetBeansProjects/props$ cat build.gradle
plugins {
id 'com.gradle.build-scan' version '1.8'
id 'java'
id 'application'
}
buildScan {
licenseAgreementUrl = 'https://gradle.com/terms-of-service'
licenseAgree = 'yes'
}
buildScan {
publishAlways()
}
sourceCompatibility = 1.9
targetCompatibility = 1.9
mainClassName = 'net.bounceme.dur.props.Main'
jar {
manifest {
// attributes 'Main-Class': mainClassName
attributes 'Class-Path': configurations.compile.collect { it.getName() }.join(' ')
}
}
repositories {
jcenter()
}
dependencies {
// This dependency is found on compile classpath of this component and consumers.
// compile 'com.google.guava:guava:22.0'
// Use JUnit test framework
// testCompile 'junit:junit:4.12'
}
thufir#dur:~/NetBeansProjects/props$
version '1.0'
apply plugin: 'java'
apply plugin: 'application'
mainClassName = 'blah.blah.blah.blah.myMain'
jar {
manifest {
attributes(
'Main-Class': mainClassName
)
}
}
This will result in a MANIFEST.MF of :
Manifest-Version: 1.0
Main-Class: blah.blah.blah.blah.myMain
Related
this is my build.gradle
this is what happens when i attempt to run the built jar.
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
implementation "net.dv8tion:JDA:5.0.0-alpha.6"
implementation group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '4.1.2'
implementation"io.github.bonigarcia:webdrivermanager:5.1.0"
}
test {
useJUnitPlatform()
}
jar {
manifest {
attributes 'Main-Class': 'bullshitPackage.main'
}
exclude 'META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA'
}
You could use the shadowjar plugin to include all dependencies in your jar.
To use it in your build.gradle file, try:
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "gradle.plugin.com.github.jengelman.gradle.plugins:shadow:7.0.0"
}
}
plugins {
id 'java'
id 'com.github.johnrengelman.shadow' version '7.0.0'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
implementation "net.dv8tion:JDA:5.0.0-alpha.6"
implementation group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '4.1.2'
implementation"io.github.bonigarcia:webdrivermanager:5.1.0"
}
test {
useJUnitPlatform()
}
jar {
manifest {
attributes 'Main-Class': 'bullshitPackage.main'
}
exclude 'META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA'
}
Then from your console, navigate to your project's root folder that contains the file gradlew, then run gradlew shadowjar to build the jar into the ./build/libs folder.
I build my project using gradle jar command, then try to start my application using command java -jar MyProject.jar
After that I have error:
Error: Could not find or load main class org.apdalgo.Main<br>
Caused by: java.lang.NoClassDefFoundError: javafx/application/Application
My build.gardle:
plugins {
id 'java'
id 'application'
id 'org.openjfx.javafxplugin' version '0.0.7'
}
group 'org.apdalgo'
version '1.0-SNAPSHOT'
sourceCompatibility = 11
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}
javafx {
version = "12"
modules = [ 'javafx.controls', 'javafx.fxml' ]
}
mainClassName = 'org.apdalgo.Main'
jar {
manifest {
attributes 'Main-Class': 'org.apdalgo.Main',
'Class-Path': configurations.runtime.files.collect { "lib/$it.name" }.join(' ')
}
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}
The command line need a bit more see https://openjfx.io/openjfx-docs/#install-javafx
java --module-path <PATH_TO_FX> --add-modules javafx.controls
I include ProjectA in ProjectB with compile files('../ProjectA/build/libs/ProjectA-1.0-SNAPSHOT.jar'). However, when running ProjectB I get classnotfound errors for dependencies in ProjectA. Like the selenium webdriver and okhttp. What must I do to get past those errors?
ProjectA build.gradle
plugins {
id 'java'
id 'org.jetbrains.kotlin.jvm' version '1.3.61'
id 'application'
}
group 'com.company.projectA'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
sourceSets {
main.java.srcDirs = ['src']
// main.kotlin.srcDirs = ['src/main/java', 'src/main/kotlin']
main.kotlin.srcDirs = ['src']
main.resources.srcDirs = ['src/main/resources']
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '2.41.0'
implementation("com.squareup.okhttp3:okhttp:4.3.1")
}
Project B build.gradle
plugins {
id 'java'
id 'org.jetbrains.kotlin.jvm' version '1.3.61'
id 'application'
}
group 'com.company.projectB'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
sourceSets {
main.java.srcDirs = ['src']
main.kotlin.srcDirs = ['src']
main.resources.srcDirs = ['src/main/resources']
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
// This pulls in the local project, but leaves out its dependencies.
compile files('../ProjectA/build/libs/ProjectA-1.0-SNAPSHOT.jar')
}
Depending on the generated jar file adds only the compiled classes from ProjectA as the dependency. In this case ProjectB should depend on ProjectA itself - that will add also ProjectA dependencies as transient dependencies.
Put settings.gradle in root directory of both projects:
settings.gradle
ProjectA/
build.gradle
ProjectB/
build.gradle
Include both projects in settings.gradle:
include ':ProjectA', ':ProjectB'
Add ProjectA as a dependency in ProjectB/build.gradle:
dependencies {
compile project(':ProjectA')
}
Or as in the example Project B build.gradle:
plugins {
id 'java'
id 'org.jetbrains.kotlin.jvm' version '1.3.61'
id 'application'
}
group 'com.company.projectB'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
sourceSets {
main.java.srcDirs = ['src']
main.kotlin.srcDirs = ['src']
main.resources.srcDirs = ['src/main/resources']
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
// This adds the local project with all its transient dependencies
implementation project(':ProjectA')
}
I am try to create multi module spring project with Gradle.
Each module has independent rest api service.
I haven't idea too much with Gradle.
library-application can access by application module but not able to execute simultaneously API of each modules using tomcat.
Module 1st : application
File settings.gradle:
rootProject.name = 'application'
include ':library-application'
project(':library-application').projectDir = new File('../library-application')
File build.gradle:
buildscript {
ext { springBootVersion = '2.1.3.RELEASE' }
repositories { jcenter()
mavenCentral()
maven { url "https://plugins.gradle.org/m2/" }
}
dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath 'org.gradle.api.plugins:gradle-tomcat-plugin:1.0',
classpath 'org.apache.openjpa:openjpa-all:2.4.1'
classpath 'at.schmutterer.oss.gradle:gradle-openjpa:0.2.0'
}
}
plugins {
id "io.spring.dependency-management" version "1.0.5.RELEASE"
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'war'
apply plugin: 'idea'
apply plugin: 'maven'
apply plugin: 'tomcat'
bootJar {
baseName = 'gs-multi-application'
version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
repositories { mavenCentral() }
dependencies {
compile('org.springframework.boot:spring-boot-starter-actuator')
compile('org.springframework.boot:spring-boot-starter-web')
compile project(':library-application')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
Module 2nd : library-application
File build.gradle:
buildscript {
repositories { jcenter()
mavenCentral()
maven { url "https://plugins.gradle.org/m2/" }
}
dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath 'org.gradle.api.plugins:gradle-tomcat-plugin:1.0',
classpath 'org.apache.openjpa:openjpa-all:2.4.1'
classpath 'at.schmutterer.oss.gradle:gradle-openjpa:0.2.0'
}
}
plugins {
id "io.spring.dependency-management" version "1.0.5.RELEASE"
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'war'
apply plugin: 'idea'
apply plugin: 'maven'
apply plugin: 'tomcat'
plugins { id "io.spring.dependency-management" version "1.0.5.RELEASE" }
ext { springBootVersion = '2.1.3.RELEASE' }
jar {
baseName = 'gs-multi-library'
version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
repositories { mavenCentral() }
dependencies {
compile('org.springframework.boot:spring-boot-starter')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
In general, root project in Gradle does nothing but only hold a overall build.gradle and settings.properties to group child projects together. Module planning should be done via managing dependencies of child projects, not the folder structure.
Try to organize your projects in this way:
root_project (very simple project doing nothing)
- library-application
- Provide service controllers
- application (depends on library-application)
- Provide SpringBootApplication
I'm using the Spring Boot gradle plugin to build an executable war. I have a FindResource.java class in src/main/resources to locate files:
FindResource.class.getResource(templateName).toURI()
When I execute gradle build I get an error, that the class FindResource cannot be resolved. Do I need to the the Spring Boot gradle plugin, that it should also use classes from the resources directory. How can I do so?
My build.gradle looks as follows:
buildscript {
ext {
springBootVersion = '1.2.5.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("io.spring.gradle:dependency-management-plugin:0.5.2.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'spring-boot'
apply plugin: 'io.spring.dependency-management'
jar {
baseName = 'abc'
version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("org.springframework.boot:spring-boot-starter-jersey")
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.apache.pdfbox:pdfbox:1.8.10")
compile('org.apache.poi:poi-ooxml:3.12')
compile('org.apache.poi:poi-scratchpad:3.12')
runtime("org.hsqldb:hsqldb")
testCompile("org.springframework.boot:spring-boot-starter-test")
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
As mentioned in the comment class files to load need to be in src/main/java/ and not in src/main/resources. This link may help give you more information on the convention of this structure.