Skip to content

Commit bee8002

Browse files
committed
Apply a specific rule to exclude JUnit
This avoids excluding junit from the test classpath, even if explicitly added by a downstream user, which doesn't make sense, and allows people to add junit to the main classpath if they need to for some reason.
1 parent de15167 commit bee8002

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

paperweight-userdev/src/main/kotlin/io/papermc/paperweight/userdev/PaperweightUser.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import io.papermc.paperweight.DownloadService
2626
import io.papermc.paperweight.PaperweightException
2727
import io.papermc.paperweight.tasks.*
2828
import io.papermc.paperweight.userdev.attribute.Obfuscation
29+
import io.papermc.paperweight.userdev.internal.JunitExclusionRule
2930
import io.papermc.paperweight.userdev.internal.setup.SetupHandler
3031
import io.papermc.paperweight.userdev.internal.setup.UserdevSetup
3132
import io.papermc.paperweight.userdev.internal.setup.util.*
@@ -162,6 +163,10 @@ abstract class PaperweightUser : Plugin<Project> {
162163
}
163164
}
164165

166+
if (userdev.applyJunitExclusionRule.get()) {
167+
applyJunitExclusionRule()
168+
}
169+
165170
// Print a friendly error message if the dev bundle is missing before we call anything else that will try and resolve it
166171
checkForDevBundle()
167172

@@ -171,6 +176,12 @@ abstract class PaperweightUser : Plugin<Project> {
171176
}
172177
}
173178

179+
private fun Project.applyJunitExclusionRule() = dependencies {
180+
components {
181+
withModule<JunitExclusionRule>("com.googlecode.json-simple:json-simple")
182+
}
183+
}
184+
174185
private fun Project.configureRepositories(userdevSetup: UserdevSetup) = repositories {
175186
maven(userdevSetup.paramMappings.url) {
176187
name = PARAM_MAPPINGS_REPO_NAME
@@ -235,7 +246,6 @@ abstract class PaperweightUser : Plugin<Project> {
235246
}
236247

237248
val mojangMappedServerConfig = target.configurations.register(MOJANG_MAPPED_SERVER_CONFIG) {
238-
exclude("junit", "junit") // json-simple exposes junit for some reason
239249
defaultDependencies {
240250
val ctx = createContext(target)
241251
userdevSetup.get().let { setup ->

paperweight-userdev/src/main/kotlin/io/papermc/paperweight/userdev/PaperweightUserExtension.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ abstract class PaperweightUserExtension(
5050
*/
5151
val injectPaperRepository: Property<Boolean> = objects.property<Boolean>().convention(true)
5252

53+
/**
54+
* Whether to patch dependencies to exclude `junit:junit` from the transitive dependencies.
55+
*
56+
* True by default to avoid `junit:junit` appearing on the `compileClasspath`.
57+
*/
58+
val applyJunitExclusionRule: Property<Boolean> = objects.property<Boolean>().convention(true)
59+
5360
/**
5461
* The [ReobfArtifactConfiguration] is responsible for setting the input and output jars for `reobfJar`,
5562
* as well as changing the classifiers of other jars (i.e. `jar` or `shadowJar`).
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* paperweight is a Gradle plugin for the PaperMC project.
3+
*
4+
* Copyright (c) 2023 Kyle Wood (DenWav)
5+
* Contributors
6+
*
7+
* This library is free software; you can redistribute it and/or
8+
* modify it under the terms of the GNU Lesser General Public
9+
* License as published by the Free Software Foundation;
10+
* version 2.1 only, no later versions.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this library; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
20+
* USA
21+
*/
22+
23+
package io.papermc.paperweight.userdev.internal
24+
25+
import org.gradle.api.artifacts.CacheableRule
26+
import org.gradle.api.artifacts.ComponentMetadataContext
27+
import org.gradle.api.artifacts.ComponentMetadataRule
28+
29+
/**
30+
* Excludes `junit:junit` from the transitive dependencies of all variants.
31+
*/
32+
@CacheableRule
33+
abstract class JunitExclusionRule : ComponentMetadataRule {
34+
override fun execute(ctx: ComponentMetadataContext) {
35+
ctx.details.allVariants {
36+
withDependencies {
37+
removeIf {
38+
it.group == "junit" && it.name == "junit"
39+
}
40+
}
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)