Skip to content

Commit

Permalink
Merge pull request #196 from PaperMC/feature/jib
Browse files Browse the repository at this point in the history
chore: use gradle jib plugin
  • Loading branch information
kashike authored May 9, 2024
2 parents b57ea18 + 9bd80f8 commit 1940141
Show file tree
Hide file tree
Showing 21 changed files with 97 additions and 95 deletions.
11 changes: 0 additions & 11 deletions .checkstyle/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,6 @@
<property name="checkLastCaseGroup" value="true"/>
</module>

<!-- https://checkstyle.org/config_design.html#FinalClass -->
<module name="FinalClass"/>

<!-- https://checkstyle.org/config_coding.html#FinalLocalVariable -->
<module name="FinalLocalVariable">
<property name="tokens" value="PARAMETER_DEF, VARIABLE_DEF"/> <!-- add PARAMETER_DEF -->
Expand All @@ -93,9 +90,6 @@
<!-- https://checkstyle.org/config_whitespace.html#GenericWhitespace -->
<module name="GenericWhitespace"/>

<!-- https://checkstyle.org/config_design.html#HideUtilityClassConstructor -->
<module name="HideUtilityClassConstructor"/>

<!-- https://checkstyle.org/config_imports.html#IllegalImport -->
<module name="IllegalImport">
<property name="illegalPkgs" value="sun, jdk, com.sun"/>
Expand Down Expand Up @@ -136,11 +130,6 @@
<!-- https://checkstyle.org/config_blocks.html#LeftCurly -->
<module name="LeftCurly"/>

<!-- https://checkstyle.org/config_naming.html#MethodName -->
<module name="MethodName">
<property name="format" value="^(?:(?:.{1,3})|(?:[gs]et[^A-Z].*)|(?:(?:[^gsA-Z]..|.[^e].|..[^t]).+))$"/>
</module>

<!-- https://checkstyle.org/config_whitespace.html#MethodParamPad -->
<module name="MethodParamPad"/>

Expand Down
6 changes: 6 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
* text=auto

*.bat text eol=crlf
*.sh text eol=lf

gradlew text eol=lf
34 changes: 0 additions & 34 deletions Dockerfile

This file was deleted.

94 changes: 79 additions & 15 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
import java.nio.file.StandardOpenOption
import kotlin.io.path.Path
import kotlin.io.path.bufferedWriter

buildscript {
dependencies {
classpath("com.google.cloud.tools:jib-spring-boot-extension-gradle:0.1.0")
}
}

plugins {
id("java")

alias(libs.plugins.indra)
alias(libs.plugins.indra.checkstyle)
alias(libs.plugins.jib)
alias(libs.plugins.spotless)
alias(libs.plugins.spring.dependency.management)
alias(libs.plugins.spring.boot)
}

group = "io.papermc"
version = "1.0.0-SNAPSHOT"

repositories {
mavenCentral()
}
Expand All @@ -34,6 +42,52 @@ spotless {
}
}

jib {
to {
image = "ghcr.io/papermc/bibliothek"
tags = setOf("latest", project.version.toString())
}

from {
image = "azul/zulu-openjdk-alpine:${indra.javaVersions().target().get()}-jre"
platforms {
// We can only build multi-arch images when pushing to a registry, not when building locally
val requestedTasks = gradle.startParameter.taskNames
if ("jibBuildTar" in requestedTasks || "jibDockerBuild" in requestedTasks) {
platform {
// todo: better logic
architecture = when (System.getProperty("os.arch")) {
"aarch64" -> "arm64"
else -> "amd64"
}
os = "linux"
}
} else {
platform {
architecture = "amd64"
os = "linux"
}
platform {
architecture = "arm64"
os = "linux"
}
}
}
}

pluginExtensions {
pluginExtension {
implementation = "com.google.cloud.tools.jib.gradle.extension.springboot.JibSpringBootExtension"
}
}

container {
args = listOf("--spring.config.additional-location=optional:file:/config/")
ports = listOf("8080")
labels.put("org.opencontainers.image.source", indra.scm().map { it.url() })
}
}

