-
Creating a WAR (Web Application Archive) file for a Spring Boot project involves configuring your project to package as a WAR instead of the default JAR (Java ARchive).
-
This process requires some changes in your pom.xml file (for Maven projects) and your main application class. Below are the steps to achieve this:
-
Ensure the packaging type is set to war in your pom.xml.
<packaging>war</packaging>
- Ensure you have the spring-boot-starter-web dependency.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
- To avoid packaging the embedded Tomcat server, set its scope to provided.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
- Ensure the Spring Boot Maven Plugin is present (The Spring Boot Maven plugin helps in packaging the project.)
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
-
To create a WAR file, you need to extend SpringBootServletInitializer in your main application class. This allows the application to be deployed to an external application server.
-
Extend SpringBootServletInitializer and Override configure Method
@SpringBootApplication
public class MySpringBootApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(MySpringBootApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(MySpringBootApplication.class);
}
}
- With the pom.xml configured and the main class modified, you can now build your project to create the WAR file. Use the following Maven command:
mvn clean package
This command cleans the project and packages it, generating the WAR file in the target directory.
- The generated WAR file can now be deployed to any standard Servlet container like Apache Tomcat, Jetty, JBoss, etc.
- With Homebrew installed, you can easily install Maven by running:
brew install maven
- Verify Maven Installation After the installation is complete, verify that Maven is installed correctly by checking its version:
mvn -version
Apache Maven 3.9.7 (8b094c9513efc1b9ce2d952b3b9c8eaedaf8cbf0)
Maven home: /opt/homebrew/Cellar/maven/3.9.7/libexec
Java version: 1.8.0_202, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/jre
Default locale: en_IN, platform encoding: UTF-8
OS name: "mac os x", version: "10.16", arch: "x86_64", family: "mac"
# Apache Tomcat
// https://www.youtube.com/watch?v=c7qZAL1bHi0
Mastering Apache Tomcat : A Comprehensive Guide For Webserver Setup
change port number
- go to conf >> server.xml
-Add Users to Tomcat
go to tomcat-users.xml >>
<user username="admin" password="admin" roles="manager-gui" />
Use of Servers Using Eclips IDE
- go to windows >> show view >> Server
-now we will see at bottom server tab then click on it.
press right click New >> Server >> Apache >>
# Maven
// https://www.youtube.com/watch?v=hV1OWzYpzxo&t=236s
Apache Maven Setup In Windows Machine
---------------------------------------
Step 1: Prerequisites
Ensure that you have Java Development Kit (JDK) installed on your Windows machine. Maven requires JDK to be installed.
- Download and install Java Software.
https://www.oracle.com/in/java/technologies/downloads/#jdk17-windows
- set JAVA_HOME in Environment Variables (System Environment)
- set path for Java
-verify Java Installation.
java -version
Step 2: Download Apache Maven Software
Apache Maven 3.9.6 is the latest release: it is the recommended version for all users.
-https://maven.apache.org/download.cgi
-https://dlcdn.apache.org/maven/maven-3/3.9.6/binaries/apache-maven-3.9.6-bin.zip
-Extract Maven Zip File
-set MAVEN_HOME in System Environment Variables.
-set path for Maven in System Environment Variables.
-verify Maven Installation
mvn -v
Step 3: Create Maven Project
-Run the following Maven command to create a new Maven project.
-Replace groupId, artifactId, and version with your desired values.
mvn archetype:generate -DgroupId=com.example -DartifactId=my-project -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
-Navigate to Project Directory
cd my-project
-Build the Project
This will compile the source code, run any tests, and package the application (by default, it creates a JAR file in the target directory).
mvn package / mvn clean package