运行mvn install与运行tomcat插件并行运行

时间:2013-11-10 06:01:07

标签: java maven configuration embedded-tomcat-7

在项目中有单元测试和前端html测试需要运行webserver。 为了使mvn install顺利通过,我需要这两种测试都顺利通过。 我使用嵌入式tomcat服务器,它是通过tomcat插件为maven启动的:

mvn tomcat7:run

所以,我启动tomcat(我的前端html测试需要它), 然后尝试在命令行中启动:mvn install,但得到以下错误:

D:\PROJECTS\SpringMvcExample>mvn install
ERROR: transport error 202: bind failed: Address already in use
ERROR: JDWP Transport dt_socket failed to initialize, TRANSPORT_INIT(510)
JDWP exit error AGENT_ERROR_TRANSPORT_INIT(197): No transports initialized [../../../src/share/back/debugInit.c:741]
FATAL ERROR in native method: JDWP No transports initialized, jvmtiError=AGENT_ERROR_TRANSPORT_INIT(197)

同时,当tomcat启动时,我可以通过Intellij Idea插件为maven运行mvn install,它可以正常工作而没有任何错误。

我的问题如何配置才能在命令行中启动maven?

这是maven多模块项目,这里是web模块的pom.xml:

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 

http://maven.apache.org/xsd/maven-4.0.0.xsd"
         xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>com.savdev</groupId>
    <artifactId>SpringMvcExample</artifactId>
    <version>1.0-SNAPSHOT</version>
</parent>
<artifactId>web</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>SpringMvcExample Webapp</name>

<properties>
    <spring.version>3.2.4.RELEASE</spring.version>
    <selenium.version>2.37.1</selenium.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
    <!--modules dependency start-->
    <dependency>
        <groupId>com.savdev</groupId>
        <artifactId>model</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>com.savdev</groupId>
        <artifactId>service</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <!--modules dependency end-->

    <!--web dependency start-->
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>7.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>3.2.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>3.2.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
        <scope>runtime</scope>
    </dependency>
    <!--web dependency end-->

    <!--tests dependency-->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${spring.version}</version>
        <scope>test</scope>
    </dependency>
    <!--<dependency>-->
        <!--<groupId>org.mockito</groupId>-->
        <!--<artifactId>mockito-all</artifactId>-->
        <!--<version>1.9.5</version>-->
        <!--<scope>test</scope>-->
    <!--</dependency>-->
    <dependency>
        <groupId>net.sourceforge.jwebunit</groupId>
        <artifactId>jwebunit-core</artifactId>
        <version>3.1</version>
        <scope>test</scope>
    </dependency>
    <!--<dependency>-->
        <!--<groupId>net.sourceforge.jwebunit</groupId>-->
        <!--<artifactId>jwebunit-htmlunit-plugin</artifactId>-->
        <!--<version>3.1</version>-->
    <!--</dependency>-->
    <!--<dependency>-->
        <!--<groupId>net.sourceforge.htmlunit</groupId>-->
        <!--<artifactId>htmlunit</artifactId>-->
        <!--<version>2.13</version>-->
    <!--</dependency>-->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>${selenium.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-firefox-driver</artifactId>
        <version>${selenium.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-chrome-driver</artifactId>
        <version>${selenium.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-htmlunit-driver</artifactId>
        <version>${selenium.version}</version>
        <scope>test</scope>
    </dependency>
</dependencies>
<build>
    <finalName>SpringMvcExample</finalName>
    <!--to run into debug mode set:-->
    <!--export MAVEN_OPTS=-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n-->
    <plugins>
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.1</version>
            <configuration>
                <path>/SpringMvcExample</path>
                <url>http://localhost:8080/manager/text</url>
                <server>tomcat7</server>
            </configuration>
        </plugin>
    </plugins>
</build>

1 个答案:

答案 0 :(得分:0)

尝试将tomcat run / shutdown绑定到合适的<execution>阶段:

<phase>pre-integration-test</phase>
<goals>
  <goal>run</goal>
</goals>

<phase>post-integration-test</phase>
<goals>
  <goal>shutdown</goal>
</goals>

如此配置,你不应该手动启动tomcat (即mvn tomcat7:run)。它应该在测试之前自动运行并在它们之后关闭。