Skip to content

Commit

Permalink
Add Gradle plugin to generate uber JAR
Browse files Browse the repository at this point in the history
Add Jon Regelmnan's Gradle plugin [shadow][1] to introduce the task
`shadowJar` that will generate an [uber JAR][2] that contains all
dependencies required to run the application.

[Configure shadow][3] to create a JAR file named `galaxy-raiders.jar`.

Update the README.md to use a lighter container (Eclipse Temujin
rather than Gradle) to run the project in production mode.

[1]: https://github.com/johnrengelman/shadow
[2]: https://stackoverflow.com/questions/11947037/what-is-an-uber-jar
[3]: https://imperceptiblethoughts.com/shadow/configuration/#configuring-output-name
  • Loading branch information
renatocf committed Jul 16, 2022
1 parent c2f44e4 commit b9aad52
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ To compile all classes in the project, run:
docker container run --rm --user gradle --volume "$PWD":/home/gradle/app --workdir /home/gradle/app gradle:7.4.2-jdk17 gradle --no-daemon clean assemble
```

To generate a runnable JAR of the project, run:
To generate a runnable self-contained JAR of the project, run:
```bash
docker container run --rm --user gradle --volume "$PWD":/home/gradle/app --workdir /home/gradle/app gradle:7.4.2-jdk17 gradle --no-daemon clean jar
docker container run --rm --user gradle --volume "$PWD":/home/gradle/app --workdir /home/gradle/app gradle:7.4.2-jdk17 gradle --no-daemon clean shadowJar
```

## Tests
Expand All @@ -61,9 +61,10 @@ To execute the project in development mode (with live reload), run:
docker container run --rm --user gradle --volume "$PWD":/home/gradle/app --workdir /home/gradle/app gradle:7.4.2-jdk17 gradle --continuous run
```

To execute the project in production mode, generate a runnable JAR, then run:
To execute the project in production mode, generate a runnable self-contained
JAR of the project, then run:
```bash
docker container run --rm --user gradle --volume "$PWD":/home/gradle/app --workdir /home/gradle/app gradle:7.4.2-jdk17 java -jar ./app/build/libs/app.jar
docker container run --rm --volume "$PWD"/app/build/libs/galaxy-raiders.jar:/bin/runner/galaxy-raiders.jar --workdir /bin/runner eclipse-temurin:17-jdk java -jar galaxy-raiders.jar
```

## Other tasks
Expand Down
16 changes: 16 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ plugins {
// Apply the Test Logger plugin to print test results in the test task.
id("com.adarshr.test-logger") version "3.2.0"

// Apply the Shadow JAR Plugin to generate a standalone uber Jar.
id("com.github.johnrengelman.shadow") version "7.1.2"

// Apply the Kover plugin to better support Kotlin code coverage.
id("org.jetbrains.kotlinx.kover") version "0.5.0"

Expand Down Expand Up @@ -79,3 +82,16 @@ tasks.test {
tasks.jacocoTestReport {
dependsOn(tasks.test)
}

// Configure the metadata to generate an uber JAR.
tasks.shadowJar {
archiveBaseName.set("galaxy-raiders")
archiveVersion.set("")
archiveClassifier.set("")
}

// Configure Gradle to send its stdin to the program.
// https://stackoverflow.com/questions/13172137/console-application-with-java-and-gradle
tasks.getByName("runShadow", JavaExec::class) {
standardInput = System.`in`
}

0 comments on commit b9aad52

Please sign in to comment.