使用maven-tomcat-plugin启动多个tomcat实例

时间:2013-01-09 18:39:38

标签: java maven tomcat maven-tomcat-plugin

我们需要使用相同的Web应用程序但不同的配置(通过spring +系统属性注入)启动3个tomcats,以便执行swing应用程序的某些集成测试。

我们有一个专门用于此测试的maven项目,它获取服务器端应用程序的WAR和swing应用程序的JAR,在3个不同的端口上启动3个tomcat实例,在这些tomcats中运行WAR并运行测试。

这是我们的maven配置:

<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>xxx</groupId>
    <artifactId>XXX-SwingTests</artifactId>
    <version>3.1.2-SNAPSHOT</version>
    <name>XXX Swing Tests</name>
    <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <source.encoding>UTF-8</source.encoding>
        <java.compiler.version>1.6</java.compiler.version>
        <XXX.version>${project.version}</XXX.version>
        <swing-test.include>**/ *SwingTest*</swing-test.include>
    </properties>

    <dependencies>
        <dependency>
            <groupId>xxx</groupId>
            <artifactId>XXX-Client-Base</artifactId>
            <version>${XXX.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>xxx</groupId>
            <artifactId>XXX-Client-Base</artifactId>
            <version>${XXX.version}</version>
            <type>test-jar</type>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>abbot</groupId>
            <artifactId>abbot</artifactId>
            <version>1.2.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <pluginManagement>
            <plugins>

                <plugin>
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat7-maven-plugin</artifactId>
                    <version>2.0</version>

                    <!-- External Libraries included in Tomcat -->
                    <dependencies>

                        <!-- Active MQ dependencies -->
                        <dependency>
                            <groupId>org.apache.activemq</groupId>
                            <artifactId>activemq-all</artifactId>
                            <version>5.6.0</version>
                        </dependency>
                        <dependency>
                            <groupId>commons-logging</groupId>
                            <artifactId>commons-logging</artifactId>
                            <version>1.1.1</version>
                        </dependency>
                        <dependency>
                            <groupId>commons-collections</groupId>
                            <artifactId>commons-collections</artifactId>
                            <version>3.2.1</version>
                        </dependency>
                        <dependency>
                            <groupId>log4j</groupId>
                            <artifactId>log4j</artifactId>
                            <version>1.2.16</version>
                        </dependency>
                        <dependency>
                            <groupId>org.springframework</groupId>
                            <artifactId>spring-context</artifactId>
                            <version>3.0.6.RELEASE</version>
                        </dependency>
                        <dependency>
                            <groupId>org.springframework</groupId>
                            <artifactId>spring-asm</artifactId>
                            <version>3.0.6.RELEASE</version>
                        </dependency>
                        <dependency>
                            <groupId>org.springframework</groupId>
                            <artifactId>spring-beans</artifactId>
                            <version>3.0.6.RELEASE</version>
                        </dependency>
                        <dependency>
                            <groupId>org.springframework</groupId>
                            <artifactId>spring-core</artifactId>
                            <version>3.0.6.RELEASE</version>
                        </dependency>
                        <dependency>
                            <groupId>org.springframework</groupId>
                            <artifactId>spring-expression</artifactId>
                            <version>3.0.6.RELEASE</version>
                        </dependency>
                        <dependency>
                            <groupId>org.apache.xbean</groupId>
                            <artifactId>xbean-spring</artifactId>
                            <version>3.9</version>
                        </dependency>
                    </dependencies>
                    <configuration>
                        <ignorePackaging>true</ignorePackaging>
                        <webapps>
                            <webapp>
                                <groupId>xxx</groupId>
                                <artifactId>XXX-Web</artifactId>
                                <version>${XXX.version}</version>
                                <type>war</type>
                                <asWebapp>true</asWebapp>
                                <contextPath>XXX</contextPath>
                            </webapp>
                        </webapps>
                        <systemProperties>
                            <!-- Allow Slashes and backslashes encoding -->
                            <org.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH>true</org.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH>
                            <org.apache.catalina.connector.CoyoteAdapter.ALLOW_BACKSLASH>true</org.apache.catalina.connector.CoyoteAdapter.ALLOW_BACKSLASH>
                        </systemProperties>
                    </configuration>

                </plugin>
            </plugins>
        </pluginManagement>

        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>*</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>false</filtering>
                <excludes>
                    <exclude>*</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>src/main/conf</directory>
                <filtering>true</filtering>
            </resource>
        </resources>

        <testResources>
            <testResource>
                <filtering>false</filtering>
                <directory>src/test/resources</directory>
                <includes>
                    <include>**/*</include>
                </includes>
                <excludes>
                    <exclude>**/*.xml</exclude>
                </excludes>
            </testResource>
            <testResource>
                <filtering>true</filtering>
                <directory>src/test/resources</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </testResource>
        </testResources>
    </build>

    <profiles>

        <!-- Profile that starts the tomcat servers -->
        <profile>
            <id>start-servers</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <build>
                <plugins>
                    <!-- Run CL server -->
                    <plugin>
                        <groupId>org.apache.tomcat.maven</groupId>
                        <artifactId>tomcat7-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>tomcat-run-CL</id>
                                <goals>
                                    <goal>run-war-only</goal>
                                </goals>
                                <phase>pre-integration-test</phase>
                                <configuration>
                                    <port>8180</port>
                                    <ajpPort>8109</ajpPort>
                                    <fork>true</fork>
                                    <systemProperties>
                                        <config_XXX_application>${project.build.directory}/test-classes/central/application.properties</config_XXX_application>
                                        <config_XXX_cron_application>${project.build.directory}/test-classes/central/cron.properties</config_XXX_cron_application>
                                        <config_XXX_external>${project.build.directory}/test-classes/central/external-providers.properties</config_XXX_external>
                                    </systemProperties>
                                    <contextFile>${project.build.directory}/classes/CL/context.xml</contextFile>
                                    <configurationDir>${project.build.directory}/tomcat-cl</configurationDir>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>

                    <!-- Run LL server -->
                    <plugin>
                        <groupId>org.apache.tomcat.maven</groupId>
                        <artifactId>tomcat7-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>tomcat-run-LL</id>
                                <goals>
                                    <goal>run-war-only</goal>
                                </goals>
                                <phase>pre-integration-test</phase>
                                <configuration>
                                    <port>8280</port>
                                    <ajpPort>8209</ajpPort>
                                    <fork>true</fork>
                                    <systemProperties>
                                        <config_XXX_application>${project.build.directory}/test-classes/local/application.properties</config_XXX_application>
                                        <config_XXX_cron_application>${project.build.directory}/test-classes/local/cron.properties</config_XXX_cron_application>
                                        <config_XXX_external>${project.build.directory}/test-classes/local/external-providers.properties</config_XXX_external>
                                    </systemProperties>
                                    <contextFile>${project.build.directory}/classes/LL/context.xml</contextFile>
                                    <configurationDir>${project.build.directory}/tomcat-ll</configurationDir>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>

                    <!-- Run OL server -->
                    <plugin>
                        <groupId>org.apache.tomcat.maven</groupId>
                        <artifactId>tomcat7-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>tomcat-run-OL</id>
                                <goals>
                                    <goal>run-war-only</goal>
                                </goals>
                                <phase>pre-integration-test</phase>
                                <configuration>
                                    <port>8380</port>
                                    <ajpPort>8309</ajpPort>
                                    <fork>true</fork>
                                    <systemProperties>
                                        <config_XXX_application>${project.build.directory}/test-classes/operational/application.properties</config_XXX_application>
                                        <config_XXX_cron_application>${project.build.directory}/test-classes/operational/cron.properties</config_XXX_cron_application>
                                        <config_XXX_external>${project.build.directory}/test-classes/operational/external-providers.properties</config_XXX_external>
                                    </systemProperties>
                                    <contextFile>${project.build.directory}/classes/OL/context.xml</contextFile>
                                    <configurationDir>${project.build.directory}/tomcat-ol</configurationDir>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>

                    <!-- Stop all servers -->
                    <plugin>
                        <groupId>org.apache.tomcat.maven</groupId>
                        <artifactId>tomcat7-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>tomcat-shutdown</id>
                                <goals>
                                    <goal>shutdown</goal>
                                </goals>
                                <phase>post-integration-test</phase>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>

        <!-- Profile that runs the swing tests in the integration-test phase -->
        <profile>
            <id>swing-tests</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <build>
                <plugins>
                    <!-- Execute swing tests in the 'integration-test' phase -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-failsafe-plugin</artifactId>
                        <version>2.13</version>
                        <executions>
                            <execution>
                                <id>integration-test</id>
                                <phase>integration-test</phase>
                                <goals>
                                    <goal>integration-test</goal>
                                    <goal>verify</goal>
                                </goals>
                                <configuration>
                                    <argLine>-Xms512m -Xmx1024m -XX:MaxPermSize=512m</argLine>
                                    <includes>
                                        <include>${swing-test.include}</include>
                                    </includes>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

</project>

即使日志没有显示任何特定内容,只有目标服务器启动的测试首先成功,其他测试失败,我无法看到原因(我怀疑弹簧上下文未正确启动,但我不能确定)。 如果我注释掉第一个插件执行,那么所有针对此服务器的测试现在都会失败(显然),但是目前列表中下一个测试的测试会通过。

您是否知道此配置有什么问题? 实现我们在这里尝试的正确方法是什么?

编辑: 实际上,日志中存在错误,但我不确定如何理解:

[INFO] [tomcat7:run-war-only {execution: tomcat-run-CL}]
[INFO] Running war on http://localhost:8180/xxx-SwingTests
[INFO] Creating Tomcat server configuration at C:\projects\xxx\workspace\xxx-SwingTests\target\tomcat-cl
[INFO] setting SystemProperties:
[INFO]  config_xxx_application=C:\projects\xxx\workspace\xxx-SwingTests\target/test-classes/central/application.properties
[INFO]  config_xxx_cron_application=C:\projects\xxx\workspace\xxx-SwingTests\target/test-classes/central/cron.properties
[INFO]  config_xxx_external=C:\projects\xxx\workspace\xxx-SwingTests\target/test-classes/central/external-providers.properties
[INFO]  org.apache.catalina.connector.CoyoteAdapter.ALLOW_BACKSLASH=true
[INFO]  org.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH=true
[INFO] create webapp with contextPath: /xxx-SwingTests
[INFO] Deploying dependency wars
[INFO] Deploy warfile: C:\Users\Administrator\.m2\repository\xxx\xxx-Web\3.1.2-SNAPSHOT\xxx-Web-3.1.2-SNAPSHOT.war to contextPath: /xxx
Jan 9, 2013 7:44:27 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8180"]
Jan 9, 2013 7:44:27 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8109"]
Jan 9, 2013 7:44:27 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Tomcat
Jan 9, 2013 7:44:27 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.30
Jan 9, 2013 7:44:27 PM org.apache.catalina.core.StandardContext resourcesStart
SEVERE: Error starting static Resources
java.lang.IllegalArgumentException: Document base C:\projects\xxx\workspace\xxx-SwingTests\target\xxx-SwingTests-3.1.2-SNAPSHOT does not exist or is not a readable directory
        at org.apache.naming.resources.FileDirContext.setDocBase(FileDirContext.java:140)
        at org.apache.catalina.core.StandardContext.resourcesStart(StandardContext.java:4906)
        at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5086)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
        at java.util.concurrent.FutureTask.run(FutureTask.java:138)
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
        at java.lang.Thread.run(Thread.java:662)
