Maven – build package for different environments

Build package for different environments using Maven

To make life easier for programmers who need to test and deploy application to a remote environment using different properties, it is possible to properly configure the pom file to fit with own necessity with just one command.

Suppose your are dealing with a web application that is tested on a local Tomcat;
the application itself read from a properties file mapped for the proper environment, one for local testing and one for distribution environment; so your need could be:

  • Build the webapp locally and distribuite to the local tomcat
  • Build the webapp and deploy it together with the properly properties file

Use the profile directive on the pom.xml to specify a test or deploy action in the following way:

Notice the following points of interest:

  • The tomcat7-maven-plugin can automatically deploy a war inside tomcat deploy folder by just giving the address in which tomcat is listening
  • The maven-antrun-plugin can execute some ant based tasks; In the case above we first delete the previous file and then copy the file (included in the source folder) to the output build directory, renaming it to

Also notice that, mvn clean install run the test profile by default, because of the activeByDefault property set to true.

Therefore, to run the deploy profile just type the following command:




Windows batch example: building an app and deploy to a remote server

Windows Batch Example

This example will show how to build an application with maven and send the built package to a remote server.

Start installing putty to a whatever directory and set it on the system path under environment variables.

Then place some variables that can be changed in a second moment whenever desired.

The variables are so explained:

workspace: refer to the application source folder,
user_home: is a remote user’s temp folder in which the built package temporarily store.
user: remote user’s account
password: remote password account
mavendir: maven installation folder

Now the first step is to start maven install process.

This command will start maven clean install to another windows (then another process) with wait option in order to wait the execution end of the application (maven install). The ^&^& exit grant the exit from the console in order to automatically continue the statements from the caller process.

will copy through pscp protocol the copy of app.war from local machine to remote server.

Now it’s time to bring the .war to remote server deploy directory, so probably root’s permissions are required.

plink is the command line interface of putty, so using ssh we’ll copy the app.war into deploy directory.

-t option enable pty allocation, so sudo password won’t be asked

-S read the password from the standard input (in this case echo -e %password%

At the same way, using ssh protocol with plink will be possible to call commands to restart the remote server