tomcat7-maven-plugin:运行多个 webapps 但端口 8080 已在使用中

Posted

技术标签:

【中文标题】tomcat7-maven-plugin:运行多个 webapps 但端口 8080 已在使用中【英文标题】:tomcat7-maven-plugin: run multiple webapps but port 8080 already in use 【发布时间】:2013-03-25 16:00:36 【问题描述】:

我正在尝试使用tomcat7-maven-plugin 设置嵌入式tomcat 容器,在pre-integration-test 阶段运行webapps,运行集成测试,然后在post-integration-test 阶段关闭tomcat。该项目是一个多模块的maven项目(包含app1app2等)。父 pom.xml 如下所示,

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.tomcat.maven</groupId>
      <artifactId>tomcat7-maven-plugin</artifactId>
      <version>2.1</version>
      <configuration>
        <contextFile>path/to/context.xml</contextFile>
        <tomcatUsers>path/to/tomcat-users.xml</tomcatUsers>
        <fork>true</fork>
      </configuration>
      <executions>
        <execution>
          <id>tomcat-run</id>
          <goals>
            <goal>run</goal>
          </goals>
          <phase>pre-integration-test</phase>
        </execution>
        <execution>
          <id>tomcat-shutdown</id>
          <goals>
            <goal>shutdown</goal>
          </goals>
          <phase>post-integration-test</phase>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

然后我就运行mvn clean integration-test --projects=app1,app2当第二个应用启动时,我得到java.net.BindException

SEVERE: Failed to initialize end point associated with ProtocolHandler ["http-bio-8080"]
java.net.BindException: Address already in use <null>:8080
    at org.apache.tomcat.util.net.JIoEndpoint.bind(JIoEndpoint.java:406)
    at org.apache.tomcat.util.net.AbstractEndpoint.init(AbstractEndpoint.java:610)
    at org.apache.coyote.AbstractProtocol.init(AbstractProtocol.java:429)
    at org.apache.coyote.http11.AbstractHttp11JsseProtocol.init(AbstractHttp11JsseProtocol.java:119)
    at org.apache.catalina.connector.Connector.initInternal(Connector.java:981)
    at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:102)
    at org.apache.catalina.core.StandardService.initInternal(StandardService.java:559)
    at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:102)
    at org.apache.catalina.core.StandardServer.initInternal(StandardServer.java:814)
    at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:102)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:139)
    at org.apache.catalina.startup.Tomcat.start(Tomcat.java:335)
    at org.apache.tomcat.maven.plugin.tomcat7.run.AbstractRunMojo.startContainer(AbstractRunMojo.java:1091)
    at org.apache.tomcat.maven.plugin.tomcat7.run.AbstractRunMojo.execute(AbstractRunMojo.java:512)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:319)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
Caused by: java.net.BindException: Address already in use
    at java.net.PlainSocketImpl.socketBind(Native Method)
    at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:383)
    at java.net.ServerSocket.bind(ServerSocket.java:328)
    at java.net.ServerSocket.<init>(ServerSocket.java:194)
    at java.net.ServerSocket.<init>(ServerSocket.java:150)
    at org.apache.tomcat.util.net.DefaultServerSocketFactory.createSocket(DefaultServerSocketFactory.java:49)
    at org.apache.tomcat.util.net.JIoEndpoint.bind(JIoEndpoint.java:393)
    ... 34 more

那么tomcat7-maven-plugin 中是否有配置可以在嵌入式 tomcat 中运行多个 webapp?

【问题讨论】:

你确定8080端口之前没有绑定过吗?我建议使用 netstat 确保它。是否可以运行单个应用程序? 如果您使用的是 linux,这将为您提供占用端口 的进程 ID:sudo netstat -tulpn | grep . @Michael 是的,端口 8080 未绑定。单个应用程序开始正常。我认为的问题是app1 在嵌入式 tomcat 中启动良好并占用了端口 8080。然后app2 尝试在另一个嵌入式 tomcat 中启动。可能这就是tomcat7:run 的工作方式。如果我使用tomcat7:run-war,插件将运行app1app2 的战争? 【参考方案1】:

参见tomcat7:run mojo 参数:您可以配置插件执行以使用其他端口,类似于此配置:

<execution>
    <id>tomcat-run</id>
    <goals>
        <goal>run</goal>
    </goals>
    <phase>pre-integration-test</phase>
    <configuration>
        <port>9090</port>
    </configuration>
</execution>

当然,为第二个项目使用另一个端口。

【讨论】:

您的意思是在父 pom.xml 中指定 port 配置吗?如何使用多个模块做到这一点? 我的意思是在每个模块(不是父模块)中指定一个特定的端口。您可以在父级中保留相同的配置,只需将特定配置添加到模块中即可。【参考方案2】:

应该支持。您应该在配置中配置 webapps 元素,Tomcat 将部署所有应用程序: http://tomcat.apache.org/maven-plugin-2.0/tomcat7-maven-plugin/run-mojo.html

也请看How to use maven plugin tomcat7:run with multiple contexts (WARs)?

【讨论】:

以上是关于tomcat7-maven-plugin:运行多个 webapps 但端口 8080 已在使用中的主要内容,如果未能解决你的问题,请参考以下文章

tomcat7-maven-plugin 插件调用本地的tomcat吗

maven插件介绍之tomcat7-maven-plugin

tomcat7-maven-plugin 端口

Java tomcat7-maven-plugin 在哪配置启动的根目录

如何在pom.xml中配置“tomcat7-maven-plugin”?

使用tomcat7-maven-plugin部署Maven Web项目