I am trying to build a Java project that I am going to eventually build into an executable Jar file. My Java project has some dependencies which are not packed inside my Jar file. So how can I make it so, so that when I run my jar file it will automatically download all the dependencies needed for it in order to run properly.
I dont want my jar file to be a fat jar file.
My Current build.gradle file looks like this:
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java application project to get you started.
* For more details take a look at the 'Building Java & JVM projects' chapter in the Gradle
* User Manual available at https://docs.gradle.org/8.0.1/userguide/building_java_projects.html
*/
plugins {
// Apply the application plugin to add support for building a CLI application in Java.
id 'application'
}
repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
}
dependencies {
// Use JUnit Jupiter for testing.
testImplementation 'org.junit.jupiter:junit-jupiter:5.9.1'
// This dependency is used by the application.
implementation 'com.google.guava:guava:31.1-jre'
implementation 'com.pi4j:pi4j-core:2.3.0'
// implementation 'com.pi4j:pi4j-ktx:2.3.0' // Kotlin DSL
implementation("com.pi4j:pi4j-plugin-raspberrypi:2.2.1")
implementation("com.pi4j:pi4j-plugin-pigpio:2.2.1")
}
application {
// Define the main class for the application.
mainClass = 'new_folder.App'
}
jar {
manifest {
attributes 'Main-Class': application.mainClass
}
}
tasks.named('test') {
// Use JUnit Platform for unit tests.
useJUnitPlatform()
}
you don't need to do anything.
gradle clean build
goto sub directory build/distributions
you can find DemoApp-1.0-SNAPSHOT.tar
and DemoApp-1.0-SNAPSHOT.zip
unzip DemoApp-1.0-SNAPSHOT.tar
goto directory DemoApp-1.0-SNAPSHOT
goto bin
, run command ./DemoApp
build.gradle
plugins {
id 'java'
id 'application'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
implementation 'com.google.guava:guava:31.1-jre'
implementation 'com.pi4j:pi4j-core:2.3.0'
implementation("com.pi4j:pi4j-plugin-raspberrypi:2.2.1")
implementation("com.pi4j:pi4j-plugin-pigpio:2.2.1")
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}
application {
mainClass = 'org.example.Main'
}
test {
useJUnitPlatform()
}
unzip DemoApp-1.0-SNAPSHOT.tar or DemoApp-1.0-SNAPSHOT.zip
File Structure
DemoApp-1.0-SNAPSHOT
├── bin
│ ├── DemoApp
│ └── DemoApp.bat
└── lib
├── checker-qual-3.12.0.jar
├── DemoApp-1.0-SNAPSHOT.jar
├── error_prone_annotations-2.11.0.jar
├── failureaccess-1.0.1.jar
├── guava-31.1-jre.jar
├── j2objc-annotations-1.3.jar
├── jsr305-3.0.2.jar
├── listenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar
├── pi4j-core-2.3.0.jar
├── pi4j-library-pigpio-2.2.1.jar
├── pi4j-plugin-pigpio-2.2.1.jar
├── pi4j-plugin-raspberrypi-2.2.1.jar
└── slf4j-api-1.7.32.jar
gradle application
will create Run Script like DemoApp
or DemoApp.bat
your dependencies implementation jar will put in lib
.