Code Monkey home page Code Monkey logo

thriving-dev / java-library-template Goto Github PK

View Code? Open in Web Editor NEW
30.0 1.0 3.0 573 KB

🎨 Java library template β€’ Gradle Kotlin DSL β€’ GitHub Actions CI/CD to build, release & publish to Maven Central β€’ Renovate β€’ Trivy β€’ Javadoc (Pages) β€’ Issue & PR Templates

Home Page: https://www.youtube.com/watch?v=nXs7hSV6ris&list=PL1tfxqbktkFYK5zYjfHxt4wFOus5R9Y3g&index=1

License: Other

Java 100.00%
github-actions github-template github-templates gradle gradle-multi-project java java-library library maven-central renovate

java-library-template's Introduction

java-library-template 🎨

Use this template Java CI Maven Central Contributor Covenant Javadoc CC BY-NC-SA 4.0

TLDR: Java Library GitHub Template Repository

Features

  • πŸ₯· 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)

Quick Start

Demo on YouTube: java-library-template 1/7: Getting started (generate from; create PAT, one-click migrate)

  1. Use this template to create your own repository

  2. 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):

    1. 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'.

    2. 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)

    3. 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
      image
    4. Provide your new PAT either as an Organisation secret or a Repository secret with the name CI_GITHUB_TOKEN.

  3. 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)

    image

  4. Final one-off tasks

Project Structure

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

CI/CD Pipeline

The heart of this template is the 'Main GitHub Actions CI/CD Pipeline'. See it in Actions (πŸ‘»).

image

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:

  1. Check. Build, test, integration test; code quality & vulnerability scans.
    Runs for active PRs - as well as part of all subsequent listed use cases.
  2. Latest. 'Check', publish SNAPSHOT version to Maven Central and Javadoc (GitHub Pages).
    Runs on pushes to the main branch.
  3. Release Process. 'Check', executes (major|minor|patch) release process via the Gradle plugin.
    Manually triggered workflow via GitHub UI/API.
  4. Library Release. 'Check', publish RELEASE version to Maven Central and Javadoc (GitHub Pages).
    Runs for pushed tags.

Publish to Maven Central

Automated Process

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)

Prerequisites: Sonatype Credentials & GPG Signing Key

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 The username of your OSSRH account. The first part of your OSSRH (Sonatype) user access token.
OSSRH_PASSWORD The password of your OSSRH account. The second part of your OSSRH (Sonatype) user access token.
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) Preview of Javadoc published to GitHub Pages by the CI/CD pipeline

Release Process

Creating a Release

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) image

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) image

The new version is automatically published to Maven Central! πŸš€ image

Prerequisites: PAT provided as CI_GITHUB_TOKEN

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.

Javadoc

Use

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

Prerequisites: Configure GitHub Pages

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)

image

Security & CodeQL Analysis

Common Vulnerabilities and Exposures (CVE)

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

  1. with each main CI/CD pipeline run
  2. Scheduled (weekly) (ref)
Preview of a critical CVE listed in the GitHub Security 'Code scanning' overview page

Please refer to official GitHub documentation for more details.

Automated Dependency Updates with Renovate

ℹ️ 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.

Dependency Dashboard

PRs created by renovate bot

Prerequisites: Enable & Configure Renovate

This template ships with a prepared renovate.json.

The recommended way to enable renovate is to use the Renovate GitHub App.

Credits

java-library-template by thriving.dev is licensed under CC BY-NC-SA 4.0

java-library-template's People

Contributors

