Make gradle take build number from env vars and add git build info (#5890)

* Make gradle take build number from env vars

* Add git information to manifest and version command/log

* trim

* Fix tests by adding NotNull annotations

* rebase

* Apply suggestions from kashike

Co-authored-by: Riley Park <riley.park@meino.net>

* Not always show branch

* Why can't everything be NotNull by default?

* Rebase

Co-authored-by: Riley Park <riley.park@meino.net>
This commit is contained in:
Prof-Bloodstone 2021-06-23 19:19:44 +02:00 committed by GitHub
parent 4e2f0be270
commit d50cc01b08
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 193 additions and 8 deletions

View file

@ -0,0 +1,157 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Professor Bloodstone <git@bloodstone.dev>
Date: Sun, 20 Jun 2021 01:48:31 +0200
Subject: [PATCH] Add Git information to version command/on startup
diff --git a/src/main/java/io/papermc/paper/util/JarManifests.java b/src/main/java/io/papermc/paper/util/JarManifests.java
new file mode 100644
index 0000000000000000000000000000000000000000..02ad04c41b9c3ec68b1dcdc22c538340d8d3455b
--- /dev/null
+++ b/src/main/java/io/papermc/paper/util/JarManifests.java
@@ -0,0 +1,93 @@
+/*
+Copyright (c) 2012-2017, jcabi.com
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met: 1) Redistributions of source code must retain the above
+copyright notice, this list of conditions and the following
+disclaimer. 2) Redistributions in binary form must reproduce the above
+copyright notice, this list of conditions and the following
+disclaimer in the documentation and/or other materials provided
+with the distribution. 3) Neither the name of the jcabi.com nor
+the names of its contributors may be used to endorse or promote
+products derived from this software without specific prior written
+permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
+NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+package io.papermc.paper.util;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.*;
+import java.util.jar.Manifest;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Modified version of jcabi-manifests
+ *
+ */
+public final class JarManifests {
+
+ public static final JarManifests JAR_MANIFESTS = new JarManifests();
+ public static final Map<String, String> MANIFEST_MAP;
+
+ static {
+ try {
+ MANIFEST_MAP = Collections.unmodifiableMap(JAR_MANIFESTS.getManifestMap());
+ } catch (final IOException ex) {
+ throw new RuntimeException(ex);
+ }
+ }
+
+ private JarManifests() {}
+
+ // Based on:
+ // https://github.com/jcabi/jcabi-manifests/blob/c4e1dd22bb6099769b8d279ebe3737e5b638b278/src/main/java/com/jcabi/manifests/ClasspathMfs.java#L49-L58
+
+ /**
+ * Get collection containing all META-INF/MANIFEST.MF streams
+ *
+ * @throws IOException if unable to get resources from classloader
+ */
+ public @NotNull Collection<@NotNull InputStream> getManifestStreams() throws IOException {
+ final Enumeration<URL> resources = getClass().getClassLoader().getResources("META-INF/MANIFEST.MF");
+ final Collection<InputStream> streams = new ArrayList<>(1);
+ while (resources.hasMoreElements()) {
+ streams.add(resources.nextElement().openStream());
+ }
+ return streams;
+ }
+
+ // Based on:
+ // https://github.com/jcabi/jcabi-manifests/blob/c4e1dd22bb6099769b8d279ebe3737e5b638b278/src/main/java/com/jcabi/manifests/Manifests.java#L209-L225
+ // https://github.com/jcabi/jcabi-manifests/blob/c4e1dd22bb6099769b8d279ebe3737e5b638b278/src/main/java/com/jcabi/manifests/Manifests.java#L381-L388
+
+ /**
+ * Get map containing entries from all manifests
+ *
+ * @throws IOException if unable to get manifest streams
+ */
+ public @NotNull Map<@NotNull String, @NotNull String> getManifestMap() throws IOException {
+ final HashMap<String, String> attribs = new HashMap<>();
+ for (final InputStream stream : getManifestStreams()) {
+ for (final Map.Entry<Object, Object> attr: new Manifest(stream).getMainAttributes().entrySet()) {
+ attribs.put(attr.getKey().toString(), attr.getValue().toString());
+ }
+ }
+ return attribs;
+ }
+}
diff --git a/src/main/java/org/bukkit/Bukkit.java b/src/main/java/org/bukkit/Bukkit.java
index e8414592b3afeb1e5db2b817b8fb7c13e073b9aa..b5158151960db5c474b3aaa24bfa428e2a158fd9 100644
--- a/src/main/java/org/bukkit/Bukkit.java
+++ b/src/main/java/org/bukkit/Bukkit.java
@@ -50,6 +50,7 @@ import org.bukkit.util.CachedServerIcon;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
+import io.papermc.paper.util.JarManifests; // Paper
/**
* Represents the Bukkit core, for version and Server singleton handling
@@ -85,7 +86,25 @@ public final class Bukkit {
}
Bukkit.server = server;
- server.getLogger().info("This server is running " + getName() + " version " + getVersion() + " (Implementing API version " + getBukkitVersion() + ")");
+ // Paper start - add git information
+ server.getLogger().info(getVersionMessage());
+ }
+ /**
+ * Gets message describing the version server is running.
+ *
+ * @return message describing the version server is running
+ */
+ @NotNull
+ public static String getVersionMessage() {
+ Map<String, String> attributes = JarManifests.MANIFEST_MAP;
+ @NotNull String gitBranch = attributes.get("Git-Branch");
+ @NotNull String gitCommit = attributes.get("Git-Commit");
+ @NotNull String branchMsg = " on " + gitBranch;
+ if ("master".equals(gitBranch) || "main".equals(gitBranch)) {
+ branchMsg = ""; // Don't show branch on main/master
+ }
+ return "This server is running " + getName() + " version " + getVersion() + " (Implementing API version " + getBukkitVersion() + ") (Git: " + gitCommit + branchMsg + ")";
+ // Paper end
}
/**
diff --git a/src/main/java/org/bukkit/command/defaults/VersionCommand.java b/src/main/java/org/bukkit/command/defaults/VersionCommand.java
index 4c2ddc722a9dc4011906ad9530b13fa9be1d3ff9..57a21495843f3a144cd73473cdc8781d6129b7ca 100644
--- a/src/main/java/org/bukkit/command/defaults/VersionCommand.java
+++ b/src/main/java/org/bukkit/command/defaults/VersionCommand.java
@@ -241,7 +241,7 @@ public class VersionCommand extends BukkitCommand {
private void setVersionMessage(final @NotNull Component msg) {
lastCheck = System.currentTimeMillis();
final Component message = net.kyori.adventure.text.TextComponent.ofChildren(
- Component.text("This server is running " + Bukkit.getName() + " version " + Bukkit.getVersion() + " (Implementing API version " + Bukkit.getBukkitVersion() + ")", net.kyori.adventure.text.format.NamedTextColor.WHITE),
+ Component.text(Bukkit.getVersionMessage(), net.kyori.adventure.text.format.NamedTextColor.WHITE),
Component.newline(),
msg
);

View file

@ -20,10 +20,10 @@ index 67fb370cad6924895a6b27052dbd5c1767e3f0c9..3e05459f27c4c5697ae65da504d67a6a
/.project
diff --git a/build.gradle.kts b/build.gradle.kts
new file mode 100644
index 0000000000000000000000000000000000000000..596b4f52bc57eca8cc7a0138a1e36d71c1d73ca6
index 0000000000000000000000000000000000000000..9b738b8561f75a2531d64abb36f4cd9d9561cbfc
--- /dev/null
+++ b/build.gradle.kts
@@ -0,0 +1,134 @@
@@ -0,0 +1,135 @@
+import com.github.jengelman.gradle.plugins.shadow.transformers.Log4j2PluginsCacheFileTransformer
+import com.github.jengelman.gradle.plugins.shadow.transformers.Transformer
+import io.papermc.paperweight.util.Git
@ -71,10 +71,11 @@ index 0000000000000000000000000000000000000000..596b4f52bc57eca8cc7a0138a1e36d71
+ manifest {
+ val git = Git(rootProject.layout.projectDirectory.path)
+ val gitHash = git("rev-parse", "HEAD").getText().substring(0, 7)
+ val implementationVersion = System.getenv("BUILD_NUMBER") ?: "\"$gitHash\""
+ attributes(
+ "Main-Class" to "org.bukkit.craftbukkit.Main",
+ "Implementation-Title" to "CraftBukkit",
+ "Implementation-Version" to "git-Paper-\"$gitHash\"",
+ "Implementation-Version" to "git-Paper-$implementationVersion",
+ "Implementation-Vendor" to SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(Date()), // Paper
+ "Specification-Title" to "Bukkit",
+ "Specification-Version" to project.version,

View file

@ -5,7 +5,7 @@ Subject: [PATCH] Build system changes
diff --git a/build.gradle.kts b/build.gradle.kts
index 596b4f52bc57eca8cc7a0138a1e36d71c1d73ca6..74bde15e021f306a2f1247678f92becbdce313b8 100644
index ecc68a9bb80d72307b930863c84ee47cac272aef..d9cc094235b84a0541650abbdeb5e934cfad00d8 100644
--- a/build.gradle.kts
+++ b/build.gradle.kts
@@ -20,21 +20,23 @@ repositories {
@ -36,7 +36,7 @@ index 596b4f52bc57eca8cc7a0138a1e36d71c1d73ca6..74bde15e021f306a2f1247678f92becb
testImplementation("junit:junit:4.13.1")
testImplementation("org.hamcrest:hamcrest-library:1.3")
}
@@ -53,6 +55,7 @@ tasks.jar {
@@ -54,6 +56,7 @@ tasks.jar {
"Specification-Title" to "Bukkit",
"Specification-Version" to project.version,
"Specification-Vendor" to "Bukkit Team",
@ -44,7 +44,7 @@ index 596b4f52bc57eca8cc7a0138a1e36d71c1d73ca6..74bde15e021f306a2f1247678f92becb
)
for (tld in setOf("net", "com", "org")) {
attributes("$tld/bukkit", "Sealed" to true)
@@ -70,15 +73,23 @@ publishing {
@@ -71,15 +74,23 @@ publishing {
}
}

View file

@ -6,7 +6,7 @@ Subject: [PATCH] Deobfuscate stacktraces in log messages, crash reports, and
diff --git a/build.gradle.kts b/build.gradle.kts
index 04938bb10c35d2e424043adb7ed8fec2e42bb816..f427c6f5adf20f9e1b1fa4ab56041506d8240c92 100644
index 0e97fc8f6b961cb28e2338dd101e80fbbc580444..e4d1fe13d541fb0e7eee81607d9651cdf7032cb0 100644
--- a/build.gradle.kts
+++ b/build.gradle.kts
@@ -1,8 +1,12 @@
@ -46,7 +46,7 @@ index 04938bb10c35d2e424043adb7ed8fec2e42bb816..f427c6f5adf20f9e1b1fa4ab56041506
testImplementation("io.github.classgraph:classgraph:4.8.47") // Paper - mob goal test
testImplementation("junit:junit:4.13.1")
testImplementation("org.hamcrest:hamcrest-library:1.3")
@@ -115,6 +129,44 @@ tasks.shadowJar {
@@ -116,6 +130,44 @@ tasks.shadowJar {
transform(ModifiedLog4j2PluginsCacheFileTransformer::class.java)
}

View file

@ -0,0 +1,27 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Professor Bloodstone <git@bloodstone.dev>
Date: Sun, 20 Jun 2021 01:14:41 +0200
Subject: [PATCH] Add git branch and commit to manifest
diff --git a/build.gradle.kts b/build.gradle.kts
index e4d1fe13d541fb0e7eee81607d9651cdf7032cb0..39beb0331282b297f4f3857560d24ce6a616210d 100644
--- a/build.gradle.kts
+++ b/build.gradle.kts
@@ -78,6 +78,7 @@ tasks.jar {
val git = Git(rootProject.layout.projectDirectory.path)
val gitHash = git("rev-parse", "HEAD").getText().substring(0, 7)
val implementationVersion = System.getenv("BUILD_NUMBER") ?: "\"$gitHash\""
+ val gitBranch = git("rev-parse", "--abbrev-ref", "HEAD").getText().trim() // Paper
attributes(
"Main-Class" to "org.bukkit.craftbukkit.Main",
"Implementation-Title" to "CraftBukkit",
@@ -87,6 +88,8 @@ tasks.jar {
"Specification-Version" to project.version,
"Specification-Vendor" to "Bukkit Team",
"Multi-Release" to "true", // Paper
+ "Git-Branch" to gitBranch, // Paper
+ "Git-Commit" to gitHash, // Paper
)
for (tld in setOf("net", "com", "org")) {
attributes("$tld/bukkit", "Sealed" to true)