Jan 9, 2013 7:44:27 PM org.apache.catalina.core.StandardContext startInternal
SEVERE: Error in resourceStart()
Jan 9, 2013 7:44:27 PM org.apache.catalina.core.StandardContext startInternal
SEVERE: Error getConfigured
Jan 9, 2013 7:44:27 PM org.apache.catalina.core.StandardContext startInternal
SEVERE: Context [/xxx-SwingTests] startup failed due to previous errors
Jan 9, 2013 7:44:27 PM org.apache.catalina.deploy.NamingResources cleanUp
WARNING: Failed to retrieve JNDI naming context for container [StandardEngine[Tomcat].StandardHost[localhost].StandardContext[/xxx-SwingTests]] so no cleanup was performed for that container
javax.naming.NameNotFoundException: Name [comp/env] is not bound in this Context. Unable to find [comp].
        at org.apache.naming.NamingContext.lookup(NamingContext.java:820)
        at org.apache.naming.NamingContext.lookup(NamingContext.java:168)
        at org.apache.catalina.deploy.NamingResources.cleanUp(NamingResources.java:988)
        at org.apache.catalina.deploy.NamingResources.stopInternal(NamingResources.java:970)
        at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232)
        at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5495)
        at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:160)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
        at java.util.concurrent.FutureTask.run(FutureTask.java:138)
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
        at java.lang.Thread.run(Thread.java:662)

2 个答案:

答案 0 :(得分:1)

问题是在同一个类加载器中开始两次jndi上下文,这对tomcat来说是不可能的。

答案 1 :(得分:0)

如何使用Jetty + Tomcat?

您无法使用Tomcat为插件提供尽可能多的实例。但是,您可以使用maven-jetty-plugin同时启动至少2个应用程序。