diff --git a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/GradleCompat.kt b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/GradleCompat.kt index cf60ff126..0061d60e7 100644 --- a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/GradleCompat.kt +++ b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/GradleCompat.kt @@ -6,7 +6,10 @@ import org.gradle.api.artifacts.ProjectDependency import org.gradle.api.file.ConfigurableFileCollection import org.gradle.api.model.ObjectFactory import org.gradle.api.plugins.JavaPlugin +import org.gradle.api.provider.MapProperty import org.gradle.api.provider.Property +import org.gradle.api.provider.Provider +import org.gradle.api.provider.SetProperty import org.gradle.util.GradleVersion /** @@ -18,21 +21,53 @@ internal inline val Project.runtimeConfiguration: Configuration ?: configurations.getByName("runtime") } -internal inline fun ObjectFactory.property(defaultValue: T? = null): Property { - return property(T::class.java).apply { - if (defaultValue != null) convention(defaultValue) +internal inline fun ObjectFactory.property( + defaultValue: Any? = null, +): Property = property(V::class.java).apply { + defaultValue ?: return@apply + if (defaultValue is Provider<*>) { + @Suppress("UNCHECKED_CAST") + convention(defaultValue as Provider) + } else { + convention(defaultValue as V) + } +} + +@Suppress("UNCHECKED_CAST") +internal inline fun ObjectFactory.setProperty( + defaultValue: Any? = null, +): SetProperty = setProperty(V::class.java).apply { + defaultValue ?: return@apply + if (defaultValue is Provider<*>) { + convention(defaultValue as Provider>) + } else { + convention(defaultValue as Iterable) + } +} + +@Suppress("UNCHECKED_CAST") +internal inline fun ObjectFactory.mapProperty( + defaultValue: Any? = null, +): MapProperty = mapProperty(String::class.java, V::class.java).apply { + defaultValue ?: return@apply + if (defaultValue is Provider<*>) { + convention(defaultValue as Provider>) + } else { + convention(defaultValue as Map) } } /** * TODO: this could be removed after bumping the min Gradle requirement to 8.8 or above. */ -internal fun ConfigurableFileCollection.conventionCompat(vararg paths: Any): ConfigurableFileCollection { - return if (GradleVersion.current() >= GradleVersion.version("8.8")) { - convention(paths) +internal inline fun ObjectFactory.fileCollection( + path: () -> Any, +): ConfigurableFileCollection = fileCollection().apply { + @Suppress("UnstableApiUsage") + if (GradleVersion.current() >= GradleVersion.version("8.8")) { + convention(path()) } else { - setFrom(paths) - this + setFrom(path()) } } diff --git a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/Utils.kt b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/Utils.kt index 4ec58607b..daac5eae8 100644 --- a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/Utils.kt +++ b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/Utils.kt @@ -26,11 +26,6 @@ internal fun createDefaultFileTreeElement( return DefaultFileTreeElement(file, relativePath, chmod, stat) } -@Suppress("NOTHING_TO_INLINE") -internal inline fun unsafeLazy(noinline initializer: () -> T): Lazy { - return lazy(LazyThreadSafetyMode.NONE, initializer) -} - internal fun Properties.inputStream( charset: Charset = Charsets.ISO_8859_1, comments: String = "", diff --git a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/relocation/SimpleRelocator.kt b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/relocation/SimpleRelocator.kt index 78837b622..9057eedc2 100644 --- a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/relocation/SimpleRelocator.kt +++ b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/relocation/SimpleRelocator.kt @@ -1,5 +1,6 @@ package com.github.jengelman.gradle.plugins.shadow.relocation +import com.github.jengelman.gradle.plugins.shadow.internal.setProperty import java.util.regex.Pattern import org.codehaus.plexus.util.SelectorUtils import org.gradle.api.model.ObjectFactory @@ -30,10 +31,10 @@ public open class SimpleRelocator @JvmOverloads constructor( private val sourcePathExcludes = mutableSetOf() @get:Input - public val includes: SetProperty = objectFactory.setProperty(String::class.java) + public val includes: SetProperty = objectFactory.setProperty() @get:Input - public val excludes: SetProperty = objectFactory.setProperty(String::class.java) + public val excludes: SetProperty = objectFactory.setProperty() init { if (rawString) { diff --git a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar.kt b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar.kt index b31bcc10c..77fe69a6c 100644 --- a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar.kt +++ b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar.kt @@ -8,9 +8,9 @@ import com.github.jengelman.gradle.plugins.shadow.internal.DependencyFilter import com.github.jengelman.gradle.plugins.shadow.internal.MinimizeDependencyFilter import com.github.jengelman.gradle.plugins.shadow.internal.UnusedTracker import com.github.jengelman.gradle.plugins.shadow.internal.ZipCompressor -import com.github.jengelman.gradle.plugins.shadow.internal.conventionCompat +import com.github.jengelman.gradle.plugins.shadow.internal.fileCollection import com.github.jengelman.gradle.plugins.shadow.internal.property -import com.github.jengelman.gradle.plugins.shadow.internal.unsafeLazy +import com.github.jengelman.gradle.plugins.shadow.internal.setProperty import com.github.jengelman.gradle.plugins.shadow.relocation.CacheableRelocator import com.github.jengelman.gradle.plugins.shadow.relocation.Relocator import com.github.jengelman.gradle.plugins.shadow.relocation.SimpleRelocator @@ -20,6 +20,7 @@ import com.github.jengelman.gradle.plugins.shadow.transformers.GroovyExtensionMo import com.github.jengelman.gradle.plugins.shadow.transformers.ServiceFileTransformer import com.github.jengelman.gradle.plugins.shadow.transformers.Transformer import com.github.jengelman.gradle.plugins.shadow.transformers.Transformer.Companion.create +import java.io.File import java.util.jar.JarFile import org.apache.tools.zip.ZipOutputStream import org.gradle.api.Action @@ -68,32 +69,37 @@ public abstract class ShadowJar : @get:Internal override val stats: ShadowStats = ShadowStats() + /** + * Minimize the jar by removing unused classes. + * + * Defaults to `false`. + */ + @get:Input + public open val minimizeJar: Property = objectFactory.property(false) + @get:Classpath - public open val toMinimize: ConfigurableFileCollection by unsafeLazy { - objectFactory.fileCollection().apply { - if (minimizeJar.get()) { - conventionCompat(dependencyFilterForMinimize.resolve(configurations.get()) - apiJars) - } + public open val toMinimize: ConfigurableFileCollection = objectFactory.fileCollection { + minimizeJar.map { + if (it) (dependencyFilterForMinimize.resolve(configurations.get()) - apiJars) else emptySet() } } @get:Classpath - public open val apiJars: ConfigurableFileCollection by unsafeLazy { - objectFactory.fileCollection().apply { - if (minimizeJar.get()) { - conventionCompat(UnusedTracker.getApiJarsFromProject(project)) - } + public open val apiJars: ConfigurableFileCollection = objectFactory.fileCollection { + minimizeJar.map { + if (it) UnusedTracker.getApiJarsFromProject(project) else emptySet() } } @get:InputFiles @get:PathSensitive(PathSensitivity.RELATIVE) - public open val sourceSetsClassesDirs: ConfigurableFileCollection by unsafeLazy { - objectFactory.fileCollection().apply { - if (minimizeJar.get()) { - project.extensions.getByType(SourceSetContainer::class.java).forEach { sourceSet -> - from(sourceSet.output.classesDirs.filter { it.isDirectory }) - } + public open val sourceSetsClassesDirs: ConfigurableFileCollection = objectFactory.fileCollection { + minimizeJar.map { + if (it) { + project.extensions.getByType(SourceSetContainer::class.java) + .map { sourceSet -> sourceSet.output.classesDirs.filter(File::isDirectory) } + } else { + emptySet() } } } @@ -113,22 +119,23 @@ public abstract class ShadowJar : } @get:Nested - public open val transformers: SetProperty = objectFactory.setProperty(Transformer::class.java) + public open val transformers: SetProperty = objectFactory.setProperty() @get:Nested - public open val relocators: SetProperty = objectFactory.setProperty(Relocator::class.java) + public open val relocators: SetProperty = objectFactory.setProperty() @get:Classpath @get:Optional - public open val configurations: SetProperty = objectFactory.setProperty(Configuration::class.java) + public open val configurations: SetProperty = objectFactory.setProperty() @get:Internal public open val dependencyFilter: Property = objectFactory.property(DefaultDependencyFilter(project)) @get:Classpath - public open val includedDependencies: ConfigurableFileCollection = objectFactory.fileCollection() - .conventionCompat(dependencyFilter.map { it.resolve(configurations.get()) }) + public open val includedDependencies: ConfigurableFileCollection = objectFactory.fileCollection { + dependencyFilter.zip(configurations) { df, cs -> df.resolve(cs) } + } /** * Enable relocation of packages in the jar. @@ -146,14 +153,6 @@ public abstract class ShadowJar : @get:Input public open val relocationPrefix: Property = objectFactory.property(ShadowBasePlugin.SHADOW) - /** - * Minimize the jar by removing unused classes. - * - * Defaults to `false`. - */ - @get:Input - public open val minimizeJar: Property = objectFactory.property(false) - @Internal override fun getManifest(): InheritManifest = super.manifest as InheritManifest diff --git a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ManifestAppenderTransformer.kt b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ManifestAppenderTransformer.kt index 230058cfd..f8bf89614 100644 --- a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ManifestAppenderTransformer.kt +++ b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ManifestAppenderTransformer.kt @@ -1,5 +1,6 @@ package com.github.jengelman.gradle.plugins.shadow.transformers +import com.github.jengelman.gradle.plugins.shadow.internal.setProperty import java.io.ByteArrayOutputStream import java.io.IOException import java.util.jar.JarFile.MANIFEST_NAME @@ -25,10 +26,8 @@ public open class ManifestAppenderTransformer @Inject constructor( ) : Transformer { private var manifestContents = ByteArray(0) - @Suppress("UNCHECKED_CAST") @get:Input - public open val attributes: SetProperty>> = - objectFactory.setProperty(Pair::class.java) as SetProperty>> + public open val attributes: SetProperty>> = objectFactory.setProperty() override fun canTransformResource(element: FileTreeElement): Boolean { return MANIFEST_NAME.equals(element.relativePath.pathString, ignoreCase = true) diff --git a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ManifestResourceTransformer.kt b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ManifestResourceTransformer.kt index c63fde158..1de0158ef 100644 --- a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ManifestResourceTransformer.kt +++ b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ManifestResourceTransformer.kt @@ -1,5 +1,6 @@ package com.github.jengelman.gradle.plugins.shadow.transformers +import com.github.jengelman.gradle.plugins.shadow.internal.mapProperty import com.github.jengelman.gradle.plugins.shadow.internal.property import com.github.jengelman.gradle.plugins.shadow.transformers.TransformerContext.Companion.getEntryTimestamp import java.io.IOException @@ -39,8 +40,7 @@ public open class ManifestResourceTransformer @Inject constructor( @get:Optional @get:Input - public open val manifestEntries: MapProperty = - objectFactory.mapProperty(String::class.java, Attributes::class.java) + public open val manifestEntries: MapProperty = objectFactory.mapProperty() override fun canTransformResource(element: FileTreeElement): Boolean { val path = element.relativePath.pathString diff --git a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/PropertiesFileTransformer.kt b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/PropertiesFileTransformer.kt index 57be7a405..2950715ae 100644 --- a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/PropertiesFileTransformer.kt +++ b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/PropertiesFileTransformer.kt @@ -2,7 +2,9 @@ package com.github.jengelman.gradle.plugins.shadow.transformers import com.github.jengelman.gradle.plugins.shadow.internal.CleanProperties import com.github.jengelman.gradle.plugins.shadow.internal.inputStream +import com.github.jengelman.gradle.plugins.shadow.internal.mapProperty import com.github.jengelman.gradle.plugins.shadow.internal.property +import com.github.jengelman.gradle.plugins.shadow.internal.setProperty import com.github.jengelman.gradle.plugins.shadow.transformers.PropertiesFileTransformer.MergeStrategy import groovy.lang.Closure import groovy.lang.Closure.IDENTITY @@ -107,12 +109,10 @@ public open class PropertiesFileTransformer @Inject constructor( internal val propertiesEntries = mutableMapOf() @get:Input - public open val paths: SetProperty = objectFactory.setProperty(String::class.java) + public open val paths: SetProperty = objectFactory.setProperty() - @Suppress("UNCHECKED_CAST") @get:Input - public open val mappings: MapProperty> = - objectFactory.mapProperty(String::class.java, Map::class.java) as MapProperty> + public open val mappings: MapProperty> = objectFactory.mapProperty() @get:Input public open val mergeStrategy: Property = objectFactory.property(MergeStrategy.First) @@ -123,10 +123,8 @@ public open class PropertiesFileTransformer @Inject constructor( @get:Input public open val charsetName: Property = objectFactory.property(Charsets.ISO_8859_1.name()) - @Suppress("UNCHECKED_CAST") @get:Internal - public open val keyTransformer: Property> = - objectFactory.property(IDENTITY) as Property> + public open val keyTransformer: Property> = objectFactory.property(IDENTITY) override fun canTransformResource(element: FileTreeElement): Boolean { val mappings = mappings.get()