TLDR: Java Library GitHub Template Repository
Introducing 'java-library-template' in 60s - YouTube
- https://thriving.dev/blog/java-library-development-get-started-quickly-with-java-library-template
- π₯· One-click automated initial project migration workflow (GitHub Action)
- Java 21 (corretto) π€ Gradle Kotlin DSL, version catalog
- GitHub Actions CI/CD pipeline, π· efficient build pipeline, caching, integration tests, test report & failed test annotations
- π One-click release process + publish to Maven Central
- Security & π¦ Vulnerability scan with trivy & GitHub CodeQL Analysis
- Automated dependency updates with Renovate π€
- Javadoc deployed with GitHub Pages
- Open Source Community ready (Code of Conduct, Contribution guidelines, Issue & PR Templates)
Demo on YouTube: java-library-template 1/7: Getting started (generate from; create PAT, one-click migrate)
-
Use this template to create your own repository
-
Create & provide a PAT (Personal Access Token) for the CI/CD pipeline
Three workflows commit and push changes to the repository and therefore require additional permissions. ('migrate-repo-template', 'publish-javadoc', 'gradle-release')
The jobs expect a secret by the name
CI_GITHUB_TOKEN
that holds a PAT with write permission for Content.To create a new access token, the following steps are required (ref GitHub documentation):
-
If the new repo owner is an organisation, enrol the organisation for 'Fine-grained personal access tokens'. In the organisation 'Settings > Third-party Access > Personal access tokens'.
-
Head to the Developer settings and enrol your personal account for the new 'Fine-grained personal access tokens'. (That's a one-off for your account and you might already have done this before)
-
Next, click on the button 'Generate new token' and create a token for the target Resource owner, with access to your project and the following 'Repository Permissions'
- Contents: Read and Write access to code
- Metadata: Read access to metadata
-
Provide your new PAT either as an Organisation secret or a Repository secret with the name
CI_GITHUB_TOKEN
.
-
-
Trigger the '!! INITIAL: Migrate Repo Template !!' workflow
βΉοΈ This workflow automatically 'migrates' all files in your new repository, updating the gradle project group, module name, package names, and all references to the repo
<owner>/<name>
.- Head over to Actions (1)
- on the left-hand side select the topmost workflow '!! INITIAL: Migrate Repo Template !!' (2)
- click the Run workflow button (3)
- fill out the form & start the pipeline (4)(5)
-
Final one-off tasks
- Choose & update the LICENSE, here
- Update Maven Publication details here
- Configure GitHub Pages to deploy branch 'gh-pages' (Javadoc)
- Add secrets required for publishing to Maven Central
- Install & configure renovate app (instructions)
The project template consists of three top-level folders:
.github/
: Defines the GitHub Actions CI tasks and templates for new pull requests, issues, etc.gradle/
: Contains Gradle Configuration files such as the Gradle Version Catalog and the Gradle Wrapper.java-library-template/
: The library source code (Gradle sub-project).
In addition, following files are worth highlighting:
gradle/libs.versions.toml
: A conventional file to declare a version catalog.settings.gradle.kts
: The multi-project Gradle settings file. Here are all the sub-projects defined.gradle.properties
: Holds the library version, needed & maintained by the CI/CD pipeline release process.**/build.gradle.kts
: Gradle build file
The heart of this template is the 'Main GitHub Actions CI/CD Pipeline'. See it in Actions (π»).
The workflow encompasses multiple jobs, modelled and linked with dependencies and conditions. Based on the context (trigger, ref, input arguments) it meets different use cases:
- Check. Build, test, integration test; code quality & vulnerability scans.
Runs for active PRs - as well as part of all subsequent listed use cases. - Latest. 'Check', publish SNAPSHOT version to Maven Central and Javadoc (GitHub Pages).
Runs on pushes to the main branch. - Release Process. 'Check', executes (major|minor|patch) release process via the Gradle plugin.
Manually triggered workflow via GitHub UI/API. - Library Release. 'Check', publish RELEASE version to Maven Central and Javadoc (GitHub Pages).
Runs for pushed tags.
The Maven publish process is fully automated and does not require manual action.
- The main branch (per process definition) always is set to the next SNAPSHOT version and is published to the Sonatype snapshot repository with each main CI/CD pipeline run. The pipeline runs e.g. when a PR is merged, but can also be triggered manually.
- Release deployment happens when a new tag is pushed to GitHub. (Part of the release process)
The initial setup for your OSSRH repository requires some manual steps and human review (see why), after which your deployment process is typically modified to get components into OSSRH. These are all one time steps. I recommend to follow the official guide.
In order to deploy your components to OSSRH with Gradle, you have to meet the requirements for your metadata in the pom.xml as well as supply the required, signed components.
βΉοΈ The publish process uses io.github.gradle-nexus.publish-plugin under the hood.
The gradle project as well as the CI/CD pipeline is already fully prepared for the publishing process. The GH actions job callable.publish-sonatype.yml requires the following Secrets:
Secret name | Value |
---|---|
OSSRH_USERNAME |
|
OSSRH_PASSWORD |
|
GPG_SIGNING_KEY |
The GPG private key to sign your artifacts (in ascii-armored format). You can obtain it with gpg --armor --export-secret-keys <[email protected]> or you can create one key online on pgpkeygen.com. |
GPG_SIGNING_PASSPHRASE |
The passphrase for unlocking the secret key. (you picked it when creating the key). |
Please define the secrets via your repository settings. (Settings > Security > Secrets and variables > Actions)
To release a new version via the CI/CD Pipeline, please follow the instructions below.
- Navigate to Actions (1)
- > Main Pipeline (2)
- Click 'Run workflow' button (3)
- Select a semver release type with the 'Release Library' dropdown (4)
- 'Run the workflow' (5)
The release process includes
- Pipeline run (incl. build & tests) that executes the release plugin (6)
- The release plugin first sets & commits the new version (7a)
- Creates & pushes a new tag (7b)
- Sets the main branch to the next SNAPSHOT version (7c)
The new version is automatically published to Maven Central! π
The CI/CD 'gradle-release' job expects a secret by the name CI_GITHUB_TOKEN
that holds a PAT (Personal Access Token) with permission to push tags as part of the release process.
If you have been following the Quick Start guide you should already have this configured. Please refer to 'Quick Start' step 2.
A Javadoc website of your library, generated by gradle, is 'published' to GitHub Pages by the CI/CD pipeline. In addition to each released version, the current snapshot version (main branch) is published as current
.
-> visit the live preview.
![Preview of Javadoc published to GitHub Pages by the CI/CD pipeline](https://private-user-images.githubusercontent.com/10864443/268627692-119e8055-2755-40d4-8ec4-69bdc2e5339b.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjMzNTU1MDYsIm5iZiI6MTcyMzM1NTIwNiwicGF0aCI6Ii8xMDg2NDQ0My8yNjg2Mjc2OTItMTE5ZTgwNTUtMjc1NS00MGQ0LThlYzQtNjliZGMyZTUzMzliLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MTElMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODExVDA1NDY0NlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTZiNzdjM2I5MGUwNmE3MzIxODdhYWFlYTA4NDEyNmVkN2YxZDAzMDVkMGI1ZmIxZDBiZTQwODdmZWVhYjg5MGMmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.WeKOsKvgzKcg6E7V5ygkqfx8veBb7kbo3kMgupICGCQ)
To host the generated Javadoc, configure GitHub Pages for your repository to deploy from branch gh-pages
. You can also find all deployments under 'pages-build-deployment'.
βΉοΈ The branch is created with the first CI/CD pipeline run. ('Publish javadoc' job)
The libraries gradle dependencies are scanned for known CVE with aquasecurity/trivy. The scan results can be reviewed and managed under 'Security > Vulnerability alerts > Code scanning'.
Scans are triggered
- with each main CI/CD pipeline run
- Scheduled (weekly) (ref)
![Preview of a critical CVE listed in the GitHub Security 'Code scanning' overview page](https://private-user-images.githubusercontent.com/10864443/268745902-2980ba31-32c4-4bd8-9adf-4d852806614b.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjMzNTU1MDYsIm5iZiI6MTcyMzM1NTIwNiwicGF0aCI6Ii8xMDg2NDQ0My8yNjg3NDU5MDItMjk4MGJhMzEtMzJjNC00YmQ4LTlhZGYtNGQ4NTI4MDY2MTRiLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MTElMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODExVDA1NDY0NlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWQwYjFjZTZmODUwZDcwNDlhNjdiODA1NjE0ZDIwMDAxYzgzNzhiYTYxNmY5Y2ZmM2Y4ZDgyZTFmNzAwNDZmNTAmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.RrqPhMfxaDKUsyS9hNGbQ4ovbfK0PALYYIT3GMwJxe8)
Please refer to official GitHub documentation for more details.
βΉοΈ Renovate (RenovateBot) is a software tool that automates the process of keeping software dependencies up-to-date by scanning code repositories, identifying outdated dependencies, and generating automated pull requests to update them.
Itβs Open Source and Community-Driven, supports a wide range of programming languages and package managers, and integrates with GitHub & Gitlab as well as other popular CI/CD systems.
![](https://private-user-images.githubusercontent.com/10864443/268630694-3300d418-8dee-4071-96df-dc53882315fd.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjMzNTU1MDYsIm5iZiI6MTcyMzM1NTIwNiwicGF0aCI6Ii8xMDg2NDQ0My8yNjg2MzA2OTQtMzMwMGQ0MTgtOGRlZS00MDcxLTk2ZGYtZGM1Mzg4MjMxNWZkLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MTElMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODExVDA1NDY0NlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWRiNmQyNDdkNjNiNjhhOWU0NTg3ZWYyMTA2NWJkYWJlMjNkZGJkMGQ5NjEwZjdhNGU3MDZhMDBkMmVkMWFiMzEmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.RN-0T8M3IkESZvDz2rN8T7XevX4_R4Vc3MpM6zDAEiI)
![](https://private-user-images.githubusercontent.com/10864443/268630713-bcd03d8f-f620-4344-85d8-5f29d28be030.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjMzNTU1MDYsIm5iZiI6MTcyMzM1NTIwNiwicGF0aCI6Ii8xMDg2NDQ0My8yNjg2MzA3MTMtYmNkMDNkOGYtZjYyMC00MzQ0LTg1ZDgtNWYyOWQyOGJlMDMwLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MTElMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODExVDA1NDY0NlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWQ0ZjRkY2Y2ZGNkMWQxZTQ1NzZiYjU1NTA0MGJhZTY0NGNjMmM3YzEyNThjOWZmNzM5MzY5NDI1ZmZlMzBjZjYmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.trWmfj3DQtfl3MvyIenZodugs0-HdUsl1OqJ0-kiI4I)
This template ships with a prepared renovate.json.
The recommended way to enable renovate is to use the Renovate GitHub App.
- inspired by https://github.com/cortinico/kotlin-android-template
- PR & issue templates copied / adapted from https://github.com/nuxt/nuxt
java-library-template by thriving.dev is licensed under CC BY-NC-SA 4.0