Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven Deploy To Multiple Tomcat Servers

Tags:

What is the most minimal example of deploying a war to multiple tomcat servers using maven that can be written?

I've tried the following URLs and asked the mailing list, but not coming up with anything that was short and would simply work.

  • http://www.nabble.com/Deploying-to-Multiple-Servers-at-Once-td21592419.html

The example should have the servers defined in the example somewhere (with sample usernames/passwords)

like image 445
cgp Avatar asked Apr 08 '09 23:04

cgp


2 Answers

The idea of Markus Lux can be also applied with a Maven2 solution, with the profiles management:

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.cargo</groupId>
            <artifactId>cargo-maven2-plugin</artifactId>
        </plugin>
    </plugins>
    ...
</build>
<profiles>
    <profile>
        <id>env-foo1</id>
        <!-- Activated when -Denv=foo1 is given as parameter. -->
        <activation>
            <property>
                <name>env</name>
                <value>foo1</value>
            </property>
        </activation>
        <properties>
            <deploy.env>xxx</deploy.env>
            <tomcat.manager>http://foo1/manager</tomcat.manager>
            <tomcat.manager.username>foo</tomcat.manager.username>
            <tomcat.manager.password>bar</tomcat.manager.password>
        </properties>
    </profile> 
    <profile>
        <id>env-foo2</id>
        <!-- Activated when -Denv=foo2 is given as parameter. -->
        <activation>
            <property>
                <name>env</name>
                <value>foo2</value>
            </property>
        </activation>
        <properties>
            <deploy.env>dev</deploy.env>
            <tomcat.manager>http://foo2/manager</tomcat.manager>
            <tomcat.manager.username>foo</tomcat.manager.username>
            <tomcat.manager.password>bar</tomcat.manager.password>
        </properties>
    </profile>
    ... 
</profiles>    

Then, you will just need to run X times the mvn command, with the adequate parameter (-Denv=foo1, -Denv=foo2,...)


In addition to that, you can enhance this solution by using the Matrix feature of the Hudson Continuous Integration server. I gave a short explanation about this feature here.

Basically, you just define a "normal" Maven2 job in Hudson, and with the Matrix feature, you can ask Hudson to run this job several times, one per environment. In others words, you create your Hudson job, and then you define the "environment axis" with all possible value for the env parameter:

  • foo1
  • foo2
  • foo3
  • ...

Hudson will then build the application with the mvn command and with the parameter -Denv=foo1.Once this build is finished, it will build the same application but with the parameter -Denv=foo2, and so on...

This way, Hudson will deploy your application in every environments...

I hope my solution will help you to reach your goals...

like image 81
Romain Linsolas Avatar answered Sep 21 '22 10:09

Romain Linsolas


With regards to using multiple profiles, the lifecycle seemed to duplicate certain steps - e.g. the number of tests doubled when using profiles activated by variables. We found that using the catalina-ant library was much more effective ;) and its more "minimal". Use "executions" element to attach the "run" goal to a lifecycle phase to streamline it, or run after package: mvn package antrun:run

You could get a bit more fancy with the ant-contrib library, and create a for loop with a list of servers, but here's a static configuration for 2 hard coded server urls.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.6</version>
    <configuration>
        <target>
            <taskdef name="deploy" classname="org.apache.catalina.ant.DeployTask"/>
            <deploy url="http://tc-app-01:8080/manager" username="manager" password="pass"
                    path="/app-path" war="file:${project.build.directory}/${project.build.finalName}.${project.packaging}" update="true"/>

            <deploy url="http://tc-app-02:8080/manager" username="manager" password="pass"
                    path="/app-path" war="file:${project.build.directory}/${project.build.finalName}.${project.packaging}" update="true"/>
        </target>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>tomcat</groupId>
            <artifactId>catalina-ant</artifactId>
            <version>6.0.29</version>
        </dependency>
    </dependencies>
</plugin>

The specific version of catalina-ant used above was manually deployed to our distributed maven repository, it can be found in the lib directory of tomcat distribution.

like image 41
Brett Cave Avatar answered Sep 19 '22 10:09

Brett Cave