Skip to content

Instantly share code, notes, and snippets.

@namila007
Last active May 2, 2018 14:28
Show Gist options
  • Save namila007/1dba836073c7f7ab95044b85c7b205c7 to your computer and use it in GitHub Desktop.
Save namila007/1dba836073c7f7ab95044b85c7b205c7 to your computer and use it in GitHub Desktop.
Deploying Maven Java Web App to an Apache Tomcat server

Deploying Maven Java Web App to Apache Tomcat server

This is about how a simple maven java web app deploy to tomcat server

---How to do---


1 Install Tomcat server

Install the tomcat server (tomcat8 or up) Download tomcat.

2.Install Maven

Get the latest binary.zip file and extract in your desired path. Download maven

3. Setting Environment variables

Add following environmental variables to your system;

  • Maven path : add extracted maven bin path (ex: C:\Program Files\apache-maven-3.5.2\bin)
  • Java Variable: add a variable called JAVA_HOME and give the jdk path, not the bin folder.

4. Creating Maven Java web project

  1. Generate the maven web arch type

    • run following command with suitable names for DgroupId and DartifactId
      • mvn archetype:generate -DgroupId={SOMETHING} -DartifactId={SOMETHING} -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
  2. Add java files and web files

    • Create a folder called java in /YOURARTIFACTIDFOLDER/src/main/ and add your java files
      • for example a project with -DgroupId=test -DartifactId=MavenTest name, java folder is in MavenTest\src\main\Java
    • Add web files to /YOURARTIFACTIDFOLDER\src\main\webapp
    • Folder structure is like below;
      • screenshot_1

5. Configuring tomcat and maven settings

  1. Add a manager role to tomcat. For this visit your installed tomcat folder>conf>tomcat-users.xml .Add the following code within <tomcat-users> tags; (make sure your tomcat directory has read/write permissions).

<role rolename="manager-gui"/> <role rolename="manager-script"/> <user username="admin" password="password" roles="manager-gui, manager-script"/>

  1. Adding tomcat as a server for maven. For this go to your installed maven directory>conf>setting.xml and add the following command within the <servers> tag.

    <server> <id>TomcatServer</id> <username>admin</username> <password>password</password> </server>

6. Configure Pom.xml

  • Add javax serverlet dependency

    • add following code within in <dependencies> tag. This is for java servlet compilation.

      • <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency>
    • add tomcat7 plugin and tomcat war generator plugins. Paste the following code within <build> <plugins> tags.

    • Tomcat7 plugin; here i set the path to root.( see the comment) <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <url>http://localhost:8080/manager/text</url> <server>TomcatServer</server> <path>/</path> </configuration> </plugin>

    • War generate plugin; <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>3.2.0</version> </plugin>

      • Maven compiling plugin; <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin>
  • You can find the Pom.xml file from POM XML FILE.

7. Run ,Deploy and enjoy :)

  • Run the command mvn install to install required dependencies and plugin.
  • Then run mvn tomcat7:deploy for deploy the java web app.
  • If any changes has done to the project you can redoply by running mvn tomcat7:redepoly command
  • To undeploy the app run the command mvn tomcat7:undeploy
References;
@namila007
Copy link
Author

Tomcat7 plugin: here i set the path to root.
*
<plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <url>http://localhost:8080/manager/text</url> <server>TomcatServer</server> <path>/</path> </configuration> </plugin>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment