If you want to start with a Java EE web application, you can simply use the maven-archetype-webapp
archetype to generate the Maven project skeleton, which is shown as follows:
$ mvn archetype:generate -B -DgroupId=com.packt.samples -DartifactId=my-webapp -Dpackage=com.packt.samples.webapp -Dversion=1.0.0 -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp -DarchetypeVersion=1.0
The preceding command will produce the following directory structure. One issue here is that it does not have the java
directory just after src/main
. If you want to add any Java code, you need to make sure that you first create an src/main/java
directory and create your Java package under it. Otherwise, with the default configuration settings, Maven won't pick your classes for compilation. By default, Maven looks for the source code inside src/main/java
:
my-webapp |-pom.xml |-src/main/webapp |-index.jsp |-WEB-INF/web.xml |- src/main/resources
The maven-archetype-webapp
archetype is not the only archetype to generate a Java EE project using the archetype
plugin. Codehaus, a collaborative environment to build open source projects, also provides a few archetypes to generate web applications. The following example uses the webapp-javaee6
archetype from Codehaus:
$ mvn archetype:generate -B -DgroupId=com.packt.samples -DartifactId=my-webapp -Dpackage=com.packt.samples.webapp -Dversion=1.0.0 -DarchetypeGroupId=org.codehaus.mojo.archetypes -DarchetypeArtifactId=webapp-javaee6 -DarchetypeVersion=1.3
The preceding command will produce the following directory structure. This overcomes one of the issues in the maven-archetype-webapp
archetype and creates the src/main/java
and src/test/java
directories. The only issue here is that it does not create the src/main/webapp/WEB-INF
directory and you need to create it manually:
my-webapp |-pom.xml |-src/main/webapp/index.jsp |-src/main/java/com/packt/samples/webapp/ |-src/test/java/com/packt/samples/webapp/
Now we have created a template web application either using the maven-archetype-webapp
or webapp-javaee6
archetype. Let's see how to deploy this web application into a remote Apache Tomcat application server from Maven itself. Most developers would prefer doing this rather over manual copying. To deploy the web application, perform the following steps:
This assumes you have already installed Apache Tomcat in your environment. If not, you can download Tomcat 7.x distribution from http://tomcat.apache.org/download-70.cgi and set it up.
TOMCAT_HOME/conf/tomcat-users.xml
file under the tomcat-users root
element. This will create a user with the name admin
and the password password
, having the manager-gui
and manager-script
roles.<role rolename="manager-gui"/> <role rolename="manager-script"/> <user username="admin" password="password" roles="manager-gui,manager-script"/>
USER_HOME/.m2/settings.xml
under the servers
element, as follows:<server> <id>apache-tomcat</id> <username>admin</username> <password>password</password> </server>
root
directory of the template web application we generated before (my-webapp
) and then add tomcat7-maven-plugin
to it. The complete pom.xml
file should look like this:<project >
<modelVersion>4.0.0</modelVersion>
<groupId>com.packt.samples</groupId>
<artifactId>my-webapp</artifactId>
<packaging>war</packaging>
<version>1.0.0</version>
<name>my-webapp Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>my-webapp</finalName>
<plugins>
<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>apache-tomcat</server>
<path>/my-webapp</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
http://localhost:8080/my-webapp/
:$ mvn clean install tomcat7:deploy
$ mvn clean install tomcat7:redeploy
$ mvn clean install tomcat7:undeploy
3.143.17.27