Skip to content

Commit

Permalink
Merge pull request #113 from Strumenta/mavenpublishing
Browse files Browse the repository at this point in the history
Setting up maven publishing and release process
  • Loading branch information
ftomassetti authored Dec 11, 2023
2 parents f1d39e2 + 5474dab commit 04d77aa
Show file tree
Hide file tree
Showing 13 changed files with 187 additions and 77 deletions.
62 changes: 40 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ To start using ANTLR Kotlin:
}
```

3. Add ANTLR 4 and the ANTLR Kotlin target to the classpath of the Gradle ANTLR plugin.
3. Add ANTLR 4 and the ANTLR Kotlin target to the classpath of the Gradle ANTLR plugin.
At the top level of your build script, add:

```kotlin
Expand All @@ -76,7 +76,7 @@ To start using ANTLR Kotlin:
antlr("org.antlr:antlr4:4.13.1")

// The ANTLR Kotlin target
antlr("com.strumenta.antlr-kotlin:antlr-kotlin-target:{version}")
antlr("com.strumenta:antlr-kotlin-target:$antlrKotlinVersion")
}
```
For more details, check out [Gradle - The ANTLR Plugin](https://docs.gradle.org/current/userguide/antlr_plugin.html).
Expand All @@ -90,7 +90,7 @@ To start using ANTLR Kotlin:
commonMain {
dependencies {
...
implementation("com.strumenta.antlr-kotlin:antlr-kotlin-runtime:{version}")
implementation("com.strumenta:antlr-kotlin-runtime:$antlrKotlinVersion")
}
}
}
Expand All @@ -114,25 +114,28 @@ To start using ANTLR Kotlin:
6. Create the ANTLR Kotlin grammar generation task.

```kotlin
val generateKotlinGrammarSource = register<AntlrTask>("generateKotlinGrammarSource") {
dependsOn("cleanGenerateKotlinGrammarSource")

// ANTLR .g4 files are under {example-project}/antlr
setSource(layout.projectDirectory.dir("antlr"))

val pkgName = "com.strumenta.antlrkotlin.parsers.generated"
arguments = listOf(
"-Dlanguage=Kotlin", // We want to generate Kotlin sources
"-visitor", // We want visitors alongside listeners
"-package", pkgName, // We want the generated sources to have this package declared
"-encoding", "UTF-8", // We want the generated sources to be encoded in UTF-8
)

// Generated files are outputted inside standard sources,
// but you can switch to output them under build/
val outDir = "src/commonMain/kotlin/${pkgName.replace(".", "/")}"
outputDirectory = layout.projectDirectory.dir(outDir).asFile
}
val generateKotlinGrammarSource = tasks.register<AntlrTask>("generateKotlinGrammarSource") {
dependsOn("cleanGenerateKotlinGrammarSource")

// ANTLR .g4 files are under {example-project}/antlr
setSource(layout.projectDirectory.dir("src/main/antlr"))

val pkgName = "com.strumenta.antlrkotlin.parsers.generated"
arguments = listOf(
"-Dlanguage=Kotlin", // We want to generate Kotlin sources
"-visitor", // We want visitors alongside listeners
"-package", pkgName, // We want the generated sources to have this package declared
"-encoding", "UTF-8", // We want the generated sources to be encoded in UTF-8
)

// Generated files are outputted inside build/
val outDir = "generatedAntlr/${pkgName.replace(".", "/")}"
outputDirectory = layout.buildDirectory.get().dir(outDir).asFile

sourceSets.getByName("main") {
java.srcDir(layout.buildDirectory.get().dir("generatedAntlr"))
}
}
```

Depending on `cleanGenerateKotlinGrammarSource` ensures the `.tokens` files are always fresh,
Expand All @@ -144,8 +147,23 @@ To start using ANTLR Kotlin:
withType<KotlinCompile<*>> {
dependsOn(generateKotlinGrammarSource)
}

```

## Maven Central Publication

Publication can be performed running:

```
./gradlew publishAllPublicationsToMavenCentralRepository --no-configuration-cache
```

However, it is recommended to use the releases plugin and run:

```
./gradlew release
```

## Contributors

