Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add buildinfo at compile time #728

Merged
merged 2 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions build-logic/jvm/src/main/kotlin/build-logic.build-info.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import buildlogic.BuildInfoTask

plugins {
java
}

val generateBuildInfo by tasks.registering(BuildInfoTask::class) {
version.set(project.version.toString())
genDir.set(project.layout.buildDirectory.dir("generated/buildinfo"))
}

sourceSets.main {
java.srcDir(generateBuildInfo)
}
37 changes: 37 additions & 0 deletions build-logic/jvm/src/main/kotlin/buildlogic/BuildInfoTask.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package buildlogic

import org.gradle.api.DefaultTask
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.file.RegularFile
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputDirectory
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction

abstract class BuildInfoTask : DefaultTask() {
@get:Input
abstract val packageName: Property<String>

@get:Input
abstract val version: Property<String>

@get:OutputDirectory
abstract val genDir: DirectoryProperty

@TaskAction
fun run() {
val output = """
|package ${packageName.get()};
|
|public class BuildInfo {
| public static final String VERSION = "${version.get()}";
|}
""".trimMargin()
loosebazooka marked this conversation as resolved.
Show resolved Hide resolved
val outputPath = genDir.file(packageName.get().replace(".", "/").plus("/BuildInfo.java")).get().asFile
outputPath.parentFile.mkdirs()
outputPath.writeText(output)
}
}
5 changes: 5 additions & 0 deletions sigstore-java/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import com.google.protobuf.gradle.id
plugins {
id("build-logic.java-published-library")
id("build-logic.test-junit5")
id("build-logic.build-info")
id("org.jsonschema2dataclass") version "5.0.0"
id("com.google.protobuf") version "0.9.4"
}
Expand Down Expand Up @@ -120,3 +121,7 @@ jsonSchema2Pojo {
tasks.named("sourcesJar") {
dependsOn("generateJsonSchema2DataClassConfigRekor")
}

tasks.generateBuildInfo {
packageName.set("dev.sigstore.buildinfo")
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package dev.sigstore.http;

import com.google.api.client.util.Preconditions;
import dev.sigstore.buildinfo.BuildInfo;
import org.immutables.value.Value;

/**
Expand All @@ -24,7 +25,7 @@
*/
@Value.Immutable
public abstract class HttpParams {
static final String DEFAULT_USER_AGENT = "sigstoreJavaClient/0.0.1";
static final String DEFAULT_USER_AGENT = "sigstoreJavaClient/" + BuildInfo.VERSION;
static final int DEFAULT_TIMEOUT = 60;
static final boolean DEFAULT_ALLOW_INSECURE_CONNECTIONS = false;

Expand Down
Loading