Trending

How to deploy Angular 6 + Spring Boot app as single deployment unit ?

In this article, we are going to check how to deploy Angular 6 & Spring Boot REST application as a single deployment unit but however best practice is to separate Spring boot and Angular application so that we decouple the client code and server code, hence the application becomes highly scalable and manageable. But sometimes there could be scenarios for example. small application/teams it is advisable to package as a single unit and deploy them. In this article, we are going to check how to use maven resources plugin and spring boot jar packaging to build and deploy as a single unit.

#1.Install Prerequisites

In the first part of the article, we would be creating a new Angular 6 client using Angular CLI & install prerequisites. We would be installing Node.js which is a cross-platform runtime system and runtime environment for applications written in JavaScript language and npm package manager for downloading packages.

  1. Install Node.js from here.
    Image – Install NodeJS

     

    Image – Installation Complete

  2. Confirm Node.js version by following commandsnode -v & npm –v

    Image – Check Node.JS & Npm versions

  3. Install Angular CLI using the following command and we would be creating angular apps using the CLI interface.npm install -g @angular/cli

    Image – Install Angular CLI

  4. Confirm if the installation is successful using the following command.ng -v

    Image – Confirm Angular CLI Installation

  5. With this step, Angular installation is complete.The next step is to create a simple spring boot project.

#2.Create a new Spring Boot and Angular 6 application

  1. Use start.spring.io or create new project from STS
    Image – Create new Spring project

     

    Image – Choose dependencies

     

  2. Create a new controller with request mapping /api/hello and do maven compile to check if the build is a success.

    Image – Create new controller

  3. Create a new angular client using ng new command with Angular CLI

    Image – Create new angular client

  4. Check if we are able to start using npm start the command from the workspace.

    Image – Use npm start to check if we are able to start the new client

  5. We should be able to see the new application http://localhost:4200

    Image – We are able to launch new application

  6. The next step is to import it to STS and modify a few items. From STS, use Import from Projects option to export angular application into STS.

    Image – Import Angular application

  7. Once the import is complete, on project settings exclude node_modules folder & click on apply. We wouldn’t need node_modules a folder because it would be generated during the build process.

    Image – Exclude node_modules folder

  8. Resultant folder post exclusion of node_modules folder.

    Image – Resultant folder excluding node_modules

  9. Modify app.component.css to update color as ‘black’ (optional step)

    Image – (Optional step) modify color

  10. Modify app.component.ts to update the title as required (optional step)

    Image – (Optional Step) update title

  11. Post modifications, run npm start to view the changes

    Image – Run npm start to view the changes

  12. Verify if you’re able to see modifications.

    Image – Verify modifications

  13. Use ng build –prod command for generating production build artifacts.
    Image – Generate Production build artifacts
  14. Post generation of the production build you should see a new folder named ‘dist’.

    Image – Production build artifacts

  15. Now we have both static sources (angular application) dist folder & Spring Boot artifacts.

#3. Use Maven resource plugin to package as a single jar

  1. The next step is to use a maven resource plugin to copy all files from dist folder to /src/main/resources/static the folder to Spring Boot Project. Following is the POM configuration
    <plugin> 
            <artifactId>maven-resources-plugin</artifactId>     
        <executions>  
                   <execution>   
                           <id>copy-resources</id>    
                          <phase>validate</phase> 
                             <goals><goal>copy-resources</goal></goals>             
                 <configuration>                                 
      <outputDirectory>${build.directory}/classes/static/</outputDirectory >      
                                 <resources>    
                                             <resource>       
                                                  <directory>../angular6-client/dist</directory> 
                                                </resource>   
                                     </resources>   
                            </configuration>        
              </execution>   
           </executions> </plugin>
  2. On Maven clean build, you should see a jar with both Angular 6 & Spring Boot application on the target folder.
  3. Execute with Java –jar the command to launch the application, you should see the Angular application served from the static folder.

    Image – Angular application served from Spring Boot Jar

  4. Spring Boot application can also be launched from the same app.
    Image – Spring Boot application from the same jar

     

Congrats! today we have learned how to use maven resources plugin and spring boot jar packaging to build and deploy as a single unit.

The sample code used in this article can be found at Github here.

Useful Resources

Summary
Article Name
How to deploy Angular 6 + Spring Boot app as single deployment unit ?
Description
In this article, we are going to check how to use maven resources plugin and spring boot jar packaging to build and deploy as single unit.
Author
Publisher Name
Upnxtblog
Publisher Logo
Karthik

Allo! My name is Karthik,experienced IT professional.Upnxtblog covers key technology trends that impacts technology industry.This includes Cloud computing,Blockchain,Machine learning & AI,Best mobile apps, Best tools/open source libs etc.,I hope you would love it and you can be sure that each post is fantastic and will be worth your time.

Share
Published by
Karthik

Recent Posts

Developing a Strong Disaster Recovery Plan for Your Business

Operating a business often entails balancing tight schedules, evolving market dynamics, and shifting consumer requirements.…

9 hours ago

How to Secure Your WordPress Hosting by Upgrading Your Login URL

Of course, every site has different needs. In the end, however, there is one aspect…

2 days ago

Social Media Marketing: A Key to Business Success with Easy Digital Life

In today's digital-first world, businesses must adopt effective strategies to stay competitive. Social media marketing…

4 days ago

Best 7 AI Tools Every UI/UX Designer Should Know About

62% of UX designers now use AI to enhance their workflows. Artificial intelligence (AI) rapidly…

6 days ago

How AI Enhances Photoshop Workflow: A Beginner’s Guide

The integration of artificial intelligence into graphic design through tools like Adobe Photoshop can save…

3 weeks ago

The Rise Of Crypto Trading Bots: A New Era In Digital Trading

The cryptocurrency trading world has grown significantly in recent years, with automation playing a key…

4 weeks ago

This website uses cookies.