dependencies {
annotationProcessor("org.springframework.boot", "spring-boot-configuration-processor")
checkstyle(libs.stylecheck)
Expand All @@ -48,20 +102,30 @@ dependencies {
}

tasks {
bootJar {
launchScript()
val outputImageId = register("printJibMeta") {
description = "Expose image information as an output for GitHub Actions"

val jibImageJson = project.jib.outputPaths.imageJson
val githubOutput = providers.environmentVariable("GITHUB_OUTPUT")
inputs.property("jibImageJson", jibImageJson)
inputs.property("githubOutput", githubOutput).optional(true)

doLast {
if (!githubOutput.isPresent) {
didWork = false
return@doLast
}

Path(githubOutput.get()).bufferedWriter(Charsets.UTF_8, options = arrayOf(StandardOpenOption.CREATE, StandardOpenOption.APPEND)).use {
it.write("imageJson=")
file(jibImageJson).bufferedReader(Charsets.UTF_8).use { meta -> meta.transferTo(it) }
}
}
}

// From StackOverflow: https://stackoverflow.com/a/53087407
// Licensed under: CC BY-SA 4.0
// Adapted to Kotlin
register<Copy>("buildForDocker") {
from(bootJar)
into("build/libs/docker")
rename { fileName ->
// a simple way is to remove the "-$version" from the jar filename
// but you can customize the filename replacement rule as you wish.
fileName.replace("-$version", "")
sequenceOf(jib, jibDockerBuild, jibBuildTar).forEach {
it.configure {
finalizedBy(outputImageId.name)
}
}
}
8 changes: 0 additions & 8 deletions docker/default.application.yaml

This file was deleted.

2 changes: 2 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
group = io.papermc
version = 1.0.0-SNAPSHOT
3 changes: 2 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ indra = "3.1.3"
[plugins]
indra = { id = "net.kyori.indra", version.ref = "indra" }
indra-checkstyle = { id = "net.kyori.indra.checkstyle", version.ref = "indra" }
jib = { id = "com.google.cloud.tools.jib", version = "3.4.2" }
spotless = { id = "com.diffplug.spotless", version = "6.25.0" }
spring-boot = { id = "org.springframework.boot", version = "3.2.2" }
spring-dependency-management = { id = "io.spring.dependency-management", version = "1.1.4" }

[libraries]
jetbrains-annotations = { group = "org.jetbrains", name = "annotations", version = "24.1.0" }
springdoc-openapi-starter-webmvc-ui = { group = "org.springdoc", name = "springdoc-openapi-starter-webmvc-ui", version = "2.3.0" }
springdoc-openapi-starter-webmvc-ui = { group = "org.springdoc", name = "springdoc-openapi-starter-webmvc-ui", version = "2.5.0" }
stylecheck = { group = "ca.stellardrift", name = "stylecheck", version = "0.2.1" }
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
14 changes: 7 additions & 7 deletions gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,15 @@ if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
case $MAX_FD in #(
max*)
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
# shellcheck disable=SC2039,SC3045
MAX_FD=$( ulimit -H -n ) ||
warn "Could not query maximum file descriptor limit"
esac
case $MAX_FD in #(
'' | soft) :;; #(
*)
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
# shellcheck disable=SC2039,SC3045
ulimit -n "$MAX_FD" ||
warn "Could not set maximum file descriptor limit to $MAX_FD"
esac
Expand Down Expand Up @@ -202,11 +202,11 @@ fi
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'

# Collect all arguments for the java command;
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
# shell script including quotes and variable substitutions, so put them in
# double quotes to make sure that they get re-expanded; and
# * put everything else in single quotes, so that it's not re-expanded.
# Collect all arguments for the java command:
# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
# and any embedded shellness will be escaped.
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
# treated as '${Hostname}' itself on the command line.

set -- \
"-Dorg.gradle.appname=$APP_BASE_NAME" \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
})
@SpringBootApplication
@ServletComponentScan
@SuppressWarnings("checkstyle:HideUtilityClassConstructor")
public class BibliothekApplication {
public static void main(final String[] args) {
SpringApplication.run(BibliothekApplication.class, args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,42 +37,34 @@ public class AppConfiguration {
private String apiVersion;
private @NotNull Path storagePath;

@SuppressWarnings("checkstyle:MethodName")
public URL getApiBaseUrl() {
return this.apiBaseUrl;
}

@SuppressWarnings("checkstyle:MethodName")
public void setApiBaseUrl(final URL apiBaseUrl) {
this.apiBaseUrl = apiBaseUrl;
}

@SuppressWarnings("checkstyle:MethodName")
public String getApiTitle() {
return this.apiTitle;
}

@SuppressWarnings("checkstyle:MethodName")
public void setApiTitle(final String apiTitle) {
this.apiTitle = apiTitle;
}

@SuppressWarnings("checkstyle:MethodName")
public String getApiVersion() {
return this.apiVersion;
}

@SuppressWarnings("checkstyle:MethodName")
public void setApiVersion(final String apiVersion) {
this.apiVersion = apiVersion;
}

@SuppressWarnings("checkstyle:MethodName")
public Path getStoragePath() {
return this.storagePath;
}

@SuppressWarnings("checkstyle:MethodName")
public void setStoragePath(final Path storagePath) {
this.storagePath = storagePath;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@

@RestController
@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE)
@SuppressWarnings("checkstyle:FinalClass")
public class DownloadController {
private static final CacheControl CACHE = HTTP.sMaxAgePublicCache(Duration.ofDays(7));
private final AppConfiguration configuration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@

@RestController
@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE)
@SuppressWarnings("checkstyle:FinalClass")
public class ProjectController {
private static final CacheControl CACHE = HTTP.sMaxAgePublicCache(Duration.ofMinutes(30));
private final ProjectCollection projects;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@

@RestController
@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE)
@SuppressWarnings("checkstyle:FinalClass")
public class ProjectsController {
private static final CacheControl CACHE = HTTP.sMaxAgePublicCache(Duration.ofDays(7));
private final ProjectCollection projects;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@

@RestController
@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE)
@SuppressWarnings("checkstyle:FinalClass")
public class VersionBuildController {
private static final CacheControl CACHE = HTTP.sMaxAgePublicCache(Duration.ofDays(7));
private final ProjectCollection projects;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@

@RestController
@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE)
@SuppressWarnings("checkstyle:FinalClass")
public class VersionBuildsController {
private static final CacheControl CACHE = HTTP.sMaxAgePublicCache(Duration.ofMinutes(5));
private final ProjectCollection projects;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@

@RestController
@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE)
@SuppressWarnings("checkstyle:FinalClass")
public class VersionController {
private static final CacheControl CACHE = HTTP.sMaxAgePublicCache(Duration.ofMinutes(5));
private final ProjectCollection projects;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@

@RestController
@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE)
@SuppressWarnings("checkstyle:FinalClass")
public class VersionFamilyBuildsController {
private static final CacheControl CACHE = HTTP.sMaxAgePublicCache(Duration.ofMinutes(5));
private final ProjectCollection projects;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@

@RestController
@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE)
@SuppressWarnings("checkstyle:FinalClass")
public class VersionFamilyController {
private static final CacheControl CACHE = HTTP.sMaxAgePublicCache(Duration.ofMinutes(5));
private final ProjectCollection projects;
Expand Down
1 change: 0 additions & 1 deletion src/main/java/io/papermc/bibliothek/exception/Advice.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import org.springframework.web.servlet.NoHandlerFoundException;

@ControllerAdvice
@SuppressWarnings("checkstyle:FinalClass")
class Advice {
private final ObjectMapper json;

Expand Down

0 comments on commit 1940141

Please sign in to comment.