Spring MVC Blueprints
上QQ阅读APP看书,第一时间看更新

Project deployment

Apache Tomcat 7 will be used as the application server for this chapter. There are two popular Maven plugins that can be used to deploy applications to Tomcat: the Apache Maven plugin and the Mojo Maven plugin. By default, this book uses the Mojo Maven plugin.

To deploy a Maven project to Tomcat using the Mojo Maven plugin, the following configuration must be followed:

  1. Locate the user/conf/tomcat-users.xml file on the Tomcat server and add the desired user with administrator rights. Information like role, username and password of the added user must be added inside the <tomcat-user> tag. A sample configuration is shown in the following code snippet:
          <role rolename="manager"/> 
          <role rolename="admin"/> 
          <user username="admin" password="admin" 
          roles="admin,manager "/> 
    
    • Now save the file.
  2. Locate the file ~/.m2/settings.xml in Maven. If the file is non-existent, download Maven from its site and copy the settings.xml to ~/.m2. Open the file and locate the <servers> tag. Insert the following tag inside the <servers> tag.
            <server> 
              <id>TomcatServer</id> 
              <username>admin</username> 
              <password>admin</password> 
            </server> 
    
    • The <id> is any desired name for the Tomcat server that will be later called by the pom.xml plugin for Tomcat 7. The <username> and <password> are the server credentials manually added in /conf/tomcat-users.xml above. Save the file.
  3. Open pom.xml and add the following Maven plugin for Tomcat 7 server.
            <plugin> 
              <groupId>org.codehaus.mojo</groupId> 
              <artifactId>tomcat-maven-plugin</artifactId> 
              <version>1.1</version> 
              <configuration> 
                <url>http://localhost:8080/manager/text</url> 
                <server>TomcatServer</server> 
                <path>/ch01</path> 
              </configuration> 
            </plugin> 
    

The <configuration> settings contain information of where to deploy the project (<url>), what context root to use (<path>/), and the server name (<server>) used in the settings.xml file.

To deploy using the Apache Tomcat7 Maven plugin, the <configuration> tag only needs the <username> and <password> tags of the administration console, including the <url> and the <path>.

Maven deployment process

All projects are deployed in the Tomcat 7 server using the Maven plugin installed in STS. Follow the steps below to properly deploy the Spring MVC projects.

  1. Be sure to have a correctly configured Maven project with the appropriate POM configuration shown in the preceding figure.
  2. Right-click on the project and locate Run As in the menu options. Then, locate Maven build... in the sub-menu option, to configure the Maven goals for the first time. Maven goal is a task or command that is executed to build and manage Maven projects.
  3. After this, a Maven configuration panel will pop up to write the needed goals for deployment, and to launch the deployment process. If the Mojo Maven plugin is used, write on the Goals textbox the following: clean install tomcat:deploy. If the Apache Tomcat7 Maven plugin is used, the goals must be: clean install tomcat7:deploy. Aside from these goals, there are some that can be essential for management like remove, update and re-deploy.
  4. Click the Run button to launch the deployment. All the Maven execution logs will be shown on the console. The outcome of the deployment will be either a BUILD SUCCESS or a BUILD FAILURE.