Skip to content

Commit 1d0cecb

Browse files
authored
Update gradle.md
1 parent 0de9bec commit 1d0cecb

File tree

1 file changed

+68
-10
lines changed

1 file changed

+68
-10
lines changed

gradle.md

+68-10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@
33

44
## Executable JAR
55

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+
615
```gradle
716
plugins {
817
id 'java'
@@ -17,18 +26,67 @@ repositories {
1726
}
1827
1928
application {
20-
// Define the main class for the application.
21-
mainClass = 'com.example.Main'
29+
mainClass = 'com.zetcode.Main'
2230
}
2331
2432
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+
}
3342
}
3443
```
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

Comments
 (0)