3
3
4
4
## Executable JAR
5
5
6
+ * With plain JAR plugin:*
7
+
8
+ This is the default Gradle task for creating a JAR archive. It packages your
9
+ compiled class files (* .class) and resources from your project into a single JAR
10
+ file.
11
+
12
+ It does not include any dependencies in the JAR. Your application needs the
13
+ libraries it depends on to be installed separately on the target system.
14
+
6
15
``` gradle
7
16
plugins {
8
17
id 'java'
@@ -17,18 +26,67 @@ repositories {
17
26
}
18
27
19
28
application {
20
- // Define the main class for the application.
21
- mainClass = 'com.example.Main'
29
+ mainClass = 'com.zetcode.Main'
22
30
}
23
31
24
32
jar {
25
- manifest {
26
- attributes(
27
- 'Main-Class': 'com.example.Main'
28
- )
29
- }
30
- from {
31
- configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
32
- }
33
+ duplicatesStrategy(DuplicatesStrategy.EXCLUDE)
34
+ manifest {
35
+ attributes(
36
+ 'Main-Class': 'com.zetcode.Main'
37
+ )
38
+ }
39
+ from {
40
+ configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
41
+ }
33
42
}
34
43
```
44
+
45
+ * With shadow JAR plugin:*
46
+
47
+ It creates a fat JAR or shadow JAR, which is a single JAR containing your
48
+ application code and all its transitive dependencies.
49
+
50
+ It makes deployment easier as you only need to distribute the single JAR file
51
+ containing everything your application needs. The JAR file will be larger due to
52
+ the inclusion of all dependencies.
53
+
54
+
55
+ ``` gradle
56
+ plugins {
57
+ id 'java'
58
+ id 'application'
59
+ id 'io.github.goooler.shadow' version '8.1.8'
60
+ }
61
+
62
+ group = 'com.zetcode'
63
+ version = '1.0-SNAPSHOT'
64
+
65
+ repositories {
66
+ mavenCentral()
67
+ }
68
+
69
+ application {
70
+ mainClass = 'com.zetcode.Main'
71
+ }
72
+
73
+ dependencies {
74
+ implementation 'org.eclipse.collections:eclipse-collections:11.1.0'
75
+ }
76
+
77
+ jar {
78
+ // duplicatesStrategy = DuplicatesStrategy.EXCLUDE
79
+ duplicatesStrategy(DuplicatesStrategy.EXCLUDE)
80
+ exclude 'META-INF/*.RSA', 'META-INF/*.SF', 'META-INF/*.DSA'
81
+ manifest {
82
+ attributes(
83
+ 'Main-Class': 'com.zetcode.Main'
84
+ )
85
+ }
86
+ from {
87
+ configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
88
+ }
89
+ }
90
+ ```
91
+
92
+
0 commit comments