Skip to content

Commit 2760e69

Browse files
Gooolergemini-code-assist[bot]Copilot
authored
Document handing DuplicatesStrategy (#1343)
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Co-authored-by: Copilot <[email protected]>
1 parent 6faea4c commit 2760e69

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

docs/configuration/merging/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,3 +355,45 @@ It must be added using the [`transform`](https://gradleup.com/shadow/api/shadow/
355355
}
356356
}
357357
```
358+
359+
## Handling Duplicates Strategy
360+
361+
`ShadowJar` is a subclass of
362+
[`org.gradle.api.tasks.AbstractCopyTask`](https://docs.gradle.org/current/dsl/org.gradle.api.tasks.AbstractCopyTask.html),
363+
which means it honors the `duplicatesStrategy` property as its parent classes do. There are several strategies to handle:
364+
365+
- `EXCLUDE`: Do not allow duplicates by ignoring subsequent items to be created at the same path.
366+
- `FAIL`: Throw a `DuplicateFileCopyingException` when subsequent items are to be created at the same path.
367+
- `INCLUDE`: Do not attempt to prevent duplicates.
368+
- `INHERIT`: Uses the same strategy as the parent copy specification.
369+
- `WARN`: Do not attempt to prevent duplicates, but log a warning message when multiple items are to be created at the same path.
370+
371+
You can see more details about them in
372+
[`DuplicatesStrategy`](https://docs.gradle.org/current/javadoc/org/gradle/api/file/DuplicatesStrategy.html).
373+
374+
`ShadowJar` recognizes `DuplicatesStrategy.INCLUDE` as the default, if you want to change the strategy, you can
375+
override it like:
376+
377+
=== "Kotlin"
378+
379+
```kotlin
380+
tasks.shadowJar {
381+
duplicatesStrategy = DuplicatesStrategy.EXCLUDE // Or something else.
382+
}
383+
```
384+
385+
=== "Groovy"
386+
387+
```groovy
388+
tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) {
389+
duplicatesStrategy = DuplicatesStrategy.EXCLUDE // Or something else.
390+
}
391+
```
392+
393+
Different strategies will lead to different results for `foo/bar` files in the JARs to be merged:
394+
395+
- `EXCLUDE`: The **first** `foo/bar` file will be included in the final JAR.
396+
- `FAIL`: **Fail** the build with a `DuplicateFileCopyingException` if there are duplicated `foo/bar` files.
397+
- `INCLUDE`: The **last** `foo/bar` file will be included in the final JAR (the default behavior).
398+
- `INHERIT`: **Fail** the build with an exception like `Entry .* is a duplicate but no duplicate handling strategy has been set`.
399+
- `WARN`: The **last** `foo/bar` file will be included in the final JAR, and a warning message will be logged.

0 commit comments

Comments
 (0)