Skip to content

little-pinecone/spring-boot-angular-scaffolding

Repository files navigation

spring-boot-angular-scaffolding

keep growing logo

Quality Gate Status Maintainability Rating Reliability Rating

Bugs Coverage Vulnerabilities Code Smells Technical Debt

GitHub

To learn how to set up a project like this one, check out the Integrate Angular with a Spring Boot project post.

This project is a multi-module application, using the following frameworks: Spring Boot for the backend and Angular for the frontend. The project can be built into a single jar file using Maven.

Getting Started

Prerequisites

Backend:

Frontend:

Running the project

First, clone this repository.

Then, build it locally with:

mvn clean install

You can run the project from command line with:

mvn spring-boot:run -Dspring-boot.run.profiles=dev

As a result, you should be able to visit the home page on http://localhost:8080/:

home page screenshot

Profiles summary

The project can be built with various different profiles to allow for flexible configuration. Below you'll find a short summary of the available profiles.

Spring profiles

  • dev - for local development. Allows things like handling requests from http://localhost:4200/.
  • angular - special profile used for client code generation. Applied automatically when the angular Maven profile is enabled.

Maven profiles

  • sonar-cloud - for code analysis on push to master
  • code-coverage - for including code coverage reports from the backend module during a sonar analysis
  • frontend-pre-sonar - for including code coverage reports from the frontend module during a sonar analysis
  • frontend-sonar - for running only a sonar analysis for the frontend module
  • angular - for generating client code

API documentation

The backend module serves one endpoint for testing purposes. First, build and run the application. Then you'll be able to reach the API docs.

Swagger

The Swagger UI page: http://localhost:8080/swagger-ui.html.

OpenAPI

Client code generation

Compatibility

Right now openapi-generator supports Angular 11.0.0. Although the generated code works with Angular 12+ used in this project, it's strongly recommended to update the generator plugin as soon as it starts supporting Angular 12.0.0 (see Issue #9511).

Generating code

To run client code generation using the openapi-generator-maven-plugin execute the following command:

cd backend
mvn clean verify -Pangular -DskipTests

The application will be started so that the API specification can be obtained from the Open API endpoint. The generated code is available in the frontend/src/main/angular/src/backend directory. Don't edit those files manually.

Working with frontend on a local environment

If you want to see how changes you make in the frontend code affect the application you don't need to build it together with the backend module every time (mvn clean install). Use the following commands:

mvn spring-boot:run -Dspring-boot.run.profiles=dev
cd frontend/src/main/angular
ng serve

and visit http://localhost:4200/. The frontend application reloads automatically on code change.

Running test suits

Backend

Run unit tests with the following command in the project directory:

mvn test

Run all tests with the following command in the project directory:

mvn verify

Frontend

Run all tests for the Angular code with:

cd frontend/src/main/angular
ng test

SonarQube analysis on a local environment

SonarQube Prerequisites

Full analysis

You can run analysis for the whole project (both backend and frontend):

mvn clean verify sonar:sonar -Pfrontend-pre-sonar -Pcode-coverage -Dsonar.login=your_username -Dsonar.password=your_password

Backend analysis

You can run a separate analysis for the backend module:

cd backend
mvn clean verify sonar:sonar -Pcode-coverage -Dsonar.login=your_username -Dsonar.password=your_password

Frontend analysis

You can run a separate analysis for the frontend module:

cd frontend
mvn sonar:sonar -Pfrontend-pre-sonar -Dsonar.login=your_username -Dsonar.password=your_password

Or use the shell script which you can find in the closing paragraphs of the How to add an Angular module built with Maven to a SonarQube analysis post.

Verifying results

Visit the Projects page and choose the right project. Depending on which modules were analysed you should see one, two, or three projects.

Forking this repository

Update project metadata

  • Make sure to provide your own values for <name>, <description>, <artifactId> and <groupId> in the pom.xml, backend/pom.xml and frontend/pom.xml files.

GitHub Actions and SonarCloud analysis

  • Remove the .github/workflows/build.yml file if you don't want to configure GitHub Actions for your repository.
  • If you don't want to configure SonarCloud for your repository: remove the sonar-cloud profile from pom.xml, <sonar.projectKey> and <sonar.projectName> from backend/pom.xml and all <sonar…> properties from frontend/pom.xml
  • If you do want to configure GitHub Actions and SonarCloud analysis, add your own SONAR_TOKEN to your repository and customize all mentioned above<sonar…> properties in the pom.xml files.

Built With

Table of contents generated with markdown-toc