logo
0
0
Login
docs: 更新文档链接

Spring Boot + Gradle + Docker

badge badge badge

In this example:

  • Using Cloud Native Build to package Spring Boot + Gradle project, build Docker image and publish to artifact repository
  • Configured build.gradle and gradle-wrapper.properties to use Tencent Cloud's Maven Mirror and Gradle Wrapper Mirror for acceleration

Prerequisites

  1. Use Cloud Native Build (CNB) to build your project
  2. Write a Dockerfile
  3. Declarative build cache
  4. Understand environment variables and their usage
  5. Learn how to use Docker artifact repository
  6. Configure .ide/Dockerfile for Cloud Native Development

Quick Start

Run Service

  1. Command line execution
# Run in terminal $ ./gradlew bootRun
  1. Debug Mode
  • Click the "Run and Debug" icon in the activity bar on the left side of VS Code, select "Launch file" from the run and debug drop-down list, and then click the green run button (or press F5 directly). View configuration

Unit Testing

The project integrates JUnit 5 and Mockito testing frameworks, and uses Jacoco for code coverage analysis.

Run Unit Tests

Run all tests:

# Run all unit tests $ ./gradlew test

View test report:

# Test report location $ open build/reports/tests/test/index.html

Code Coverage Report

The project uses Jacoco plugin to generate code coverage reports:

# Generate code coverage report $ ./gradlew jacocoTestReport # View coverage report $ open build/reports/jacoco/index.html

Code Quality Check

Run comprehensive quality checks (including tests, coverage and code style):

# Run all code quality checks $ ./gradlew codeQualityCheck

Code Quality Check

The project integrates Checkstyle to ensure code quality and consistency. Checkstyle is a development tool that helps Java programmers adhere to coding standards.

Run Checkstyle Check

You can run Checkstyle check with the following command:

# Run Checkstyle check $ ./gradlew checkstyleMain # Generate Checkstyle HTML report $ ./gradlew checkstyleReport

The generated HTML report is located at build/reports/checkstyle/main.html.

Customize Checkstyle Rules

The Checkstyle configuration file is located at config/checkstyle/checkstyle.xml, which uses Google Java style guide by default. You can modify this file to customize the check rules according to project requirements.

Main check rules include:

  • Code format and indentation
  • Naming conventions for variables, methods and classes
  • Whitespace and newline rules
  • Comments and Javadoc requirements
  • Import statement specifications
  • Code structure and design
  • Common issue: When Gradle builds Java projects, it downloads Gradle Wrapper according to distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip in gradle/wrapper/gradle-wrapper.properties under project root directory, which can be slow.
  • Acceleration solution: Change distributionUrl in gradle/wrapper/gradle-wrapper.properties to distributionUrl=https\://mirrors.cloud.tencent.com/gradle/gradle-7.6-bin.zip Reference: Gradle download address.

Single project build: modify build.gradle file in project root directory

repositories { mavenLocal() maven { url "https://mirrors.cloud.tencent.com/nexus/repository/maven-public/" } mavenCentral() }

Multi-project build: modify build.gradle file in project root directory

allprojects { repositories { mavenLocal() maven { url "https://mirrors.cloud.tencent.com/nexus/repository/maven-public/" } mavenCentral() } }