You can see the complete list on GitHub, but here we list those who contributed ten commits or more.
Expand Down
33 changes: 31 additions & 2 deletions antlr-kotlin-runtime/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import com.strumenta.kotlinmultiplatform.gradle.ext.targetsNative
import com.strumenta.kotlinmultiplatform.gradle.ext.*
import org.jetbrains.kotlin.gradle.dsl.ExplicitApiMode
import org.jetbrains.dokka.gradle.DokkaTask
import com.vanniktech.maven.publish.SonatypeHost

plugins {
id("strumenta.multiplatform")
id("org.jetbrains.dokka")
id("com.vanniktech.maven.publish")
}

strumentaMultiplatform {
applyJvm()
applyJs()

if (targetsJS()) {
applyJs()
}

// Opting-in for native targets should be explicit,
// as it makes the build and test process slower.
Expand Down Expand Up @@ -43,3 +50,25 @@ kotlin {
}
}
}

tasks.withType<DokkaTask>().configureEach {
dokkaSourceSets {
configureEach {
suppress.set(true)
}

val commonMain by getting {
suppress.set(false)
platform.set(org.jetbrains.dokka.Platform.jvm)
}
}
}

mavenPublishing {
coordinates(project.group as String, project.name, project.version as String)
setupPom(project, "Runtime for ANTLR Kotlin")

publishToMavenCentral(SonatypeHost.DEFAULT, true)

signAllPublications()
}
25 changes: 19 additions & 6 deletions antlr-kotlin-target/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
import com.strumenta.kotlinmultiplatform.gradle.ext.*
import org.jetbrains.dokka.gradle.DokkaTask
import java.net.URI
import java.net.URL
import com.vanniktech.maven.publish.SonatypeHost

plugins {
id("strumenta.jvm.library")
id("org.jetbrains.dokka")
id("com.vanniktech.maven.publish")
}

dependencies {
api(libs.antlr4)
}

