Skip to content

Commit 43dfef4

Browse files
authored
refactor: streamline resource loading with try-with-resources in plugin classes (#1025)
1 parent 329b890 commit 43dfef4

57 files changed

Lines changed: 455 additions & 552 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/build.yml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,17 @@ jobs:
6969
# only publish once
7070
if: github.ref == 'refs/heads/master' && matrix.java == '21' && matrix.storageType == 'mariadb'
7171
env:
72-
ORG_GRADLE_PROJECT_sonatypeUsername: ${{ secrets.ORG_GRADLE_PROJECT_SONATYPEUSERNAME }}
73-
ORG_GRADLE_PROJECT_sonatypePassword: ${{ secrets.ORG_GRADLE_PROJECT_SONATYPEPASSWORD }}
74-
ORG_GRADLE_PROJECT_signingKey: ${{ secrets.ORG_GRADLE_PROJECT_SIGNINGINMEMORYKEY }}
75-
ORG_GRADLE_PROJECT_signingPassword: ${{ secrets.ORG_GRADLE_PROJECT_SIGNINGINMEMORYKEYPASSWORD }}
72+
# vanniktech plugin credential names
73+
ORG_GRADLE_PROJECT_mavenCentralUsername: ${{ secrets.ORG_GRADLE_PROJECT_SONATYPEUSERNAME }}
74+
ORG_GRADLE_PROJECT_mavenCentralPassword: ${{ secrets.ORG_GRADLE_PROJECT_SONATYPEPASSWORD }}
75+
ORG_GRADLE_PROJECT_signingInMemoryKey: ${{ secrets.ORG_GRADLE_PROJECT_SIGNINGINMEMORYKEY }}
76+
ORG_GRADLE_PROJECT_signingInMemoryKeyPassword: ${{ secrets.ORG_GRADLE_PROJECT_SIGNINGINMEMORYKEYPASSWORD }}
7677
run: |
7778
VERSION=$(grep "^version=" gradle.properties | cut -d'=' -f2)
7879
if [[ "$VERSION" == *"SNAPSHOT"* ]]; then
79-
# SNAPSHOT: publish to snapshot repository (no staging/release)
80-
./gradlew publishToSonatype
80+
# SNAPSHOT: publish to Central Portal snapshot repository
81+
./gradlew publishToMavenCentral
8182
else
82-
# Release: publish, stage, and release to Maven Central
83-
./gradlew publishToSonatype closeAndReleaseSonatypeStagingRepository
83+
# Release: publish and release to Maven Central
84+
./gradlew publishAndReleaseToMavenCentral
8485
fi

build.gradle.kts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,5 @@ logger.lifecycle("""
1111
""")
1212

1313
plugins {
14-
id("io.github.gradle-nexus.publish-plugin") version "2.0.0"
1514
id("io.freefair.aggregate-javadoc") version "8.11"
1615
}
17-
18-
nexusPublishing {
19-
repositories {
20-
sonatype { // Central Portal (OSSRH sunset June 2025)
21-
nexusUrl.set(uri("https://central.sonatype.com/service/local/"))
22-
snapshotRepositoryUrl.set(uri("https://central.sonatype.com/repository/maven-snapshots/"))
23-
}
24-
}
25-
}

buildSrc/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ dependencies {
2222
implementation("org.jfrog.buildinfo:build-info-extractor-gradle:5.2.5")
2323
implementation("org.spongepowered:spongegradle-plugin-development:2.3.0")
2424
implementation("fabric-loom:fabric-loom.gradle.plugin:1.9.2")
25+
implementation("com.vanniktech:gradle-maven-publish-plugin:0.30.0")
2526
}

buildSrc/src/main/kotlin/LibsConfig.kt

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
2+
import com.vanniktech.maven.publish.MavenPublishBaseExtension
3+
import com.vanniktech.maven.publish.SonatypeHost
24
import org.gradle.api.Plugin
35
import org.gradle.api.Project
46
import org.gradle.api.artifacts.ModuleDependency
@@ -17,14 +19,13 @@ import org.gradle.api.publish.maven.MavenPublication
1719
import org.gradle.api.tasks.bundling.Jar
1820
import org.gradle.api.tasks.javadoc.Javadoc
1921
import org.gradle.external.javadoc.StandardJavadocDocletOptions
20-
import org.gradle.plugins.signing.*
2122
import org.gradle.kotlin.dsl.*
2223
import javax.inject.Inject
2324

2425
fun Project.applyLibrariesConfiguration() {
2526
apply(plugin = "java-base")
2627
apply(plugin = "maven-publish")
27-
apply(plugin = "signing")
28+
apply(plugin = "com.vanniktech.maven.publish")
2829
apply(plugin = "com.gradleup.shadow")
2930

3031
repositories {
@@ -179,15 +180,10 @@ fun Project.applyLibrariesConfiguration() {
179180
}
180181
}
181182

182-
configure<SigningExtension> {
183-
val pubExt = checkNotNull(extensions.findByType(PublishingExtension::class.java))
184-
val publication = pubExt.publications["maven"]
185-
186-
val signingKey = findProperty("signingKey")?.toString()
187-
if (!signingKey.isNullOrBlank()) {
188-
useInMemoryPgpKeys(signingKey, findProperty("signingPassword")?.toString())
189-
sign(publication)
190-
}
183+
// Configure vanniktech plugin for Central Portal publishing
184+
configure<MavenPublishBaseExtension> {
185+
publishToMavenCentral(SonatypeHost.CENTRAL_PORTAL)
186+
signAllPublications()
191187
}
192188
}
193189

bukkit/build.gradle.kts

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,47 @@
11
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
2+
import com.vanniktech.maven.publish.SonatypeHost
3+
import com.vanniktech.maven.publish.JavaLibrary
4+
import com.vanniktech.maven.publish.JavadocJar
25

36
plugins {
47
`java-library`
5-
`maven-publish`
6-
signing
8+
id("com.vanniktech.maven.publish")
79
}
810

911
applyPlatformAndCoreConfiguration()
1012
applyShadowConfiguration()
1113

12-
publishing {
13-
publications {
14-
create<MavenPublication>("mavenJava") {
15-
from(components["java"])
16-
17-
pom {
18-
name.set("BanManagerBukkit")
19-
description.set("BanManager for Bukkit")
20-
url.set("https://github.com/BanManagement/BanManager/")
21-
licenses {
22-
license {
23-
name.set("Creative Commons Attribution-NonCommercial-ShareAlike 2.0 UK: England & Wales")
24-
url.set("https://github.com/BanManagement/BanManager/blob/master/LICENCE")
25-
}
26-
}
27-
developers {
28-
developer {
29-
id.set("confuser>")
30-
name.set("James Mortemore")
31-
email.set("jamesmortemore@gmail.com")
32-
}
33-
}
34-
scm {
35-
connection.set("scm:git:git://github.com/BanManagement/BanManager.git")
36-
developerConnection.set("scm:git:ssh://git@github.com/BanManagement/BanManager.git")
37-
url.set("https://github.com/BanManagement/BanManager/")
38-
}
14+
mavenPublishing {
15+
publishToMavenCentral(SonatypeHost.CENTRAL_PORTAL)
16+
signAllPublications()
17+
18+
configure(JavaLibrary(
19+
javadocJar = JavadocJar.Javadoc(),
20+
sourcesJar = true
21+
))
22+
23+
pom {
24+
name.set("BanManagerBukkit")
25+
description.set("BanManager for Bukkit")
26+
url.set("https://github.com/BanManagement/BanManager/")
27+
licenses {
28+
license {
29+
name.set("Creative Commons Attribution-NonCommercial-ShareAlike 2.0 UK: England & Wales")
30+
url.set("https://github.com/BanManagement/BanManager/blob/master/LICENCE")
3931
}
4032
}
41-
}
42-
}
43-
44-
signing {
45-
val signingKey = findProperty("signingKey")?.toString()
46-
if (!signingKey.isNullOrBlank()) {
47-
useInMemoryPgpKeys(signingKey, findProperty("signingPassword")?.toString())
48-
sign(publishing.publications["mavenJava"])
33+
developers {
34+
developer {
35+
id.set("confuser")
36+
name.set("James Mortemore")
37+
email.set("jamesmortemore@gmail.com")
38+
}
39+
}
40+
scm {
41+
connection.set("scm:git:git://github.com/BanManagement/BanManager.git")
42+
developerConnection.set("scm:git:ssh://git@github.com/BanManagement/BanManager.git")
43+
url.set("https://github.com/BanManagement/BanManager/")
44+
}
4945
}
5046
}
5147

bukkit/src/main/java/me/confuser/banmanager/bukkit/BMBukkitPlugin.java

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818

1919
import java.io.File;
2020
import java.io.IOException;
21+
import java.io.InputStream;
2122
import java.io.InputStreamReader;
2223
import java.io.Reader;
24+
import java.nio.charset.StandardCharsets;
2325
import java.time.Duration;
2426

2527
public class BMBukkitPlugin extends JavaPlugin {
@@ -84,31 +86,34 @@ private PluginInfo setupConfigs() throws IOException {
8486
this.saveResource(name, false);
8587
} else {
8688
File file = new File(getDataFolder(), name);
87-
Reader defConfigStream = new InputStreamReader(getResource(file.getName()), "UTF8");
88-
89-
YamlConfiguration conf = YamlConfiguration.loadConfiguration(file);
90-
YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
91-
conf.setDefaults(defConfig);
92-
conf.options().copyDefaults(true);
93-
conf.save(file);
89+
try (InputStream in = getResource(file.getName());
90+
Reader defConfigStream = new InputStreamReader(in, StandardCharsets.UTF_8)) {
91+
YamlConfiguration conf = YamlConfiguration.loadConfiguration(file);
92+
YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
93+
conf.setDefaults(defConfig);
94+
conf.options().copyDefaults(true);
95+
conf.save(file);
96+
}
9497
}
9598
}
9699

97100
// Load plugin.yml
98101
PluginInfo pluginInfo = new PluginInfo();
99-
Reader defConfigStream = new InputStreamReader(getResource("plugin.yml"), "UTF8");
100-
YamlConfiguration conf = YamlConfiguration.loadConfiguration(defConfigStream);
101-
ConfigurationSection commands = conf.getConfigurationSection("commands");
102-
String pluginName = conf.getString("name");
103-
104-
if (!pluginName.equals("BanManager")) {
105-
throw new IOException("Unable to start BanManager as " + pluginName + " has broken resource loading forcing BanManager to load their plugin.yml file; please alert the author to resolve this issue");
106-
}
102+
try (InputStream in = getResource("plugin.yml");
103+
Reader defConfigStream = new InputStreamReader(in, StandardCharsets.UTF_8)) {
104+
YamlConfiguration conf = YamlConfiguration.loadConfiguration(defConfigStream);
105+
ConfigurationSection commands = conf.getConfigurationSection("commands");
106+
String pluginName = conf.getString("name");
107+
108+
if (!pluginName.equals("BanManager")) {
109+
throw new IOException("Unable to start BanManager as " + pluginName + " has broken resource loading forcing BanManager to load their plugin.yml file; please alert the author to resolve this issue");
110+
}
107111

108-
for (String command : commands.getKeys(false)) {
109-
ConfigurationSection cmd = commands.getConfigurationSection(command);
112+
for (String command : commands.getKeys(false)) {
113+
ConfigurationSection cmd = commands.getConfigurationSection(command);
110114

111-
pluginInfo.setCommand(new PluginInfo.CommandInfo(command, cmd.getString("permission"), cmd.getString("usage"), cmd.getStringList("aliases")));
115+
pluginInfo.setCommand(new PluginInfo.CommandInfo(command, cmd.getString("permission"), cmd.getString("usage"), cmd.getStringList("aliases")));
116+
}
112117
}
113118

114119
Bukkit.getScheduler().scheduleSyncDelayedTask(this, () -> {

bungee/build.gradle.kts

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,47 @@
11
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
2+
import com.vanniktech.maven.publish.SonatypeHost
3+
import com.vanniktech.maven.publish.JavaLibrary
4+
import com.vanniktech.maven.publish.JavadocJar
25

36
plugins {
47
`java-library`
5-
`maven-publish`
6-
signing
8+
id("com.vanniktech.maven.publish")
79
}
810

911
applyPlatformAndCoreConfiguration()
1012
applyShadowConfiguration()
1113

12-
publishing {
13-
publications {
14-
create<MavenPublication>("mavenJava") {
15-
from(components["java"])
16-
17-
pom {
18-
name.set("BanManagerBungee")
19-
description.set("BanManager for BungeeCord")
20-
url.set("https://github.com/BanManagement/BanManager/")
21-
licenses {
22-
license {
23-
name.set("Creative Commons Attribution-NonCommercial-ShareAlike 2.0 UK: England & Wales")
24-
url.set("https://github.com/BanManagement/BanManager/blob/master/LICENCE")
25-
}
26-
}
27-
developers {
28-
developer {
29-
id.set("confuser")
30-
name.set("James Mortemore")
31-
email.set("jamesmortemore@gmail.com")
32-
}
33-
}
34-
scm {
35-
connection.set("scm:git:git://github.com/BanManagement/BanManager.git")
36-
developerConnection.set("scm:git:ssh://git@github.com/BanManagement/BanManager.git")
37-
url.set("https://github.com/BanManagement/BanManager/")
38-
}
14+
mavenPublishing {
15+
publishToMavenCentral(SonatypeHost.CENTRAL_PORTAL)
16+
signAllPublications()
17+
18+
configure(JavaLibrary(
19+
javadocJar = JavadocJar.Javadoc(),
20+
sourcesJar = true
21+
))
22+
23+
pom {
24+
name.set("BanManagerBungee")
25+
description.set("BanManager for BungeeCord")
26+
url.set("https://github.com/BanManagement/BanManager/")
27+
licenses {
28+
license {
29+
name.set("Creative Commons Attribution-NonCommercial-ShareAlike 2.0 UK: England & Wales")
30+
url.set("https://github.com/BanManagement/BanManager/blob/master/LICENCE")
3931
}
4032
}
41-
}
42-
}
43-
44-
signing {
45-
val signingKey = findProperty("signingKey")?.toString()
46-
if (!signingKey.isNullOrBlank()) {
47-
useInMemoryPgpKeys(signingKey, findProperty("signingPassword")?.toString())
48-
sign(publishing.publications["mavenJava"])
33+
developers {
34+
developer {
35+
id.set("confuser")
36+
name.set("James Mortemore")
37+
email.set("jamesmortemore@gmail.com")
38+
}
39+
}
40+
scm {
41+
connection.set("scm:git:git://github.com/BanManagement/BanManager.git")
42+
developerConnection.set("scm:git:ssh://git@github.com/BanManagement/BanManager.git")
43+
url.set("https://github.com/BanManagement/BanManager/")
44+
}
4945
}
5046
}
5147

bungee/src/main/java/me/confuser/banmanager/bungee/BMBungeePlugin.java

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.bstats.bungeecord.Metrics;
1515

1616
import java.io.*;
17+
import java.nio.charset.StandardCharsets;
1718
import java.nio.file.Files;
1819
import java.time.Duration;
1920

@@ -117,31 +118,34 @@ private PluginInfo setupConfigs() throws IOException {
117118
e.printStackTrace();
118119
}
119120
} else {
120-
Reader defConfigStream = new InputStreamReader(getResourceAsStream(file.getName()), "UTF8");
121-
122-
YamlConfiguration conf = YamlConfiguration.loadConfiguration(file);
123-
YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
124-
conf.setDefaults(defConfig);
125-
conf.options().copyDefaults(true);
126-
conf.save(file);
121+
try (InputStream in = getResourceAsStream(file.getName());
122+
Reader defConfigStream = new InputStreamReader(in, StandardCharsets.UTF_8)) {
123+
YamlConfiguration conf = YamlConfiguration.loadConfiguration(file);
124+
YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
125+
conf.setDefaults(defConfig);
126+
conf.options().copyDefaults(true);
127+
conf.save(file);
128+
}
127129
}
128130
}
129131

130132
// Load plugin.yml
131133
PluginInfo pluginInfo = new PluginInfo();
132-
Reader defConfigStream = new InputStreamReader(getResourceAsStream("plugin.yml"), "UTF8");
133-
YamlConfiguration conf = YamlConfiguration.loadConfiguration(defConfigStream);
134-
ConfigurationSection commands = conf.getConfigurationSection("commands");
135-
String pluginName = conf.getString("name");
136-
137-
if (!pluginName.equals("BanManager")) {
138-
throw new IOException("Unable to start BanManager as " + pluginName + " has broken resource loading forcing BanManager to load their plugin.yml file; please alert the author to resolve this issue");
139-
}
134+
try (InputStream in = getResourceAsStream("plugin.yml");
135+
Reader defConfigStream = new InputStreamReader(in, StandardCharsets.UTF_8)) {
136+
YamlConfiguration conf = YamlConfiguration.loadConfiguration(defConfigStream);
137+
ConfigurationSection commands = conf.getConfigurationSection("commands");
138+
String pluginName = conf.getString("name");
139+
140+
if (!pluginName.equals("BanManager")) {
141+
throw new IOException("Unable to start BanManager as " + pluginName + " has broken resource loading forcing BanManager to load their plugin.yml file; please alert the author to resolve this issue");
142+
}
140143

141-
for (String command : commands.getKeys(false)) {
142-
ConfigurationSection cmd = commands.getConfigurationSection(command);
144+
for (String command : commands.getKeys(false)) {
145+
ConfigurationSection cmd = commands.getConfigurationSection(command);
143146

144-
pluginInfo.setCommand(new PluginInfo.CommandInfo(command, cmd.getString("permission"), cmd.getString("usage"), cmd.getStringList("aliases")));
147+
pluginInfo.setCommand(new PluginInfo.CommandInfo(command, cmd.getString("permission"), cmd.getString("usage"), cmd.getStringList("aliases")));
148+
}
145149
}
146150

147151
return pluginInfo;

0 commit comments

Comments
 (0)