Monday 4 May 2015

Deploy WAR on tomcat from maven command

Many times while doing testing of web project (.war) during development phase we have to deploy project on tomcat server frequently. So every times we have copy war file then paste it to {TOMCAT_HOME}\webapp directory. Some times need to restart tomcat as well.

My this post focuses on reducing this time by automating deployment from maven command.  So we can deploy, un-deploy, update war file and also do start, stop tomcat server from Maven command.

For this automation we have make changes in following files.

    1.{TOMCAT_HOME}/conf/tomcat-users.xml
    2.{MAVEN_HOME}/conf/settings.xml
    3.{PROJECT_HOME}/pom.xml
    4.Use this configured plugin to deploy, undeploy etc.
 
 let's start making above changes one by one. This post uses tomcat6 however if you are using different tomcat version, you can achieve same by using tomcat version specific plugin in step. 3.

1. Update {TOMCAT_HOME}/conf/tomcat-users.xml

   Add 'manager-script' role and its username/password.  Properties highlighted in yellow are only relevant properties with this tutorial.
<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
  <role rolename="tomcat"/>
  <role rolename="manager-gui"/>
  <!-- 'manager-script' role will be used for deployment from Maven -->
  <role rolename="manager-script"/>
  <user username="admin" password="tomcat" roles="tomcat,manager-gui,manager-script"/>
</tomcat-users>

2. Update {MAVEN_HOME}/conf/settings.xml

   Update you maven settings.xml file with below snippet. Keep rest of the part of settings.xml as it is.
<?xml version="1.0" encoding="UTF-8"?>
<settings>
  ...
  <servers>
   	<server>
		<id>TomcatServer</id>
		<!-- Username/password below should match with one mapped with 'manager-script' role in tomcat_home/conf/tomcat-users.xml file-->
		<username>admin</username>
		<password>tomcat</password>
	</server>
  </servers>
  ...
</settings>

Username/Password should match with username/password configured with role 'manager-script' in step 1.

3. Update {PROJECT_HOME}/pom.xml

   Update the following part in your pom.xml. Value of <server> should match with value of     <id>TomcatServer</id> in {MAVEN_HOME}/conf/settings.xml file
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	...
	<build>
		<finalName>ConcatService</finalName>
		<plugins>
			...			
			<plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat6-maven-plugin</artifactId> 
                                <!-- For tomcat7 use -->
                                <!-- <artifactId>tomcat7-maven-plugin</artifactId> -->  
                                <version>2.2</version>
				<configuration>
					<url>http://localhost:8080/manager</url>
					<server>TomcatServer</server>
					<path>/ConcatService</path>
				</configuration>
			</plugin>
			...
		</plugins>
	</build>
</project>


4. Useful tomcat maven plugin goals.
mvn tomcat6:redeploy
mvn tomcat6:undeploy
mvn tomcat6:start
mvn tomcat6:stop 

Some Useful links.
Error Resolutions : 
If you got following error while executing any of the tomcat goals. (Which i had got.)


[INFO] [war:war {execution: default-war}]
[INFO] Packaging webapp
[INFO] ------------------------------------------------------------------------
[ERROR] FATAL ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Cannot construct org.apache.maven.plugin.war.util.WebappStructure as it does not have a no-args constructor
---- Debugging information ----
message             : Cannot construct org.apache.maven.plugin.war.util.WebappStructure as it does not have a no-args constructor
cause-exception     : com.thoughtworks.xstream.converters.reflection.ObjectAccessException
cause-message       : Cannot construct org.apache.maven.plugin.war.util.WebappStructure as it does not have a no-args constructor
class               : org.apache.maven.plugin.war.util.WebappStructure
required-type       : org.apache.maven.plugin.war.util.WebappStructure
path                : /webapp-structure
line number         : 1
-------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Trace
com.thoughtworks.xstream.converters.ConversionException: Cannot construct org.apache.maven.plugin.war.util.WebappStructure as it does not have a no-args constructor
---- Debugging information ----
message             : Cannot construct org.apache.maven.plugin.war.util.WebappStructure as it does not have a no-args constructor
cause-exception     : com.thoughtworks.xstream.converters.reflection.ObjectAccessException
cause-message       : Cannot construct org.apache.maven.plugin.war.util.WebappStructure as it does not have a no-args constructor
class               : org.apache.maven.plugin.war.util.WebappStructure
required-type       : org.apache.maven.plugin.war.util.WebappStructure
path                : /webapp-structure
line number         : 1
-------------------------------
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:63)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:45)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:46)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:117)
    at com.thoughtworks.xstream.core.ReferenceByXPathMarshallingStrategy.unmarshal(ReferenceByXPathMarshallingStrategy.java:29)
    at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:846)
    at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:833)
    at com.thoughtworks.xstream.XStream.fromXML(XStream.java:781)
    at org.apache.maven.plugin.war.util.WebappStructureSerializer.fromXml(WebappStructureSerializer.java:73)
    at org.apache.maven.plugin.war.AbstractWarMojo.buildWebapp(AbstractWarMojo.java:404)
    at org.apache.maven.plugin.war.AbstractWarMojo.buildExplodedWebapp(AbstractWarMojo.java:375)
    at org.apache.maven.plugin.war.WarMojo.performPackaging(WarMojo.java:181)
    at org.apache.maven.plugin.war.WarMojo.execute(WarMojo.java:143)
    at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:490)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:694)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalWithLifecycle(DefaultLifecycleExecutor.java:556)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:535)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:387)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:348)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:180)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:328)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:138)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:362)
    at org.apache.maven.cli.compat.CompatibleMain.main(CompatibleMain.java:60)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
    at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
    at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)
    at org.codehaus.classworlds.Launcher.main(Launcher.java:375)
Caused by: com.thoughtworks.xstream.converters.reflection.ObjectAccessException: Cannot construct org.apache.maven.plugin.war.util.WebappStructure as it does not have a no-args constructor
    at com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider.newInstance(PureJavaReflectionProvider.java:59)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.instantiateNewInstance(AbstractReflectionConverter.java:257)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:124)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:56)
    ... 31 more

Solutions for above error: Just remove old target directory ({PROJECT_HOME}\target) or run $mvn clean command and then execute tomcat goal again. If still appears try to use updated version of 'tomcat6-maven-plugin'.


No comments:

Post a Comment