Skip to content

Commit d5e3cec

Browse files
authored
Fix lint errors and cleanups (#1043)
* Inline UnusedTracker.toMinimize * Optimize jarFile init * Fix SimpleDateFormat usage * Use configureEach * Suppress WithTypeWithoutConfigureEach for ShadowPlugin This reverts commit d1d3e80. * Use map for inheritFrom * Suppress EagerGradleConfiguration for inheritFrom This reverts commit 0479a33. * Remove redundant getOrElse * Revert "Remove redundant getOrElse" This reverts commit 1fa1557. * Replace mainClassName with mainClass * Replace deprecated FileCopyDetails.setMode * Rename configure * Clean up SimpleRelocator * Rearrange createCopyAction * Add JvmDefaultWithCompatibility for Transformer * Tweak style
1 parent 363a3dc commit d5e3cec

File tree

10 files changed

+45
-103
lines changed

10 files changed

+45
-103
lines changed

api/shadow.api

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ public final class com/github/jengelman/gradle/plugins/shadow/relocation/Relocat
181181
}
182182

183183
public class com/github/jengelman/gradle/plugins/shadow/relocation/SimpleRelocator : com/github/jengelman/gradle/plugins/shadow/relocation/Relocator {
184+
public fun <init> (Ljava/lang/String;Ljava/lang/String;)V
185+
public fun <init> (Ljava/lang/String;Ljava/lang/String;Ljava/util/List;)V
184186
public fun <init> (Ljava/lang/String;Ljava/lang/String;Ljava/util/List;Ljava/util/List;)V
185187
public fun <init> (Ljava/lang/String;Ljava/lang/String;Ljava/util/List;Ljava/util/List;Z)V
186188
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/String;Ljava/util/List;Ljava/util/List;ZILkotlin/jvm/internal/DefaultConstructorMarker;)V
@@ -553,6 +555,10 @@ public abstract interface class com/github/jengelman/gradle/plugins/shadow/trans
553555
public abstract fun transform (Lcom/github/jengelman/gradle/plugins/shadow/transformers/TransformerContext;)V
554556
}
555557