actions-user avatar benny123tw avatar hartmut-co-uk avatar renovate[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

java-library-template's Issues

Replace renovate with dependabot

Describe the feature

Instead of using renovate, the template should switch to dependabot.

Why?
Because it reduces the configuration of this template.
Users don't have to install anything.
As soon as a .github/dependabot.yml file is present, dependabot is configured and ready to go.

I'm not familar with renovate, however, you might lose the ability to update Gradle itself.
As far as I know renovate can handle Gradle updates while dependabot can't.
But this could also be solved by using e.g. the Wrapper-Gradle-Plugin.

Additional information

  • Would you be willing to help implement this feature?

Final checks

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/0.initial.migrate-repo-template.yml
  • actions/checkout v4
.github/workflows/callable.build.yml
  • actions/checkout v4
  • actions/setup-java v4
  • gradle/actions v3
  • gradle/actions v3.5.0
  • mikepenz/action-junit-report v4
.github/workflows/callable.code-analysis.codeql.yml
  • actions/checkout v4
  • github/codeql-action v3
  • actions/setup-java v4
  • gradle/actions v3
  • gradle/actions v3.5.0
  • github/codeql-action v3
.github/workflows/callable.code-analysis.trivy.yml
  • actions/checkout v4
  • actions/setup-java v4
  • gradle/actions v3
  • gradle/actions v3.5.0
  • github/codeql-action v3
.github/workflows/callable.gradle-release.yml
  • actions/checkout v4
  • actions/setup-java v4
  • gradle/actions v3
  • gradle/actions v3.5.0
.github/workflows/callable.integration-test.yml
  • actions/checkout v4
  • actions/setup-java v4
  • gradle/actions v3
  • gradle/actions v3.5.0
  • mikepenz/action-junit-report v4
.github/workflows/callable.publish-javadoc.yml
  • actions/checkout v4
  • actions/setup-java v4
  • gradle/actions v3
  • gradle/actions v3.5.0
  • actions/upload-artifact v4
  • actions/checkout v4
  • actions/download-artifact v4
.github/workflows/callable.publish-sonatype.yml
  • actions/checkout v4
  • actions/setup-java v4
  • gradle/actions v3
  • gradle/actions v3.5.0
gradle
gradle.properties
settings.gradle.kts
build.gradle.kts
gradle/libs.versions.toml
  • org.junit.jupiter:junit-jupiter 5.10.3
  • org.assertj:assertj-core 3.26.3
  • org.testcontainers:testcontainers 1.20.0
  • org.testcontainers:junit-jupiter 1.20.0
  • io.github.gradle-nexus.publish-plugin 2.0.0
  • net.researchgate.release 3.0.2
java-library-template/build.gradle.kts
gradle-wrapper
gradle/wrapper/gradle-wrapper.properties
  • gradle 8.9

  • Check this box to trigger a request for Renovate to run again on this repository

Enhancing Java Library Template with Automated JMH Benchmark Integration

Describe the feature

In the spirit of fostering high-performance Java library development, I'd like to propose an enhancement to your Java library template: the integration of automated Java Microbenchmark Harness (JMH) benchmarks. By incorporating the "benchmark-action/github-action-benchmark" GitHub Action, we can offer an out-of-the-box solution for performance benchmarking in any library created from this template.

This integration would automatically run JMH benchmarks against pull requests, providing immediate feedback if a change negatively impacts performance beyond an acceptable threshold. It's a proactive measure to ensure that all libraries developed using your template adhere to the highest standards of performance from the outset. Not only does this streamline the development process for library maintainers, but it also instills a culture of performance awareness and optimization within the community.

Automating benchmarking through this GitHub Action eliminates the need for manual performance checks, making the library development process more efficient and reliable. It allows contributors to focus on innovation and functionality, secure in the knowledge that performance standards are being upheld automatically.

Additional information

  • Would you be willing to help implement this feature?

Final checks

CICD job 'integration test' does not publish test results

Describe the bug

The 'integration test' mikepenz/action-junit-report@v4 always publishes empty test reports.

Library version(s) affected

<= 1.1.1

Steps to reproduce

Check any build pipelines for 'integration test' results:
image

Additional context, environment

No response

Logs

Run mikepenz/action-junit-report@v4
  with:
    report_paths: **/build/test-results/test/TEST-*.xml
    token: ***
    annotate_only: false
    check_annotations: true
    update_check: false
    check_name: JUnit Test Report
    fail_on_failure: false
    require_tests: false
    require_passed_tests: false
    include_passed: false
    check_retries: false
    transformers: []
    job_summary: true
    detailed_summary: false
    annotate_notice: false
    follow_symlink: false
    job_name: intTest
    truncate_stack_traces: true
  env:
    JAVA_HOME: /opt/hostedtoolcache/Java_Corretto_jdk/21.0.2-13.1/x64
    JAVA_HOME_21_X64: /opt/hostedtoolcache/Java_Corretto_jdk/21.0.2-13.1/x64
    GRADLE_BUILD_ACTION_SETUP_COMPLETED: true
    GRADLE_BUILD_ACTION_CACHE_RESTORED: true
πŸ“˜ Reading input values
πŸ“¦ Process test results
  Retrieved 1 reports to process.
  ℹ️ Posting with conclusion 'success' to refs/heads/main (sha: 9207686e078f830d4a52baa0bd7706803dddaedb)
πŸš€ Publish results
  ℹ️ - JUnit Test Report - No test results found!
  ℹ️ - JUnit Test Report - Creating check (Annotations: 0)

The gradle/actions/setup-gradle action supercedes gradle/gradle-build-action

Describe the feature

ref https://github.com/gradle/gradle-build-action/releases/tag/v3.1.0

As of v3 this action has been superceded by gradle/actions/setup-gradle.
Any workflow that uses gradle/gradle-build-action@v3 will transparently delegate to gradle/actions/setup-gradle@v3.

Users are encouraged to update their workflows, replacing:

uses: gradle/gradle-build-action@v3

with

uses: gradle/actions/setup-gradle@v3

See the setup-gradle documentation for up-to-date documentation for gradle/actions/setup-gradle.

For release details, see https://github.com/gradle/actions/releases/tag/v3.1.0

Additional information

  • Would you be willing to help implement this feature?

Final checks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.