Monday, March 16, 2020

Deploy static resource in war file using Maven

In this post I will demonstrate how we can deploy simple static file in a war file. This will only use maven and will not use any other framework.


Prerequisites 


  1. You should have install java 1.8 or above.
  2. You should have Eclipse/IntelliJ Idea installed in your PC.
  3. Your PC should setup Maven installed and configured.


Create the Project

In InteliJIdea go to Files - > New -> Project select default JDK installed and click next as show in below figure.



Then provide Project name and GroupID to your project. I will use following details

Name      : StaticWebResource
GroupID : com.nirmal.blog

Then click Finish.


Add relevant dependencies Project

Lets add relevant dependencies in to the project. Just modify project pom file as show below.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.nirmal.blog</groupId>
    <artifactId>StaticWebResource</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <finalName>StaticWebResource</finalName>
    </build>

</project>

Make sure to add maven-war-plugin and set the configuration "failOnMissingWebXml"  value as false.



Add static resource in to Project

For the demonstration purposes I will use "jquery.min.js". First download this file in to local PC. Then create new folder call webapp. Then put your static resources in this folder. In my demonstration I will put downloaded jquery.min.js file.




Lets Create the war file

To create the war file just perform maven install (Just double click on install under lifecycle in Maven section ) and then you can see the generated war file inside target folder.



Deploy in to JBoss server

Log in to the JBoss web admin console and navigate to deployments tab. Then click on Add button and Then click Next. Select war file and again click next and Click Finish. Application will successfully deployed in to the JBoss server.

Now you should be able to access the deployed static resource from following URL.

http://localhost:8080/StaticWebResource/jquery.min.js

Loaded static resource file. 
You can find project source on following GIT resource.



No comments:

Post a Comment