558+
public final class com/github/jengelman/gradle/plugins/shadow/transformers/Transformer$DefaultImpls {
559+
public static fun getName (Lcom/github/jengelman/gradle/plugins/shadow/transformers/Transformer;)Ljava/lang/String;
560+
}
561+
556562
public final class com/github/jengelman/gradle/plugins/shadow/transformers/TransformerContext {
557563
public static final field Companion Lcom/github/jengelman/gradle/plugins/shadow/transformers/TransformerContext$Companion;
558564
public fun <init> (Ljava/lang/String;Ljava/io/InputStream;)V

lint-baseline.xml

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<issues format="6" by="lint 8.7.2" type="baseline" client="gradle" dependencies="false" name="AGP (8.7.2)" variant="all" version="8.7.2">
33

4-
<issue
5-
id="SimpleDateFormat"
6-
message="To get local formatting use `getDateInstance()`, `getDateTimeInstance()`, or `getTimeInstance()`, or use `new SimpleDateFormat(String template, Locale locale)` with for example `Locale.US` for ASCII dates."
7-
errorLine1=" val year = SimpleDateFormat(&quot;yyyy&quot;).format(Date()).let {"
8-
errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~">
9-
<location
10-
file="src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/transformers/ApacheNoticeResourceTransformer.kt"
11-
line="75"
12-
column="18"/>
13-
</issue>
14-
15-
<issue
16-
id="EagerGradleConfiguration"
17-
message="Avoid using method get"
18-
errorLine1=" project.file(&quot;${install.get().destinationDir.path}/lib/${shadowJar.get().archiveFile.get().asFile.name}&quot;)"
19-
errorLine2=" ~~~">
20-
<location
21-
file="src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/ShadowApplicationPlugin.kt"
22-
line="54"
23-
column="35"/>
24-
</issue>
25-
26-
<issue
27-
id="EagerGradleConfiguration"
28-
message="Avoid using method get"
29-
errorLine1=" project.file(&quot;${install.get().destinationDir.path}/lib/${shadowJar.get().archiveFile.get().asFile.name}&quot;)"
30-
errorLine2=" ~~~">
31-
<location
32-
file="src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/ShadowApplicationPlugin.kt"
33-
line="54"
34-
column="78"/>
35-
</issue>
36-
37-
<issue
38-
id="EagerGradleConfiguration"
39-
message="Avoid using method get"
40-
errorLine1=" shadow.manifest.inheritFrom(jarTask.get().manifest)"
41-
errorLine2=" ~~~">
42-
<location
43-
file="src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/ShadowJavaPlugin.kt"
44-
line="81"
45-
column="43"/>
46-
</issue>
47-
484
<issue
495
id="InternalGradleApiUsage"
506
message="Avoid using internal Gradle APIs"
@@ -210,26 +166,4 @@
210166
column="1"/>
211167
</issue>
212168

213-
<issue
214-
id="WithTypeWithoutConfigureEach"
215-
message="Avoid passing a closure to withType, use withType().configureEach instead"
216-
errorLine1=" plugins.withType(JavaPlugin::class.java) {"
217-
errorLine2=" ~~~~~~~~">
218-
<location
219-
file="src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/ShadowPlugin.kt"
220-
line="14"
221-
column="15"/>
222-
</issue>
223-
224-
<issue
225-
id="WithTypeWithoutConfigureEach"
226-
message="Avoid passing a closure to withType, use withType().configureEach instead"
227-
errorLine1=" plugins.withType(ApplicationPlugin::class.java) {"
228-
errorLine2=" ~~~~~~~~">
229-
<location
230-
file="src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/ShadowPlugin.kt"
231-
line="17"
232-
column="15"/>
233-
</issue>
234-
235169
</issues>

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ public abstract class ShadowApplicationPlugin : Plugin<Project> {
5050
it.group = ApplicationPlugin.APPLICATION_GROUP
5151
it.conventionMapping.map("jvmArgs") { javaApplication.applicationDefaultJvmArgs }
5252
it.jarFile.fileProvider(
53-
project.providers.provider {
54-
project.file("${install.get().destinationDir.path}/lib/${shadowJar.get().archiveFile.get().asFile.name}")
53+
install.zip(shadowJar) { i, s ->
54+
project.file("${i.destinationDir.path}/lib/${s.archiveFile.get().asFile.name}")
5555
},
5656
)
5757
val toolchain = project.extensions.getByType(JavaPluginExtension::class.java).toolchain
@@ -70,7 +70,7 @@ public abstract class ShadowApplicationPlugin : Plugin<Project> {
7070
it.description = "Creates OS specific scripts to run the project as a JVM application using the shadow jar"
7171
it.group = ApplicationPlugin.APPLICATION_GROUP
7272
it.classpath = project.files(shadowJar)
73-
it.conventionMapping.map("mainClassName") { javaApplication.mainClass.get() }
73+
it.mainClass.set(javaApplication.mainClass)
7474
it.conventionMapping.map("applicationName") { javaApplication.applicationName }
7575
it.conventionMapping.map("outputDir") { project.layout.buildDirectory.dir("scriptsShadow").get().asFile }
7676
it.conventionMapping.map("defaultJvmOpts") { javaApplication.applicationDefaultJvmArgs }
@@ -100,7 +100,7 @@ public abstract class ShadowApplicationPlugin : Plugin<Project> {
100100
task.doLast {
101101
task.eachFile {
102102
if (it.path == "bin/${applicationName.get()}") {
103-
it.mode = 0x755
103+
it.permissions { permissions -> permissions.unix(755) }
104104
}
105105
}
106106
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public abstract class ShadowJavaPlugin @Inject constructor(
7878
shadow.group = ShadowBasePlugin.GROUP_NAME
7979
shadow.description = "Create a combined JAR of project and runtime dependencies"
8080
shadow.archiveClassifier.set("all")
81+
@Suppress("EagerGradleConfiguration")
8182
shadow.manifest.inheritFrom(jarTask.get().manifest)
8283
val attrProvider = jarTask.map { it.manifest.attributes["Class-Path"]?.toString().orEmpty() }
8384
val files = project.objects.fileCollection().from(shadowConfiguration)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ public abstract class ShadowPlugin : Plugin<Project> {
1111
override fun apply(project: Project) {
1212
project.run {
1313
plugins.apply(ShadowBasePlugin::class.java)
14+
@Suppress("WithTypeWithoutConfigureEach")
1415
plugins.withType(JavaPlugin::class.java) {
1516
plugins.apply(ShadowJavaPlugin::class.java)
1617
}
18+
@Suppress("WithTypeWithoutConfigureEach")
1719
plugins.withType(ApplicationPlugin::class.java) {
1820
plugins.apply(ShadowApplicationPlugin::class.java)
1921
}

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import org.vafer.jdependency.ClazzpathUnit
1515
internal class UnusedTracker private constructor(
1616
classDirs: Iterable<File>,
1717
classJars: FileCollection,
18-
private val _toMinimize: FileCollection,
18+
@get:InputFiles val toMinimize: FileCollection,
1919
) {
2020
private val projectUnits: List<ClazzpathUnit>
2121
private val cp = Clazzpath()
@@ -24,9 +24,6 @@ internal class UnusedTracker private constructor(
2424
projectUnits = classDirs.map { cp.addClazzpathUnit(it) } + classJars.map { cp.addClazzpathUnit(it) }
2525
}
2626

27-
@get:InputFiles
28-
val toMinimize: FileCollection get() = _toMinimize
29-
3027
fun findUnused(): Set<String> {
3128
val unused = cp.clazzes.toMutableSet()
3229
for (cpu in projectUnits) {
@@ -37,7 +34,7 @@ internal class UnusedTracker private constructor(
3734
}
3835

3936
fun addDependency(jarOrDir: File) {
40-
if (_toMinimize.contains(jarOrDir)) {
37+
if (toMinimize.contains(jarOrDir)) {
4138
cp.addClazzpathUnit(jarOrDir)
4239
}
4340
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ import org.gradle.api.tasks.Optional
1616
public open class SimpleRelocator @JvmOverloads constructor(
1717
pattern: String?,
1818
shadedPattern: String?,
19-
includes: List<String>?,
20-
excludes: List<String>?,
19+
includes: List<String>? = null,
20+
excludes: List<String>? = null,
2121
private val _isRawString: Boolean = false,
2222
) : Relocator {
2323
private val _pattern: String?

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

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -198,28 +198,6 @@ public abstract class ShadowJar :
198198
action?.execute(dependencyFilterForMinimize)
199199
}
200200

201-
override fun createCopyAction(): CopyAction {
202-
val documentationRegistry = services.get(DocumentationRegistry::class.java)
203-
val unusedTracker = if (minimizeJar) {
204-
UnusedTracker.forProject(apiJars, sourceSetsClassesDirs.files, toMinimize)
205-
} else {
206-
null
207-
}
208-
return ShadowCopyAction(
209-
archiveFile.get().asFile,
210-
internalCompressor,
211-
documentationRegistry,
212-
metadataCharset,
213-
_transformers,
214-
_relocators,
215-
rootPatternSet,
216-
_stats,
217-
isPreserveFileTimestamps,
218-
minimizeJar,
219-
unusedTracker,
220-
)
221-
}
222-
223201
override fun dependencies(action: Action<DependencyFilter>?): ShadowJar = apply {
224202
action?.execute(_dependencyFilter)
225203
}
@@ -280,7 +258,7 @@ public abstract class ShadowJar :
280258
destination: String,
281259
action: Action<SimpleRelocator>?,
282260
): ShadowJar = apply {
283-
val relocator = SimpleRelocator(pattern, destination, mutableListOf(), mutableListOf())
261+
val relocator = SimpleRelocator(pattern, destination)
284262
addRelocator(relocator, action)
285263
}
286264

@@ -307,8 +285,30 @@ public abstract class ShadowJar :
307285
logger.info(_stats.toString())
308286
}
309287

310-
private fun <R : Relocator> addRelocator(relocator: R, configure: Action<R>?) {
311-
configure?.execute(relocator)
288+
override fun createCopyAction(): CopyAction {
289+
val documentationRegistry = services.get(DocumentationRegistry::class.java)
290+
val unusedTracker = if (minimizeJar) {
291+
UnusedTracker.forProject(apiJars, sourceSetsClassesDirs.files, toMinimize)
292+
} else {
293+
null
294+
}
295+
return ShadowCopyAction(
296+
archiveFile.get().asFile,
297+
internalCompressor,
298+
documentationRegistry,
299+
metadataCharset,
300+
_transformers,
301+
_relocators,
302+
rootPatternSet,
303+
_stats,
304+
isPreserveFileTimestamps,
305+
minimizeJar,
306+
unusedTracker,
307+
)
308+
}
309+
310+
private fun <R : Relocator> addRelocator(relocator: R, action: Action<R>?) {
311+
action?.execute(relocator)
312312
_relocators.add(relocator)
313313
}
314314

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import java.io.PrintWriter
44
import java.nio.charset.Charset
55
import java.text.SimpleDateFormat
66
import java.util.Date
7+
import java.util.Locale
78
import java.util.TreeSet
89
import org.apache.tools.zip.ZipEntry
910
import org.apache.tools.zip.ZipOutputStream
@@ -74,7 +75,7 @@ public open class ApacheNoticeResourceTransformer : Transformer {
7475

7576
override fun transform(context: TransformerContext) {
7677
if (entries.isEmpty()) {
77-
val year = SimpleDateFormat("yyyy").format(Date()).let {
78+
val year = SimpleDateFormat("yyyy", Locale.US).format(Date()).let {
7879
if (inceptionYear != it) "$inceptionYear-$it" else it
7980
}
8081
// add headers

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import org.gradle.api.tasks.Internal
1212
* @author Charlie Knudsen
1313
* @author John Engelman
1414
*/
15+
@JvmDefaultWithCompatibility
1516
public interface Transformer : Named {
1617
public fun canTransformResource(element: FileTreeElement): Boolean
1718

0 commit comments

Comments
 (0)