From 87c0432d707a627fec0181eb78e442e499500bbd Mon Sep 17 00:00:00 2001 From: pstaeck <36667270+pstaeck@users.noreply.github.com> Date: Tue, 28 May 2024 10:26:20 +0200 Subject: [PATCH] adapt workflow and build script in order to deploy on maven central (#76) The purpose of the pull request is to switch deployment of t2iapi (java part) from github packages to maven central. # Checklist The following aspects have been respected by the author of this pull request, confirmed by both pull request assignee **and** reviewer: * Changelog update (necessity checked and entry added or not added respectively) * [X] Pull Request Assignee * [x] Reviewer * README update (necessity checked and entry added or not added respectively) * [X] Pull Request Assignee * [x] Reviewer --------- Co-authored-by: Staeck, Philipp --- .github/workflows/build-java.yml | 55 +++++++++++++++----------------- java/build.gradle.kts | 54 +++++++++++++++++++++++++++---- tests/java/build.gradle.kts | 2 +- 3 files changed, 74 insertions(+), 37 deletions(-) diff --git a/.github/workflows/build-java.yml b/.github/workflows/build-java.yml index c4c085d..ad22829 100644 --- a/.github/workflows/build-java.yml +++ b/.github/workflows/build-java.yml @@ -13,7 +13,7 @@ jobs: runs-on: ubuntu-latest steps: # Run `git checkout` - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Source dependency versions working-directory: config run: | @@ -29,13 +29,13 @@ jobs: wget https://github.com/protocolbuffers/protobuf/releases/download/v${{ env.COMMON_PROTOC_VERSION }}/protoc-${{ env.COMMON_PROTOC_VERSION }}-linux-x86_64.zip -P ${{ runner.temp }}/protoc_install unzip ${{ runner.temp }}/protoc_install/*.zip -d ${{ runner.temp }}/protoc_install/protoc echo "${{ runner.temp }}/protoc_install/protoc/bin" >> $GITHUB_PATH - - name: Set up JDK 11 - uses: actions/setup-java@v3 + - name: Set up JDK 17 + uses: actions/setup-java@v4 with: - java-version: '11' + java-version: '17' distribution: 'temurin' - name: Setup Gradle - uses: gradle/gradle-build-action@v2 + uses: gradle/actions/setup-gradle@v3 with: gradle-version: ${{ env.GRADLE_VERSION }} - name: Execute build @@ -46,12 +46,12 @@ jobs: with: name: target_jars path: java/build/libs/*.jar - run-tests-java11: + run-tests-java17: runs-on: ubuntu-latest needs: build-java steps: # Run `git checkout` - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Download t2iapi jar artifact uses: actions/download-artifact@v3 with: @@ -61,13 +61,13 @@ jobs: if: startsWith(github.ref, 'refs/tags/v') run: | echo RELEASE_VERSION=1 >> $GITHUB_ENV - - name: Set up JDK 11 - uses: actions/setup-java@v3 + - name: Set up JDK 17 + uses: actions/setup-java@v4 with: - java-version: '11' + java-version: '17' distribution: 'temurin' - name: Setup Gradle - uses: gradle/gradle-build-action@v2 + uses: gradle/actions/setup-gradle@v3 with: gradle-version: ${{ env.GRADLE_VERSION }} - name: Execute tests @@ -78,24 +78,16 @@ jobs: with: name: test_results path: tests/java/build/reports/tests/test/**/*.* - deploy-github-packages: + deploy-maven-central: runs-on: ubuntu-latest - needs: run-tests-java11 + needs: run-tests-java17 if: github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/') - permissions: - packages: write - contents: write steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Source dependency versions working-directory: config run: | cat versions.txt >> $GITHUB_ENV - - name: Download t2iapi test results artifact - uses: actions/download-artifact@v3 - with: - name: test_results - path: test_results/ - name: Detect release version if: startsWith(github.ref, 'refs/tags/v') run: | @@ -107,21 +99,24 @@ jobs: wget https://github.com/protocolbuffers/protobuf/releases/download/v${{ env.COMMON_PROTOC_VERSION }}/protoc-${{ env.COMMON_PROTOC_VERSION }}-linux-x86_64.zip -P ${{ runner.temp }}/protoc_install unzip ${{ runner.temp }}/protoc_install/*.zip -d ${{ runner.temp }}/protoc_install/protoc echo "${{ runner.temp }}/protoc_install/protoc/bin" >> $GITHUB_PATH - - name: Set up JDK 11 - uses: actions/setup-java@v3 + - name: Set up Java for publishing to Maven Central Repository + uses: actions/setup-java@v4 with: - java-version: '11' + java-version: '17' distribution: 'temurin' - name: Setup Gradle - uses: gradle/gradle-build-action@v2 + uses: gradle/actions/setup-gradle@v3 with: gradle-version: ${{ env.GRADLE_VERSION }} - name: Publish package working-directory: ./java - run: ./gradlew publish + run: | + ./gradlew publish env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - + MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }} + MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }} + MAVEN_GPG_PASSPHRASE: ${{ secrets.MAVEN_GPG_PASSPHRASE }} + MAVEN_GPG_PRIVATE_KEY: ${{ secrets.MAVEN_GPG_PRIVATE_KEY }} - name: Install zip if: startsWith(github.ref, 'refs/tags/') uses: montudor/action-zip@v1 @@ -140,4 +135,4 @@ jobs: java/build/publications/maven/pom-default.xml java-test-results.zip prerelease: ${{ !startsWith(github.ref, 'refs/tags/v') }} - body_path: CHANGELOG.md + body_path: CHANGELOG.md \ No newline at end of file diff --git a/java/build.gradle.kts b/java/build.gradle.kts index 1dd8fae..7aa07ca 100644 --- a/java/build.gradle.kts +++ b/java/build.gradle.kts @@ -5,6 +5,7 @@ plugins { `maven-publish` id("com.google.protobuf") version "0.9.1" id("com.google.osdetector") version "1.7.1" + signing } val configFile = File("../config/versions.txt").readLines() @@ -14,9 +15,10 @@ val protocVersion = configFileMap["JAVA_PROTOC_VERSION"] val grpcVersion = configFileMap["JAVA_GRPC_VERSION"] val baseVersion = configFileMap["BASE_PACKAGE_VERSION"]!! val buildId: String? = System.getenv("GITHUB_RUN_NUMBER") -val t2iapiVersion: String = when (System.getenv("RELEASE_VERSION") == "1") { +val isRelease = System.getenv("RELEASE_VERSION") == "1" +val t2iapiVersion: String = when (isRelease) { true -> baseVersion - false -> baseVersion + ( buildId?.let { ".$it" } ?: "" ) + false -> baseVersion + ( buildId?.let { ".$it" } ?: "" ) + "-SNAPSHOT" } version = t2iapiVersion @@ -44,6 +46,11 @@ tasks.compileJava { targetCompatibility = "11" } +java { + withJavadocJar() + withSourcesJar() +} + protobuf { protoc { // The artifact spec for the Protobuf Compiler @@ -74,6 +81,26 @@ publishing { from(components["java"]) pom { name.set("test 2 interface api") + url.set("https://github.com/Draegerwerk/t2iapi") + description.set( + "t2iapi describes a product-independent interface to manipulate devices which utilize" + + " ISO/IEEE 11073 SDC during verification." + ) + + scm { + connection.set("scm:git:git://github.com/Draegerwerk/t2iapi.git") + developerConnection.set("scm:git:ssh://github.com/Draegerwerk/t2iapi.git") + url.set("https://github.com/Draegerwerk/t2iapi") + } + + developers { + developer { + id.set("t2i") + name.set("T2I Team") + email.set("t2i@draeger.com") + } + } + licenses { license { name.set("MIT License") @@ -86,12 +113,27 @@ publishing { repositories { maven { - name = "GitHubPackages" - url = uri("https://maven.pkg.github.com/Draegerwerk/t2iapi") + name = "Sonatype" + + val releaseUrl = "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/" + val snapshotsUrl = "https://s01.oss.sonatype.org/content/repositories/snapshots/" + + url = uri(if (isRelease) releaseUrl else snapshotsUrl) credentials { - username = System.getenv("GITHUB_ACTOR") - password = System.getenv("GITHUB_TOKEN") + username = System.getenv("MAVEN_USERNAME") + password = System.getenv("MAVEN_PASSWORD") } } } +} + +signing { + useInMemoryPgpKeys(System.getenv("MAVEN_GPG_PRIVATE_KEY"), System.getenv("MAVEN_GPG_PASSPHRASE")) + sign(publishing.publications["maven"]) +} + +tasks.javadoc { + if (JavaVersion.current().isJava9Compatible) { + (options as StandardJavadocDocletOptions).addBooleanOption("html5", true) + } } \ No newline at end of file diff --git a/tests/java/build.gradle.kts b/tests/java/build.gradle.kts index 227480d..a5dec92 100644 --- a/tests/java/build.gradle.kts +++ b/tests/java/build.gradle.kts @@ -17,7 +17,7 @@ val baseVersion = configFileMap["BASE_PACKAGE_VERSION"]!! val buildId: String? = System.getenv("GITHUB_RUN_NUMBER") val t2iapiVersion: String = when (System.getenv("RELEASE_VERSION") == "1") { true -> baseVersion - false -> baseVersion + ( buildId?.let { ".$it" } ?: "" ) + false -> baseVersion + ( buildId?.let { ".$it" } ?: "" ) + "-SNAPSHOT" } dependencies {