Andrei Pall

Linux Software Engineering

How to deploy Angular and Spring Boot on Tomcat

To deploy a Spring Boot API with an Angular frontend on Apache Tomcat, you'll need to build both applications, combine them properly, and deploy them as a single .war or serve them separately depending on your setup. Here's how to do it step by step:

1. Build the Angular Project

Run this in your Angular project folder:

ng build --configuration production

This generates static files in dist/your-project-name/.

2. Copy Angular Build Output to Spring Boot resources/static

In your Spring Boot project:

Delete or clean src/main/resources/static (optional).

Copy Angular's output:

cp -r dist/your-project-name/* src/main/resources/static/

Spring Boot will serve these files as static web resources (like an SPA) at http://yourdomain.com/index.html.

3. Modify Spring Boot Controller (Optional)

To serve the index.html for all unknown paths (Angular routing), add a fallback controller:

@Controller
public class ForwardController {
    @RequestMapping(value = "/{[path:[^\\.]*}")
    public String redirect() {
        return "forward:/index.html";
    }
}

4. Package Spring Boot as a WAR

Edit pom.xml:

<packaging>war</packaging>

And add this to your SpringBootApplication class:

@SpringBootApplication
public class MyApp extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(MyApp.class);
    }
}

5. Build the WAR

mvn clean package

You’ll get a file like: target/myapp.war

6. Deploy the WAR to Tomcat

Copy the WAR to Tomcat’s webapps/ directory.

Start Tomcat.

Access the app at: http://localhost:8080/myapp/

Newer >>