I want to create a jar file which can be used by command like java -cp .\TrendAnalyzer.jar trend_detect.Main
.
However, I can't find a way to create a jar file from settings.gradle, not build.gradle.
How can I create a jar file with dependencies from settings.gradle, which contains include
etc. information.

- 223
- 2
- 15
-
Look into here https://stackoverflow.com/questions/21721119/creating-runnable-jar-with-gradle – Babl Dec 06 '22 at 08:00
-
2Why just settings.gradle rather than build.gradle? – utrucceh Dec 06 '22 at 13:22
-
@Babl https://stackoverflow.com/a/21721305/3809427 is written in 2014 and it doesn't work. Shadow plugin works but https://github.com/yukihane/stackoverflow-qa/tree/main/jaso92559 (in Japanese) is more appropriate than official document. – Loran Dec 07 '22 at 23:17
-
@utrucceh I don't know when it started, now Gradle generates settings.gradle and dependency information is written in settings.gradle . So I think it's needed to generate jar file. However, a Jar file can be generated by editting build.gradle according to https://github.com/yukihane/stackoverflow-qa/tree/main/jaso92559 (in Japanese). – Loran Dec 07 '22 at 23:23
2 Answers
Add com.github.johnrengelman.shadow plugin to main module build.jar
plugins {
// *snip*
id 'com.github.johnrengelman.shadow' version '7.1.2'
}
application {
// Define the main class for the application.
mainClass = 'jaso92559.app.App'
}
jar {
manifest {
// Define the main class for the application.
attributes "Main-Class": "jaso92559.app.App"
}
}
run ./gradle shadowJar
command on the path where settings.gradle exists.
In app\build\libs, app-all.jar file is generated and it can be run java -jar app-all.jar
.
According to https://github.com/yukihane/stackoverflow-qa/tree/main/jaso92559

- 223
- 2
- 15
To create a jar file from settings.gradle, you can use the jar task in Gradle. This task allows you to package the compiled code and any dependencies into a jar file, which can then be executed using the java command with the -cp option.
Creating a jar file using the jar task in settings.gradle:
// settings.gradle
include ':trend_detect'
// build.gradle (in the trend_detect module)
apply plugin: 'java'
jar {
manifest {
attributes 'Main-Class': 'trend_detect.Main'
}
from {
configurations.compile.collect {
it.isDirectory() ? it :
zipTree(it) }
}
}
The include statement in settings.gradle specifies that the trend_detect module should be included in the build.
In the build.gradle file for the trend_detect module, the jar task is defined to create a jar file with a MANIFEST.MF file that specifies the main class (trend_detect.Main). The from statement in the jar task specifies that the jar file should include all of the compiled code and dependencies from the compile configuration.
Once the jar file has been created, you can run it using the java command with the -cp option, as follows:
java -cp .\TrendAnalyzer.jar trend_detect.Main
This will execute the main() method in the trend_detect.Main class, using the compiled code and dependencies contained in the jar file.

- 209
- 7