tomcat7-maven-plugin: run multiple webapps but port 8080 already in use
Asked Answered
B

2

6

I'm trying to use tomcat7-maven-plugin to setup an embedded tomcat container, run webapps in pre-integration-test phase, run integration tests, then shutdown tomcat in post-integration-test phase. The project is a multi-module maven project (contains app1, app2, etc). Parent pom.xml looks like following,

<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>

Then I just run mvn clean integration-test --projects=app1,app2. When the second app is starting, I get 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

So is there a configuration in tomcat7-maven-plugin to run multiple webapps in an embedded tomcat?

Byram answered 3/4, 2013 at 19:45 Comment(3)
Are you sure the port 8080 was not binded before? I suggest to ensure it using netstat. Is it possible to run the single application?Laky
if you're on linux this will give you the process ID which occupies port <PORT>: sudo netstat -tulpn | grep <PORT>.Swelter
@Laky Yes port 8080 was not binded. Single app started fine. The problem I think is that app1 started fine in an embedded tomcat and took port 8080. Then app2 tried to start in another embedded tomcat. Probably this is how tomcat7:run works. If I use tomcat7:run-war, the plugin will run wars of of app1 and app2 altogether?Byram
A
14

See tomcat7:run mojo parameters: you can configure plugin executions to use other ports, with something similar to this config:

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

And of course use another port for the second project.

Arvonio answered 4/4, 2013 at 7:37 Comment(2)
Did you mean specifying port config in parent pom.xml? How do I do this with multiple modules?Byram
I mean specifying in each module (not the parent) a specific port. You can keep the same configuration in the parent, and just add the specific configuration to the modules.Arvonio
L
0

It should be supported. You should configure the webapps element in the configuration and Tomcat will deploy all applications: http://tomcat.apache.org/maven-plugin-2.0/tomcat7-maven-plugin/run-mojo.html

Look also How to use maven plugin tomcat7:run with multiple contexts (WARs)?

Laky answered 5/4, 2013 at 6:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.