Skip to content

Commit 941e065

Browse files
authored
Migrate ShadowJar to using lazy properties (#1044)
* Migrate public variables in ShadowJar * Rename isEnableRelocation to enableRelocation * Mark AbstractDependencyFilter properties Transient * Remove empty list conventions * Can't migrate relocators to using lazy property * Simplify getters by lazy * Remove @transient for dependencyFilterForMinimize * More named usages * Optimize get * Migrate FileCollection to ConfigurableFileCollection * Explicit allRelocators * Simplify allRelocators * Update baseline * Note this change * Deprecated isEnableRelocation * Tweak doc * Revert "Deprecated isEnableRelocation" This reverts commit 6d4f3aa. * Rearrange and tweak styles * Mark configurations as a ListProperty of Configuration
1 parent d373be1 commit 941e065

File tree

11 files changed

+112
-156
lines changed

11 files changed

+112
-156
lines changed

api/shadow.api

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -294,20 +294,21 @@ public abstract class com/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar
294294
protected fun createCopyAction ()Lorg/gradle/api/internal/file/copy/CopyAction;
295295
public fun dependencies (Lorg/gradle/api/Action;)Lcom/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar;
296296
public synthetic fun dependencies (Lorg/gradle/api/Action;)Lcom/github/jengelman/gradle/plugins/shadow/tasks/ShadowSpec;
297-
public final fun getApiJars ()Lorg/gradle/api/file/FileCollection;
298-
public final fun getConfigurations ()Ljava/util/List;
299-
public final fun getDependencyFilter ()Lcom/github/jengelman/gradle/plugins/shadow/internal/DependencyFilter;
300-
public final fun getIncludedDependencies ()Lorg/gradle/api/file/FileCollection;
297+
public final fun getApiJars ()Lorg/gradle/api/file/ConfigurableFileCollection;
298+
public abstract fun getConfigurations ()Lorg/gradle/api/provider/ListProperty;
299+
public abstract fun getDependencyFilter ()Lorg/gradle/api/provider/Property;
300+
public abstract fun getEnableRelocation ()Lorg/gradle/api/provider/Property;
301+
public abstract fun getIncludedDependencies ()Lorg/gradle/api/file/ConfigurableFileCollection;
301302
public fun getManifest ()Lcom/github/jengelman/gradle/plugins/shadow/tasks/InheritManifest;
302303
public synthetic fun getManifest ()Lorg/gradle/api/java/archives/Manifest;
303-
public final fun getRelocationPrefix ()Ljava/lang/String;
304-
public final fun getRelocators ()Ljava/util/List;
304+
public abstract fun getMinimizeJar ()Lorg/gradle/api/provider/Property;
305+
public abstract fun getRelocationPrefix ()Lorg/gradle/api/provider/Property;
306+
public abstract fun getRelocators ()Lorg/gradle/api/provider/ListProperty;
305307
public final fun getRootPatternSet ()Lorg/gradle/api/tasks/util/PatternSet;
306-
public final fun getSourceSetsClassesDirs ()Lorg/gradle/api/file/FileCollection;
308+
public final fun getSourceSetsClassesDirs ()Lorg/gradle/api/file/ConfigurableFileCollection;
307309
public fun getStats ()Lcom/github/jengelman/gradle/plugins/shadow/ShadowStats;
308-
public final fun getToMinimize ()Lorg/gradle/api/file/FileCollection;
309-
public final fun getTransformers ()Ljava/util/List;
310-
public final fun isEnableRelocation ()Z
310+
public final fun getToMinimize ()Lorg/gradle/api/file/ConfigurableFileCollection;
311+
public abstract fun getTransformers ()Lorg/gradle/api/provider/ListProperty;
311312
public fun mergeGroovyExtensionModules ()Lcom/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar;
312313
public synthetic fun mergeGroovyExtensionModules ()Lcom/github/jengelman/gradle/plugins/shadow/tasks/ShadowSpec;
313314
public fun mergeServiceFiles ()Lcom/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar;
@@ -330,12 +331,6 @@ public abstract class com/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar
330331
public synthetic fun relocate (Ljava/lang/String;Ljava/lang/String;)Lcom/github/jengelman/gradle/plugins/shadow/tasks/ShadowSpec;
331332
public fun relocate (Ljava/lang/String;Ljava/lang/String;Lorg/gradle/api/Action;)Lcom/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar;
332333
public synthetic fun relocate (Ljava/lang/String;Ljava/lang/String;Lorg/gradle/api/Action;)Lcom/github/jengelman/gradle/plugins/shadow/tasks/ShadowSpec;
333-
public final fun setConfigurations (Ljava/util/List;)V
334-
public final fun setDependencyFilter (Lcom/github/jengelman/gradle/plugins/shadow/internal/DependencyFilter;)V
335-
public final fun setEnableRelocation (Z)V
336-
public final fun setRelocationPrefix (Ljava/lang/String;)V
337-
public final fun setRelocators (Ljava/util/List;)V
338-
public final fun setTransformers (Ljava/util/List;)V
339334
public fun transform (Lcom/github/jengelman/gradle/plugins/shadow/transformers/Transformer;)Lcom/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar;
340335
public synthetic fun transform (Lcom/github/jengelman/gradle/plugins/shadow/transformers/Transformer;)Lcom/github/jengelman/gradle/plugins/shadow/tasks/ShadowSpec;
341336
public fun transform (Ljava/lang/Class;)Lcom/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar;

lint-baseline.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@
118118
errorLine2="~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
119119
<location
120120
file="src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar.kt"
121-
line="25"
121+
line="24"
122122
column="1"/>
123123
</issue>
124124

@@ -129,7 +129,7 @@
129129
errorLine2="~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
130130
<location
131131
file="src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar.kt"
132-
line="26"
132+
line="25"
133133
column="1"/>
134134
</issue>
135135

@@ -140,7 +140,7 @@
140140
errorLine2="~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
141141
<location
142142
file="src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar.kt"
143-
line="27"
143+
line="26"
144144
column="1"/>
145145
</issue>
146146

@@ -151,7 +151,7 @@
151151
errorLine2="~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
152152
<location
153153
file="src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar.kt"
154-
line="28"
154+
line="27"
155155
column="1"/>
156156
</issue>
157157

src/docs/changes/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
**Changed**
1111

1212
- **BREAKING CHANGE:** Rewrite this plugin in Kotlin. ([#1012](https://github.com/GradleUp/shadow/pull/1012))
13+
- **BREAKING CHANGE:** Migrate `ShadowJar` to using lazy properties. ([#1044](https://github.com/GradleUp/shadow/pull/1044))
14+
`isEnableRelocation` is deprecated, use `enableRelocation` instead.
1315
- **BREAKING CHANGE:** Resolve `Configuration` directly in `DependencyFilter`. ([#1045](https://github.com/GradleUp/shadow/pull/1045))
1416

1517
**Removed**

src/docs/configuration/relocation/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,16 @@ Shadow is shipped with a task that can be used to automatically configure all pa
6666
This feature was formally shipped into a 2nd plugin (`com.github.johnrengelman.plugin-shadow`) but has been
6767
removed for clarity reasons in version 4.0.0.
6868

69-
To configure automatic dependency relocation, set `enableRelocation true` and optionally specify a custom
69+
To configure automatic dependency relocation, set `enableRelocation = true` and optionally specify a custom
7070
`relocationPrefix` to override the default value of `"shadow"`.
7171

7272
```groovy
7373
// Configure Auto Relocation
7474
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
7575
7676
tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) {
77-
enableRelocation true
78-
relocationPrefix "myapp"
77+
enableRelocation = true
78+
relocationPrefix = "myapp"
7979
}
8080
```
8181

@@ -84,4 +84,4 @@ In versions before 8.1.0 it was necessary to configure a separate `ConfigureShad
8484
> Configuring package auto relocation can add significant time to the shadow process as it will process all dependencies
8585
in the configurations declared to be shadowed. By default, this is the `runtime` or `runtimeClasspath` configurations.
8686
Be mindful that some Gradle plugins will automatically add dependencies to your class path. You may need to remove these
87-
dependencies if you do not intend to shadow them into your library.
87+
dependencies if you do not intend to shadow them into your library.

src/docs/plugins/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ dependencies {
3131
}
3232
3333
tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) {
34-
enableRelocation true
34+
enableRelocation = true
3535
}
3636
```
3737

@@ -56,4 +56,4 @@ The `ConfigureShadowRelocation` task, scans the dependencies from the configurat
5656
packages using the specified `prefix` on the associated `ShadowJar` task.
5757

5858
While this is useful for developing Gradle plugins, nothing about the `ConfigureShadowRelocation` task is tied to
59-
Gradle projects. It can be used for standard Java or Groovy projects.
59+
Gradle projects. It can be used for standard Java or Groovy projects.

src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/ShadowJavaPlugin.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.github.jengelman.gradle.plugins.shadow
22

3+
import com.github.jengelman.gradle.plugins.shadow.internal.DefaultDependencyFilter
4+
import com.github.jengelman.gradle.plugins.shadow.internal.runtimeConfiguration
35
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
46
import javax.inject.Inject
57
import org.gradle.api.Plugin
@@ -88,11 +90,13 @@ public abstract class ShadowJavaPlugin @Inject constructor(
8890
shadow.manifest.attributes["Class-Path"] = attrs.joinToString(" ").trim()
8991
}
9092
}
91-
shadow.from(sourceSets.getByName("main").output)
92-
shadow.configurations = listOf(
93-
project.configurations.findByName(JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME)
94-
?: project.configurations.getByName("runtime"),
95-
)
93+
shadow.from(sourceSets.named("main").map { it.output })
94+
shadow.enableRelocation.convention(false)
95+
shadow.minimizeJar.convention(false)
96+
shadow.relocationPrefix.convention(ShadowBasePlugin.SHADOW)
97+
shadow.dependencyFilter.convention(DefaultDependencyFilter(project))
98+
shadow.configurations.convention(listOf(project.runtimeConfiguration))
99+
shadow.includedDependencies.setFrom(shadow.dependencyFilter.map { it.resolve(shadow.configurations.get()) })
96100
shadow.exclude(
97101
"META-INF/INDEX.LIST",
98102
"META-INF/*.SF",

src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/AbstractDependencyFilter.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import org.gradle.api.specs.Spec
1111
import org.gradle.api.specs.Specs
1212

1313
internal sealed class AbstractDependencyFilter(
14-
private val project: Project,
14+
@Transient private val project: Project,
15+
@Transient protected val includeSpecs: MutableList<Spec<ResolvedDependency>> = mutableListOf(),
16+
@Transient protected val excludeSpecs: MutableList<Spec<ResolvedDependency>> = mutableListOf(),
1517
) : DependencyFilter {
16-
protected val includeSpecs: MutableList<Spec<ResolvedDependency>> = mutableListOf()
17-
protected val excludeSpecs: MutableList<Spec<ResolvedDependency>> = mutableListOf()
1818

1919
protected abstract fun resolve(
2020
dependencies: Set<ResolvedDependency>,

src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/UnusedTracker.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ internal class UnusedTracker private constructor(
5151
fun getApiJarsFromProject(project: Project): FileCollection {
5252
val apiDependencies = project.configurations.findByName("api")?.dependencies
5353
?: return project.files()
54-
val runtimeConfiguration = project.configurations.findByName("runtimeClasspath")
55-
?: project.configurations.getByName("runtime")
54+
val runtimeConfiguration = project.runtimeConfiguration
5655
val apiJars = mutableListOf<File>()
5756
apiDependencies.forEach { dep ->
5857
when (dep) {

src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/Utils.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
package com.github.jengelman.gradle.plugins.shadow.internal
22

33
import java.io.InputStream
4+
import org.gradle.api.Project
5+
import org.gradle.api.artifacts.Configuration
6+
import org.gradle.api.plugins.JavaPlugin
7+
8+
/**
9+
* Return `runtimeClasspath` or `runtime` configuration.
10+
*/
11+
internal inline val Project.runtimeConfiguration: Configuration get() {
12+
return configurations.findByName(JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME)
13+
?: configurations.getByName("runtime")
14+
}
15+
16+
@Suppress("NOTHING_TO_INLINE")
17+
internal inline fun <T> unsafeLazy(noinline initializer: () -> T): Lazy<T> =
18+
lazy(LazyThreadSafetyMode.NONE, initializer)
419

520
internal fun Class<*>.requireResourceAsText(name: String): String {
621
return requireResourceAsStream(name).bufferedReader().readText()

0 commit comments

Comments
 (0)