This is about how a simple maven java web app deploy to tomcat server
Install the tomcat server (tomcat8 or up) Download tomcat.
Get the latest binary.zip
file and extract in your desired path.
Download maven
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.
-
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
- run following command with suitable names for DgroupId and DartifactId
-
Add java files and web files
- 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"/>
-
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>
-
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>
- Maven compiling plugin;
-
-
You can find the
Pom.xml
file from POM XML FILE.
- 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
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>