Skip to content

Commit

Permalink
Merge pull request #101 from vlsi/add_buildlogic
Browse files Browse the repository at this point in the history
Add build-logic to make adding new modules easier
  • Loading branch information
loosebazooka committed Sep 7, 2022
2 parents 9d30c3f + 02ca153 commit 200d266
Show file tree
Hide file tree
Showing 180 changed files with 551 additions and 222 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
.gradle
build
/build
/*/build
/http

# For occasional use of https://github.com/melix/includegit-gradle-plugin
/checkouts

# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored)
!gradle-wrapper.jar

Expand Down
1 change: 1 addition & 0 deletions build-logic-commons/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/*/build/
33 changes: 33 additions & 0 deletions build-logic-commons/gradle-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
plugins {
`kotlin-dsl`
}

group = "dev.sigstore.build-logic"

java {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}

// We use precompiled script plugins (== plugins written as src/kotlin/build-logic.*.gradle.kts files,
// and we need to declare dependency on org.gradle.kotlin.kotlin-dsl:org.gradle.kotlin.kotlin-dsl.gradle.plugin
// to make it work.
// Unfortunately, Gradle does not expose the version of `kotlin-dsl` in-core plugin, so we call `kotlin-dsl`
// on our own PluginDependenciesSpec object, so it leaks the version to us.
// See https://github.com/gradle/gradle/issues/17016
val kotlinDslVersion = PluginDependenciesSpec { id ->
object : PluginDependencySpec {
var version: String? = null
override fun version(version: String?) = apply { this.version = version }
override fun apply(apply: Boolean) = this
override fun toString() = version ?: ""
}
}.`kotlin-dsl`.toString()

dependencies {
implementation("org.gradle.kotlin.kotlin-dsl:org.gradle.kotlin.kotlin-dsl.gradle.plugin:$kotlinDslVersion")
}

