0

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

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启动时,我可以mvn install通过Intellij Idea插件为maven运行,它工作正常,没有任何错误。

我的问题是如何配置才能在命令行中启动 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>

4

1 回答 1

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)。它应该在测试之前自动运行并在测试之后关闭。

于 2013-11-10T07:53:38.850 回答