Using maven-deploy-plugin

And another very interesting topic is the automatic deploy of the assembled package. Let's say we built our own library using Maven. How do we automatically push it to a local, corporate, or central Maven repository?

Maven has a special maven-deploy-plugin plugin for this . Example:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-deploy-plugin</artifactId>
    	<version>2.5</version>
    	<configuration>
          <file>${project.build.directory}\${project.artifactId}-src.zip</file>
          <url>${project.distributionManagement.repository.url}</url>
          <repositoryId>${project.distributionManagement.repository.id}</repositoryId>
          <groupId>${project.groupId}</groupId>
          <artifactId>${project.artifactId}</artifactId>
          <version>${project.version}</version>
      	  <packaging>zip</packaging>
          <pomFile>pom.xml</pomFile>
    	</configuration>
  	</plugin>

With these settings, you can push the built library to the Maven repository as a valid package. We will not analyze this process in detail, but briefly consider what is happening here:

The file tag specifies a file that will be pushed to the Maven repository as a new library.

The url tag is the path to the Maven repository (local/corporate/…).

The repositoryId tag specifies the identifier of the repository to which the deployment will be made.

The groupId , artifactId , version tags define the standard package identification in the Maven repository. It is by these three parameters that a library can be uniquely identified.

The packaging tag is used to ensure that the result is sent as a single zip file. If you do not specify it, then there will be one jar file, even if you had several jar files.

The pomFile tag is optional and allows you to send another pom.xml to the repository that does not contain hidden or overhead data.

Deploying a web application to Tomcat using Maven

The most popular web server for Java web applications is Apache Tomcat . And of course, with the help of Maven, you can deploy war files directly to a local or even remote Tomcat server.

We will learn how to install and configure Tomcat sometime later, but now we will only touch on the topic of automatic deployment of our web application.

Step one. We need to give Maven access to the Tomcat server. To do this, open the conf/tomcat-users.xml file in the directory where Apache Tomcat is unpacked and add the manager-gui and manager-script roles :

<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
  <role rolename="manager-gui"/>
  <role rolename="manager-script"/>
  <user username="admin" password="admin" roles="manager-gui,manager-script" />
</tomcat-users>

Step two. Allow Maven access to Tomcat. To do this, open the $MAVEN_HOME/conf/settings.xml file and add the server:

<?xml version="1.0" encoding="UTF-8"?>
<settings ...>
  <servers>
	<server>
  	<id>TomcatServer</id>
  	<username>admin</username>
  	<password>admin</password>
	</server>
  </servers>
</settings>

Step three. We add a special plugin for automated deployment of our application to Apache Tomcat. The plugin is called tomcat7-maven-plugin . By the way, it was created not by the Maven developers, but by the Tomcat developers, as you can guess from the name.

	<build>
    	<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>TomcatServer</server>
                	<path>/simpleProject</path>
            	</configuration>
        	</plugin>
    	</plugins>
	</build>

In the configuration section, specify:

  • url is the address where Tomcat is running and the path to manager/text
  • server - server id from the settings.xml file
  • path - the address where the deployed application will be available

Deployment management commands:

mvn tomcat7:deploy Deploy the application to Tomcat
mvn tomcat7:undeploy Remove application from Tomcat
mvn tomcat7:redeploy Re-deploy the application

Deploy with Cargo Plugin

Another useful and versatile plugin for deploying web applications is the Cargo Plugin . He knows how to work with different types of web servers. Here is how to deploy with it in Apache Tomcat:

<build>
    <plugins>
    	<plugin>
            <groupId>org.codehaus.cargo</groupId>
            <artifactId>cargo-maven2-plugin</artifactId>
        	<version>1.9.10</version>
        	<configuration>
            	<container>
                	<containerId>tomcat8x</containerId>
                    <type>installed</type>
                	<home>Insert absolute path to tomcat 7 installation</home>
            	</container>
            	<configuration>
                    <type>existing</type>
                    <home>Insert absolute path to tomcat 7 installation</home>
            	</configuration>
        	</configuration>
   	    </plugin>
    </plugins>
</build>

To install the web application on your local Tomcat, you just need to run the commands:

mvn install
mvn cargo:deploy

If we want to deploy to a remote web server, then we will have to set up access rights to this server. To do this, you just need to register them in pom.xml :

<configuration>
	<container>
        <containerId>tomcat8x</containerId>
    	<type>remote</type>
	</container>
	<configuration>
    	<type>runtime</type>
    	<properties>
            <cargo.remote.username>admin</cargo.remote.username>
            <cargo.remote.password>admin</cargo.remote.password>
        	<cargo.tomcat.manager.url>http://localhost:8080/manager/text</cargo.tomcat.manager.url>
    	</properties>
	</configuration>
</configuration>

Deploy with IntelliJ IDEA

Intellij IDEA does all the work itself, all you need is an installed Tomcat .

Step one. Create a local Tomcat configuration:

Step two. Then select the local Tomcat:

Step three. Configuring Tomcat:

Step four. Add the path to the Tomcat folder.

Step five. We add our project as an artifact to Tomcat.

To do this, go to the Deployment tab and click the + button on the right .

That's all.

By the way, if you need to deploy to a remote server, just select Remote Tomcat in the second step.