kotlinDslPluginOptions {
jvmTarget.set("11")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
plugins {
id("java-library")
id("org.gradle.kotlin.kotlin-dsl") // this is 'kotlin-dsl' without version
}

java {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}

tasks.validatePlugins {
failOnWarning.set(true)
enableStricterValidation.set(true)
}

kotlinDslPluginOptions {
jvmTarget.set("11")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package buildlogic

import org.gradle.kotlin.dsl.DependencyHandlerScope
import org.gradle.kotlin.dsl.`kotlin-dsl`
import org.gradle.plugin.use.PluginDependenciesSpec
import org.gradle.plugin.use.PluginDependencySpec

val DependencyHandlerScope.kotlinDslVersion: String
get() = PluginDependenciesSpec {
object : PluginDependencySpec {
var version: String? = null
override fun version(version: String?) = apply { this.version = version }
override fun apply(apply: Boolean) = this
override fun toString() = version ?: ""
}
}.`kotlin-dsl`.toString()

fun DependencyHandlerScope.embeddedKotlinDsl() =
"org.gradle.kotlin.kotlin-dsl:org.gradle.kotlin.kotlin-dsl.gradle.plugin:$kotlinDslVersion"
1 change: 1 addition & 0 deletions build-logic-commons/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
systemProp.org.gradle.kotlin.dsl.precompiled.accessors.strict=true
9 changes: 9 additions & 0 deletions build-logic-commons/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
dependencyResolutionManagement {
repositories {
gradlePluginPortal()
}
}

rootProject.name = "build-logic-commons"

include("gradle-plugin")
1 change: 1 addition & 0 deletions build-logic/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/*/build/
34 changes: 34 additions & 0 deletions build-logic/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Build logic for Sigstore Java

This is a subset of extra plugins for factoring out
the common patterns from the common build logic.

The recommended approach is to use build composition, so every build script
should list all its prerequisites in the top-most `plugins { ... }` block.

The use of `allprojects` and `subprojects` is an anti-pattern as it makes it hard to identify
the configuration for a given project.

Let us consider an example (see `/sigstore-gradle-sign-base-plugin/build.gradle.kts`):

```kotlin
plugins {
id("build-logic.kotlin-dsl-published-gradle-plugin")
id("build-logic.test-junit5")
}

description = "Gradle plugin with the base set of tasks and configurations for Sigstore singing (no signing is done by default)"

dependencies {
compileOnly(project(":sigstore-java"))
implementation("com.fasterxml.jackson.core:jackson-databind:2.13.3")

testImplementation(project(":sigstore-testkit"))
}
```

It means that we deal with a Gradle plugin written in Kotlin that will be published to Central,
and which uses JUnit 5 for testing.

If you want to see what the logic does, you could open `buildlogic.kotlin-dsl-published-plugin.gradle.kts`
and `buildlogic.test-junit5.gradle.kts`.
3 changes: 3 additions & 0 deletions build-logic/basics/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
plugins {
id("build-logic.kotlin-dsl-gradle-plugin")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
repositories {
mavenCentral()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
tasks.withType<AbstractArchiveTask>().configureEach {
// Ensure builds are reproducible
isPreserveFileTimestamps = false
isReproducibleFileOrder = true
dirMode = "775".toInt(8)
fileMode = "664".toInt(8)
}
1 change: 1 addition & 0 deletions build-logic/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
systemProp.org.gradle.kotlin.dsl.precompiled.accessors.strict=true
15 changes: 15 additions & 0 deletions build-logic/jvm/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import buildlogic.embeddedKotlinDsl

plugins {
id("build-logic.kotlin-dsl-gradle-plugin")
}

repositories {
gradlePluginPortal()
}

dependencies {
implementation(embeddedKotlinDsl())
implementation("com.github.vlsi.gradle-extensions:com.github.vlsi.gradle-extensions.gradle.plugin:1.82")
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
plugins {
id("build-logic.java")
id("java-library")
}
48 changes: 48 additions & 0 deletions build-logic/jvm/src/main/kotlin/build-logic.java.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import buildlogic.filterEolSimple

plugins {
`java-base`
id("com.github.vlsi.gradle-extensions")
}

java {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}

tasks.withType<JavaCompile>().configureEach {
inputs.property("java.version", System.getProperty("java.version"))
inputs.property("java.vm.version", System.getProperty("java.vm.version"))
options.apply {
encoding = "UTF-8"
compilerArgs.add("-Xlint:deprecation")
compilerArgs.add("-Werror")
}
}

tasks.withType<Javadoc>().configureEach {
(options as StandardJavadocDocletOptions).apply {
addBooleanOption("Xwerror", true)
addStringOption("sourcepath", "src/main/java")
// intentionally ignore missing errors for now
addBooleanOption("Xdoclint:all,-missing", true)
}
}

// Add default license/notice when missing (e.g. see :src:config that overrides LICENSE)

tasks.withType<Jar>().configureEach {
into("META-INF") {
filterEolSimple("crlf")
from("$rootDir/LICENSE")
from("$rootDir/NOTICE")
}
manifest {
attributes["Bundle-License"] = "Apache-2.0"
attributes["Specification-Title"] = project.name + " " + project.description
attributes["Specification-Vendor"] = "dev.sigstore"
attributes["Implementation-Vendor"] = "dev.sigstore"
attributes["Implementation-Vendor-Id"] = "dev.sigstore"
// Implementation-Version is not here to make jar reproducible across versions
}
}
12 changes: 12 additions & 0 deletions build-logic/jvm/src/main/kotlin/build-logic.test-junit5.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
plugins {
`java-library`
}

dependencies {
testImplementation("org.junit.jupiter:junit-jupiter")
testImplementation("org.assertj:assertj-core")
}

tasks.withType<Test>().configureEach {
useJUnitPlatform()
}
21 changes: 21 additions & 0 deletions build-logic/jvm/src/main/kotlin/buildlogic/CopySpecExtensions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package buildlogic

import org.apache.tools.ant.filters.FixCrLfFilter
import org.gradle.api.file.CopySpec
import org.gradle.kotlin.dsl.filter

/**
* Converts end-of-line markers in the current [CopySpec] to the given value.
* See [org.apache.tools.ant.filters.FixCrLfFilter.CrLf] for the possible values of `eol`.
* See https://github.com/gradle/gradle/issues/8688.
*/
fun CopySpec.filterEolSimple(eol: String) {
filteringCharset = "UTF-8"
filter(
FixCrLfFilter::class, mapOf(
"eol" to FixCrLfFilter.CrLf.newInstance(eol),
"fixlast" to true,
"ctrlz" to FixCrLfFilter.AddAsisRemove.newInstance("asis")
)
)
}
13 changes: 13 additions & 0 deletions build-logic/publishing/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
plugins {
id("build-logic.kotlin-dsl-gradle-plugin")
}

repositories {
gradlePluginPortal()
}

dependencies {
implementation(project(":basics"))
implementation(project(":jvm"))
implementation("com.gradle.plugin-publish:com.gradle.plugin-publish.gradle.plugin:1.0.0")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
plugins {
id("build-logic.repositories")
id("build-logic.java-library")
id("build-logic.reproducible-builds")
id("build-logic.publish-to-central")
}

java {
withJavadocJar()
withSourcesJar()
}

publishing {
publications {
create<MavenPublication>("mavenJava") {
from(components["java"])
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import org.gradle.api.publish.internal.PublicationInternal

plugins {
id("java-library")
id("maven-publish")
}

val repoUrl = "https://github.com/sigstore/sigstore-java"

publishing {
publications.withType<MavenPublication>().configureEach {
// Use the resolved versions in pom.xml
// Gradle might have different resolution rules, so we set the versions
// that were used in Gradle build/test.
versionMapping {
usage(Usage.JAVA_RUNTIME) {
fromResolutionResult()
}
usage(Usage.JAVA_API) {
fromResolutionOf("runtimeClasspath")
}
}
pom {
name.set(
(project.findProperty("artifact.name") as? String)
?: project.name
)
// This code might be executed before project-related build.gradle.kts is evaluated
// So we delay access to project.description
description.set(
project.provider { project.description }
)
inceptionYear.set("2022")
url.set(repoUrl)
organization {
name.set("Sigstore")
url.set("https://sigstore.dev")
}
developers {
developer {
organization.set("Sigstore authors")
organizationUrl.set("https://sigstore.dev")
}
}
issueManagement {
system.set("GitHub Issues")
url.set("$repoUrl/issues")
}
licenses {
license {
name.set("Apache-2.0")
url.set("https://www.apache.org/licenses/LICENSE-2.0.txt")
}
}
scm {
connection.set("scm:git:$repoUrl.git")
developerConnection.set("scm:git:$repoUrl.git")
url.set(repoUrl)
tag.set("HEAD")
}
}
}
}

val createReleaseBundle by tasks.registering(Sync::class) {
description = "This task should be used by github actions to create release artifacts along with a slsa attestation"
val releaseDir = layout.buildDirectory.dir("release")
outputs.dir(releaseDir)

into(releaseDir)
rename("pom-default.xml", "${project.name}-${project.version}.pom")
rename("module.json", "${project.name}-${project.version}.module")
}

publishing {
publications.configureEach {
(this as PublicationInternal<*>).allPublishableArtifacts {
val publicationArtifact = this
createReleaseBundle.configure {
dependsOn(publicationArtifact)
from(publicationArtifact.file)
}
}
}
}

3 changes: 3 additions & 0 deletions build-logic/root-build/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
plugins {
id("build-logic.kotlin-dsl-gradle-plugin")
}
13 changes: 13 additions & 0 deletions build-logic/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
dependencyResolutionManagement {
repositories {
gradlePluginPortal()
}
}

rootProject.name = "build-logic"

includeBuild("../build-logic-commons")
include("basics")
include("jvm")
include("publishing")
include("root-build")
Loading

0 comments on commit 200d266

Please sign in to comment.