GithubHelp home page GithubHelp logo

andredesousa / essential-spring-scaffold Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 1.0 100 KB

This project provides a baseline to help you kick start a Spring project.

License: MIT License

Java 85.95% Mustache 14.05%
java springframework spring-boot junit5 mockito swagger openapi ci unit-testing integration-testing docker ansible swarm prettier checkstyle pmd changelog gradle restful

essential-spring-scaffold's Introduction

Essential Spring Scaffold

The Spring Framework is an application framework and inversion of control container for the Java platform. This project was generated with Spring Initializr. It has a complete RESTful API configured, including build, test, and deploy scripts as examples.

Table of Contents

Project structure

When working in a large team with many developers that are responsible for the same codebase, having a common understanding of how the application should be structured is vital. Based on best practices from the community, Spring, other github projects and developer experience, your project should look like this:

├── ci
|  ├── build
|  └── deploy
├── gradle
├── src
|  ├── integrationTest
|  ├── main
|  |  ├── java
|  |  |  └── app
|  |  |    ├── Application.java
|  |  |    ├── AppController.java
|  |  |    ├── AppService.java
|  |  |    └── Swagger.java
|  |  └── resources
|  |    ├── application-prod.properties
|  |    └── application.properties
|  └── test
├── .editorconfig
├── .gitignore
├── .prettierrc.json
├── build.gradle
├── CHANGELOG.md
├── changelog.mustache
├── checkstyle.xml
├── gradlew
├── gradlew.bat
├── LICENSE
├── README.md
└── settings.gradle

All of the app's code goes in a folder named src/main. The unit tests and integration test are in the src/test and src/integrationTest folders. Static files are placed in src/main/resources folder.

Available gradle tasks

The tasks in build.gradle file were built with simplicity in mind to automate as much repetitive tasks as possible and help developers focus on what really matters.

The next tasks should be executed in a console inside the root directory:

  • ./gradlew tasks - Displays the tasks runnable from root project 'app'.
  • ./gradlew bootRun - Runs this project as a Spring Boot application.
  • ./gradlew check - Runs all checks.
  • ./gradlew test - Runs the unit tests.
  • ./gradlew integrationTest - Run the integration tests.
  • ./gradlew lint - Runs several static code analysis.
  • ./gradlew format - Applies code formatting steps to source code in-place.
  • ./gradlew clean - Deletes the build directory.
  • ./gradlew javadoc - Generates Javadoc API documentation for the main source code.
  • ./gradlew generateOpenApiDocs - Generates the spring doc openapi file.
  • ./gradlew generateChangelog - Generates a changelog from GIT repository.
  • ./gradlew dependencyUpdates - Displays the dependency updates for the project.
  • ./gradlew build - Assembles and tests this project.
  • ./gradlew bootJar - Assembles an executable jar archive containing the main classes and their dependencies.
  • ./gradlew bootBuildImage - Builds an OCI image of the application using the output of the bootJar task.
  • ./gradlew release - Performs release, creates tag and pushes it to remote.
  • ./gradlew deploy - Deploys the application to Docker Swarm.
  • ./gradlew help - Displays a help message.

For more details, read the Command-Line Interface documentation in the Gradle User Manual.

Development mode

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run". Use ./gradlew bootRun to run this project as a Spring Boot application.

Beyond that, you can serve the app using continuous build which will automatically re-execute builds on file change. Run ./gradlew classes -t and ./gradlew bootRun on different terminals to enable auto restart on file change. However, this functionality is provided by Spring Boot DevTools.

This app includes Swagger. It is available at http://localhost:8080/swagger-ui/. The OpenAPI Specification is automatically generated. Run ./gradlew generateOpenApiDocs to see the file openapi.json.

Linting and formatting code

A linter is a static code analysis tool used to flag programming errors, bugs, stylistic errors and suspicious constructs.

It includes Prettier, Checkstyle, PMD and SpotBugs:

  • Prettier enforces a consistent style by parsing your code and re-printing it with its own rules, wrapping code when necessary.
  • Checkstyle finds class design problems, method design problems, and others. It also has the ability to check code layout and formatting issues.
  • PMD finds common programming flaws like unused variables, empty catch blocks, unnecessary object creation, and so forth.
  • SpotBugs is used to perform static analysis on Java code. It looks for instances of "bug patterns".

Use ./gradlew lint to analyze your code. Many problems can be automatically fixed with ./gradlew format task. Depending on our editor, you may want to add an editor extension to lint and format your code while you type or on-save.

Running unit tests

Use ./gradlew test to execute the unit tests via JUnit 5 and Mockito. Use ./gradlew test -t to keep executing unit tests in real time while watching for file changes in the background. You can see the HTML report opening the index.html file in your web browser.

It's a common requirement to run subsets of a test suite, such as when you're fixing a bug or developing a new test case. Gradle provides different mechanisms. For example, the following command lines run either all or exactly one of the tests in the SomeTestClass test case:

./gradlew test --tests SomeTestClass

For more details, you can see the Test filtering section of the Gradle documentation.

This project uses JaCoCo which provides code coverage metrics for Java. The minimum code coverage is set to 80%. You can see the HTML coverage report opening the index.html file in your web browser.

Running integration tests

Use ./gradlew integrationTest to execute the integration tests via JUnit 5 and others. Use ./gradlew integrationTest -t to keep executing your tests while watching for file changes in the background. You can see the HTML report opening the index.html file in your web browser.

Like unit tests, you can also run subsets of a test suite. See the Test filtering section of the Gradle documentation.

Debugging

You can debug the source code, add breakpoints, inspect variables and view the application's call stack. Also, you can use the IDE for debugging the source code, unit and integration tests. These functionalities are provided natively or using Spring Boot DevTools.

You can customize the log verbosity of gradle tasks using the -i or --info flag.

Commit messages convention

In order to have a consistent git history every commit must follow a specific template. Here's the template:

<type>(<ITEM ID>?): <subject>

Type

Must be one of the following:

  • build: Changes that affect the build system or external dependencies (example scopes: Gradle, Maven)
  • ci: Changes to our CI configuration files and scripts (example scopes: Jenkins, Travis, Circle, SauceLabs)
  • chore: Changes to the build process or auxiliary tools and libraries such as documentation generation
  • docs: Documentation only changes
  • feat: A new feature
  • fix: A bug fix
  • perf: A code change that improves performance
  • refactor: A code change that neither fixes a bug nor adds a feature
  • revert: A commit that reverts a previous one
  • style: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc.)
  • test: Adding missing tests or correcting existing tests

ITEM ID

The related issue or user story or even defect.

  • For user stories, you shoud use US- as prefix. Example: feat(US-4321): ...
  • For no related issues or defects you should leave it blank. Example: feat: ...

Subject

The subject contains a succinct description of the change.

Build and deployment

In ci folder you can find scripts for your Jenkins CI pipeline and an example for deploying your application with Ansible to Docker Swarm.

This project follows Semantic Versioning and uses git tags to define the current version of the project. Use ./gradlew currentVersion to print the current version extracted from SCM and ./gradlew release to release the current version. Use ./gradlew bootJar to build the project and ./gradlew bootBuildImage to build the Docker image for production. The build artifacts are stored in the build/libs directory. You can deploy this project to Docker Swarm using ./gradlew deploy task.

Reference Documentation

For further reference, please consider the following sections:

essential-spring-scaffold's People

Contributors

andredesousa avatar

Watchers

 avatar

Forkers

p2a8t4a5a

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.