Monday, July 26, 2010

Deploying from Maven

Here is a tip that saves a lot of time when developing web applications in Java. I find myself teaching this again and again every so often and so I decided to make it a blog port.

To build and deploy from maven I use the below maven snippet:

...
<properties>
<tomcat.deploy.dir>${env.MVN_TOMCAT_HOME_DEPLOY}</tomcat.deploy.dir>
</properties>
...
<profiles>
<profile>
<id>dev-tomcat-deploy-webapp</id>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>e1</id>
<phase>process-resources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo
message="Copying from ${basedir}/src/main/webapp to ${tomcat.deploy.dir}/${pom.build.finalName}" />
<copy todir="${tomcat.deploy.dir}/${pom.build.finalName}">
<fileset includes="**/*.*" dir="${basedir}/src/main/webapp" />
</copy>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>dev-tomcat-deploy-war</id>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>e1</id>
<phase>install</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>Deploying WAR locally</echo>
<copy todir="${tomcat.deploy.dir}" file="target/${pom.build.finalName}.war" />
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>


I have not tested this in Windows but probably with the help of cygwin or any other other *nix for windows ports you might be able to use it.

First be sure you have the target deployment directory configured as an environment variable.

#vi ~/.profile
export MVN_TOMCAT_HOME_DEPLOY=/Users/nestor/apache-tomcat-6.0.28/webapps


You can use the below command to build and deploy in tomcat from one command:

mvn clean install -Pdev-tomcat-deploy-war antrun:run


You can use the below command just to deploy the webapp content of your web application. This means you just get changes in creatives or JSP files for example. Of course this accelerates your development (No new rebuild, no new redeployment, no session lost) BTW only new files will be copied.

mvn process-resources -Pdev-tomcat-deploy-webapp antrun:run

No comments:

Followers