publishing {
publications {
create<MavenPublication>("mavenJava") {
from(components["java"])
}
}
mavenPublishing {
coordinates(project.group as String, project.name, project.version as String)
setupPom(project, "Kotlin target for ANTLR")

publishToMavenCentral(SonatypeHost.DEFAULT, true)

signAllPublications()
}

tasks {
findByName("generateMetadataFileForMavenPublication")?.dependsOn("kotlinSourcesJar")
}
47 changes: 15 additions & 32 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,36 +1,26 @@
import com.strumenta.kotlinmultiplatform.gradle.ext.mavenRepositoryUrl

plugins {
`maven-publish`
id("net.researchgate.release") version "3.0.2"
}

allprojects {
version = "1.0.0-SNAPSHOT"
group = "com.strumenta.antlr-kotlin"
group = "com.strumenta"

repositories {
mavenCentral()
}

apply(plugin = "maven-publish")

// TODO: setup a real Maven repository to publish to, e.g., Maven Central
val repoUrl = mavenRepositoryUrl()
}

if (repoUrl != null) {
publishing {
repositories {
maven {
name = "StrumentaAntlrKotlin"
url = project.uri(repoUrl)
subprojects {

// TODO: this should be removed once the publication targets
// a real public repository
isAllowInsecureProtocol = true
}
}
tasks.withType<Test>().all {
testLogging {
showStandardStreams = true
showExceptions = true
exceptionFormat = org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL
}
}

}

tasks {
Expand All @@ -40,17 +30,10 @@ tasks {
}
}

tasks {
create("ciBasicBuild") {
dependsOn(":antlr-kotlin-runtime:compileKotlinJvm")
dependsOn(":antlr-kotlin-tests:compileKotlinJvm")
dependsOn(":antlr-kotlin-target:compileKotlin")
}

create("ciTest") {
dependsOn(":antlr-kotlin-runtime:jvmTest")
dependsOn(":antlr-kotlin-tests:jvmTest")
dependsOn(":antlr-kotlin-target:test")
release {
buildTasks.set(listOf(":antlr-kotlin-runtime:publishAllPublicationsToMavenCentralRepository", ":antlr-kotlin-target:publishAllPublicationsToMavenCentralRepository"))
git {
requireBranch.set("")
pushToRemote.set("origin")
}
}

2 changes: 2 additions & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ plugins {
dependencies {
// The Kotlin Gradle Plugin dependency propagates to the entire workspace
implementation(libs.kotlin.plugin)
implementation("com.vanniktech:gradle-maven-publish-plugin:0.25.3")
implementation("org.jetbrains.dokka:dokka-gradle-plugin:1.9.10")
}

gradlePlugin {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
package com.strumenta.kotlinmultiplatform.gradle.ext

import org.gradle.api.Project
import org.gradle.api.artifacts.dsl.RepositoryHandler
import org.gradle.api.plugins.JavaPluginExtension
import org.gradle.api.publish.PublishingExtension
import org.gradle.api.publish.maven.MavenPublication
import org.gradle.kotlin.dsl.create
import org.gradle.kotlin.dsl.getByType
import org.gradle.kotlin.dsl.repositories
import org.gradle.kotlin.dsl.withType
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import java.net.URI
import com.vanniktech.maven.publish.MavenPublishBaseExtension

/**
* Returns the Java project extension.
Expand Down Expand Up @@ -34,13 +42,14 @@ fun Project.stringProperty(name: String): String? =
* Returns a project property as a boolean (`"true" == true`, else `false`).
*/
fun Project.booleanProperty(name: String): Boolean =
findProperty(name) == "true"
(findProperty(name) as? String)?.trim()?.lowercase() == "true"

/**
* Returns whether we are building for a release, or not.
*/
fun Project.releaseBuild(): Boolean =
booleanProperty("repo.is.release")
!(stringProperty("version")?.endsWith("-SNAPSHOT")
?: throw IllegalStateException("Project version not specified"))

/**
* Returns the URL of the Maven repository to publish artifacts to,
Expand All @@ -61,3 +70,61 @@ fun Project.mavenRepositoryUrl(): String? {
*/
fun Project.targetsNative(): Boolean =
booleanProperty("target.is.native")

fun Project.targetsJS(): Boolean =
booleanProperty("target.is.js")


fun PublishingExtension.addSonatypeRepository(project: Project) {
repositories {
maven {
name = "oss"
url = URI(project.mavenRepositoryUrl())
credentials {
username = project.findProperty("ossrhUsername") as? String ?: "Unknown user"
password = project.findProperty("ossrhPassword") as? String ?: "Unknown password"
}
}
}
}

val Project.publicationName
get() = project.name.replace("-", "_")

fun MavenPublishBaseExtension.setupPom(project: Project, descriptionValue: String) {
pom {
name.set(project.name)
description.set(descriptionValue)
// version = project.version as String
// packaging = "jar"
url.set("https://github.com/Strumenta/antlr-kotlin")

scm {
connection.set("scm:git:https://github.com/Strumenta/antlr-kotlin.git")
developerConnection.set("scm:git:[email protected]:Strumenta/antlr-kotlin.git")
url.set("https://github.com/Strumenta/antlr-kotlin.git")
}

licenses {
license {
name.set("Apache Licenve V2.0")
url.set("https://www.apache.org/licenses/LICENSE-2.0")
distribution.set("repo")
}
}

// The developers entry is strictly required by Maven Central
developers {
developer {
id.set("ftomassetti")
name.set("Federico Tomassetti")
email.set("[email protected]")
}
developer {
id.set("lppedd")
name.set("Edoardo Luppi")
email.set("[email protected]")
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,5 @@ class StrumentaJvmLibraryModulePlugin : Plugin<Project> {
val java = project.javaExtension
java.sourceCompatibility = JavaVersion.VERSION_1_8
java.targetCompatibility = JavaVersion.VERSION_1_8
java.withSourcesJar()
}
}
5 changes: 2 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,5 @@ kotlin.incremental.js = true
# Project settings
#########################
target.is.native = true
repo.is.release = false
repo.snapshot.url =
repo.release.url =
target.is.js = true
version = 0.1.0-RC7-SNAPSHOT
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
kotlin = "1.9.21"
kotlin-wrappers = "1.0.0-pre.659"
antlr4 = "4.13.1"
dokka = "1.9.10"

[libraries]
kotlin-wrappers-bom = { group = "org.jetbrains.kotlin-wrappers", name = "kotlin-wrappers-bom", version.ref = "kotlin-wrappers" }
Expand All @@ -14,3 +15,4 @@ kotlin-plugin = { group = "org.jetbrains.kotlin", name = "kotlin-gradle-plugin",
[plugins]
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
kotlin-multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
dokka = { id = "org.jetbrains.dokka", version.ref = "dokka" }
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
1 change: 1 addition & 0 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-all.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading

0 comments on commit 04d77aa

Please sign in to comment.