|
| 1 | +name: java |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: main |
| 6 | + paths: |
| 7 | + - 'java/**' |
| 8 | + - '.github/workflows/java.yml' |
| 9 | + pull_request: |
| 10 | + paths: |
| 11 | + - 'java/**' |
| 12 | + - '.github/workflows/java.yml' |
| 13 | + |
| 14 | +env: |
| 15 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 16 | + |
| 17 | +defaults: |
| 18 | + run: |
| 19 | + working-directory: java |
| 20 | + |
| 21 | +jobs: |
| 22 | + findChangedJavaFiles: |
| 23 | + runs-on: ubuntu-latest |
| 24 | + timeout-minutes: 10 |
| 25 | + outputs: |
| 26 | + isJavaFilesChanged: ${{ steps.setIsJavaFilesChangedOutput.outputs.isJavaFilesChanged }} |
| 27 | + steps: |
| 28 | + - uses: actions/checkout@v6 |
| 29 | + with: |
| 30 | + fetch-depth: 0 |
| 31 | + - name: Get changed files using defaults |
| 32 | + id: changed-files |
| 33 | + uses: tj-actions/changed-files@v47 |
| 34 | + - name: Set output isJavaFilesChanged |
| 35 | + id: setIsJavaFilesChangedOutput |
| 36 | + run: | |
| 37 | + isJavaFilesChanged='false' |
| 38 | + echo "Changed files: ${{ steps.changed-files.outputs.all_changed_files }}" |
| 39 | + for changedFile in ${{ steps.changed-files.outputs.all_changed_files }}; do |
| 40 | + if [[ $changedFile == java/*.java ]] || [[ $changedFile == java/*.xml ]] || [[ $changedFile == java/* ]] || [[ $changedFile == .github/workflows/java.yml ]]; then |
| 41 | + echo "isJavaFilesChanged='true'" |
| 42 | + isJavaFilesChanged='true' |
| 43 | + break |
| 44 | + fi |
| 45 | + done |
| 46 | + echo "isJavaFilesChanged=${isJavaFilesChanged}" >> $GITHUB_OUTPUT |
| 47 | + echo "isJavaFilesChanged: ${isJavaFilesChanged}" |
| 48 | +
|
| 49 | + build: |
| 50 | + needs: [findChangedJavaFiles] |
| 51 | + if: ${{ needs.findChangedJavaFiles.outputs.isJavaFilesChanged == 'true' }} |
| 52 | + runs-on: ubuntu-latest |
| 53 | + timeout-minutes: 15 |
| 54 | + steps: |
| 55 | + - uses: actions/checkout@v6 |
| 56 | + with: |
| 57 | + submodules: true |
| 58 | + - name: Set up JDK 11 |
| 59 | + uses: actions/setup-java@v4 |
| 60 | + with: |
| 61 | + java-version: '11' |
| 62 | + distribution: 'temurin' |
| 63 | + cache: maven |
| 64 | + - name: Build with Maven |
| 65 | + run: mvn -B compile --file pom.xml |
| 66 | + |
| 67 | + test: |
| 68 | + needs: [findChangedJavaFiles, build] |
| 69 | + if: ${{ needs.findChangedJavaFiles.outputs.isJavaFilesChanged == 'true' }} |
| 70 | + runs-on: ubuntu-latest |
| 71 | + timeout-minutes: 15 |
| 72 | + strategy: |
| 73 | + matrix: |
| 74 | + java: ['11', '17', '21'] |
| 75 | + steps: |
| 76 | + - uses: actions/checkout@v6 |
| 77 | + with: |
| 78 | + submodules: true |
| 79 | + - name: Set up JDK ${{ matrix.java }} |
| 80 | + uses: actions/setup-java@v4 |
| 81 | + with: |
| 82 | + java-version: ${{ matrix.java }} |
| 83 | + distribution: 'temurin' |
| 84 | + cache: maven |
| 85 | + - name: Test with Maven |
| 86 | + run: mvn -B test --file pom.xml |
| 87 | + |
| 88 | + format: |
| 89 | + needs: [findChangedJavaFiles] |
| 90 | + if: ${{ needs.findChangedJavaFiles.outputs.isJavaFilesChanged == 'true' }} |
| 91 | + runs-on: ubuntu-latest |
| 92 | + timeout-minutes: 10 |
| 93 | + steps: |
| 94 | + - uses: actions/checkout@v6 |
| 95 | + with: |
| 96 | + submodules: true |
| 97 | + - name: Set up JDK 11 |
| 98 | + uses: actions/setup-java@v4 |
| 99 | + with: |
| 100 | + java-version: '11' |
| 101 | + distribution: 'temurin' |
| 102 | + cache: maven |
| 103 | + - name: Check formatting with Spotless |
| 104 | + run: mvn -B spotless:check --file pom.xml |
| 105 | + |
| 106 | + publishToMavenCentral: |
| 107 | + needs: [test, format, findChangedJavaFiles] |
| 108 | + if: ${{ needs.findChangedJavaFiles.outputs.isJavaFilesChanged == 'true' && github.event_name == 'push' && github.ref == 'refs/heads/main' }} |
| 109 | + runs-on: ubuntu-latest |
| 110 | + timeout-minutes: 30 |
| 111 | + steps: |
| 112 | + - uses: actions/checkout@v6 |
| 113 | + with: |
| 114 | + submodules: true |
| 115 | + - name: Set up JDK 11 |
| 116 | + uses: actions/setup-java@v4 |
| 117 | + with: |
| 118 | + java-version: '11' |
| 119 | + distribution: 'temurin' |
| 120 | + cache: maven |
| 121 | + server-id: ossrh |
| 122 | + server-username: MAVEN_USERNAME |
| 123 | + server-password: MAVEN_PASSWORD |
| 124 | + gpg-private-key: ${{ secrets.GPG_PRIVATE_KEY }} |
| 125 | + gpg-passphrase: MAVEN_GPG_PASSPHRASE |
| 126 | + - name: Check if version already published |
| 127 | + id: version-check |
| 128 | + run: | |
| 129 | + PACKAGE_VERSION=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout) |
| 130 | + PACKAGE_GROUP=$(mvn help:evaluate -Dexpression=project.groupId -q -DforceStdout) |
| 131 | + PACKAGE_ARTIFACT=$(mvn help:evaluate -Dexpression=project.artifactId -q -DforceStdout) |
| 132 | + echo "Package: $PACKAGE_GROUP:$PACKAGE_ARTIFACT:$PACKAGE_VERSION" |
| 133 | +
|
| 134 | + # Check if version exists on Maven Central |
| 135 | + URL="https://repo1.maven.org/maven2/$(echo $PACKAGE_GROUP | tr '.' '/')/$PACKAGE_ARTIFACT/$PACKAGE_VERSION/" |
| 136 | + if curl --head --silent --fail "$URL" > /dev/null 2>&1; then |
| 137 | + echo "Version $PACKAGE_VERSION already exists on Maven Central" |
| 138 | + echo "should_publish=false" >> $GITHUB_OUTPUT |
| 139 | + else |
| 140 | + echo "Version $PACKAGE_VERSION does not exist on Maven Central" |
| 141 | + echo "should_publish=true" >> $GITHUB_OUTPUT |
| 142 | + fi |
| 143 | + - name: Publish to Maven Central |
| 144 | + if: steps.version-check.outputs.should_publish == 'true' |
| 145 | + run: mvn -B deploy -Prelease --file pom.xml |
| 146 | + env: |
| 147 | + MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }} |
| 148 | + MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }} |
| 149 | + MAVEN_GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }} |
| 150 | + |
| 151 | + publishRelease: |
| 152 | + runs-on: ubuntu-latest |
| 153 | + timeout-minutes: 10 |
| 154 | + needs: [publishToMavenCentral] |
| 155 | + if: ${{ needs.findChangedJavaFiles.outputs.isJavaFilesChanged == 'true' && needs.publishToMavenCentral.result == 'success' && github.event_name == 'push' && github.ref == 'refs/heads/main' }} |
| 156 | + steps: |
| 157 | + - uses: actions/checkout@v6 |
| 158 | + with: |
| 159 | + submodules: true |
| 160 | + - name: Set up JDK 11 |
| 161 | + uses: actions/setup-java@v4 |
| 162 | + with: |
| 163 | + java-version: '11' |
| 164 | + distribution: 'temurin' |
| 165 | + cache: maven |
| 166 | + - name: Check if GitHub release already exists |
| 167 | + id: release-check |
| 168 | + run: | |
| 169 | + PACKAGE_VERSION=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout) |
| 170 | + TAG_NAME="java_$PACKAGE_VERSION" |
| 171 | + echo "Checking if release $TAG_NAME already exists" |
| 172 | +
|
| 173 | + # Check if release exists |
| 174 | + if gh release view "$TAG_NAME" >/dev/null 2>&1; then |
| 175 | + echo "Release $TAG_NAME already exists" |
| 176 | + echo "should_create_release=false" >> $GITHUB_OUTPUT |
| 177 | + else |
| 178 | + echo "Release $TAG_NAME does not exist" |
| 179 | + echo "should_create_release=true" >> $GITHUB_OUTPUT |
| 180 | + fi |
| 181 | + env: |
| 182 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 183 | + - name: Create GitHub release |
| 184 | + if: steps.release-check.outputs.should_create_release == 'true' |
| 185 | + run: | |
| 186 | + PACKAGE_VERSION=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout) |
| 187 | + PACKAGE_GROUP=$(mvn help:evaluate -Dexpression=project.groupId -q -DforceStdout) |
| 188 | + PACKAGE_ARTIFACT=$(mvn help:evaluate -Dexpression=project.artifactId -q -DforceStdout) |
| 189 | +
|
| 190 | + # Create release with consistent tag format: java_version |
| 191 | + gh release create "java_${PACKAGE_VERSION}" \ |
| 192 | + --title "[Java] $PACKAGE_VERSION" \ |
| 193 | + --notes "https://central.sonatype.com/artifact/$PACKAGE_GROUP/$PACKAGE_ARTIFACT/$PACKAGE_VERSION" |
| 194 | + env: |
| 195 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments