Skip to main content

Deploying an AngularJS App as a WAR File

· 3 min read
Anand Raja
Senior Software Engineer

Here’s a detailed step-by-step guide to develop an AngularJS application, package it as a .war file, and deploy it with a Java backend.

1. Project Structure

Organize your project like this:

your-project/
├── src/
│ └── main/
│ ├── java/ # Java backend code (e.g., Spring controllers)
│ └── webapp/ # Web resources (AngularJS app, HTML, CSS, JS)
│ ├── index.html
│ └── scripts/
│ └── angular/
│ └── spotfireSpa/
│ └── controller.js
├── pom.xml # Maven configuration

2. Develop the AngularJS Application

  • Place all AngularJS files (controllers, services, etc.) in src/main/webapp/scripts/angular/.
  • Your main HTML file (e.g., index.html) should reference these scripts.
  • Example controller (controller.js):
var spotfireSpaControllers = angular.module('spotfireSpaApp');

spotfireSpaControllers.controller('spotfireSpaCtrl', ['$rootScope','$scope','$http','$timeout', '$window', '$location', function ($rootScope, $scope, $http, $timeout, $window, $location) {
// ...controller logic...
}]);

3. Configure Maven for WAR Packaging

In your pom.xml, ensure you have the maven-war-plugin:

<packaging>war</packaging>

<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.2</version>
<configuration>
<warSourceDirectory>src/main/webapp</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>

4. Build the WAR File

From your project root, run:

mvn clean package
  • This compiles your Java code and copies all files from src/main/webapp (including your AngularJS app) into the WAR.
  • The output will be in target/your-project.war.

A .war file stands for Web Application Archive. It is a packaged file format used to distribute and deploy Java web applications.

Key Points:

  • WAR files bundle all components of a web application:
    • Java classes (servlets, controllers)
    • Libraries (JARs)
    • Static resources (HTML, CSS, JavaScript, images)
    • Configuration files (like web.xml)
  • WAR files are deployed to Java application servers (like Apache Tomcat, Jetty, JBoss).
  • The server unpacks the WAR and serves the application to users.

5. Deploy the WAR File

  • Copy the generated .war file to your Java application server’s deployment directory (e.g., webapps/ in Apache Tomcat).
  • Start or restart your server.
  • Access your app via http://localhost:8080/your-project/.

6. How It Works

  • The server serves static files (HTML, JS, CSS) from the WAR.
  • AngularJS runs in the browser, making API calls to your Java backend.
  • All resources are bundled together for easy deployment.

Summary Table

StepWhat to Do
Project StructurePlace AngularJS in src/main/webapp
Develop FrontendWrite AngularJS code, reference in HTML
Maven ConfigUse maven-war-plugin in pom.xml
BuildRun mvn clean package
DeployCopy WAR to server, access via browser

In this example, Java is not directly used inside the AngularJS code. However, Java is typically required for the following reasons in an AngularJS and Java web application:

  1. Backend Services:
    Java (using frameworks like Spring) provides REST APIs or backend logic. The AngularJS frontend calls these APIs to fetch or update data.

  2. Security & Business Logic:
    Java handles authentication, authorization, and complex business rules on the server side, which should not be exposed to the client.

  3. Packaging & Deployment:
    Java (with Maven) packages both the frontend (AngularJS) and backend into a .war file for deployment on Java application servers (like Tomcat).

In this code:

  • The AngularJS controller manages UI logic and emits events.
  • If there is a need to fetch data or perform actions that require server-side processing, $http is used to call Java backend APIs.
  • The Java backend is essential for anything that cannot or should not be done in the browser (e.g., database access, secure operations).

Summary:
Java is needed for backend processing, API endpoints, and packaging/deployment, while AngularJS handles the frontend UI and user interactions.