Alternate port for Tomcat (not 8080) when starting with Maven?

TomcatMaven 2

Tomcat Problem Overview


Is there an easy way to specify an alternate port for Tomcat in the pom or on the commandline. I'd like to have several projects running on the same machine.

Tomcat Solutions


Solution 1 - Tomcat

I know this thread is old but…

The link to the documentation, provided by Greg is interesting :

port:
The port to run the Tomcat server on.
    Type: int
    Required: No
    Expression: ${maven.tomcat.port}
    Default: 8080

The expression is what maven use to get the value in its code. It could come from a configuration file, or from the command line.

You could run

mvn -Dmaven.tomcat.port=8181 tomcat:run-war

Solution 2 - Tomcat

Using the syntax given on tomcat-maven-plugin, you can directly specify the port:

> > org.codehaus.mojo > tomcat-maven-plugin >
> tomcat-development-server > 9966 >
>

Solution 3 - Tomcat

I had a similar problem when I had several small servlets that run their integration-test phase at the same time, which became a problem as those were configured to use the same port. But thanks to the build-helper-maven-plugin:reserve-network-port goal, it is possible to get random port numbers that are available. Then I can create an URL containing http://localhost:[port]/[servletname], which is feed into the Java test class.

Retrieving random port:

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
	<execution>
		<id>reserve-network-port</id>
		<goals>
			<goal>reserve-network-port</goal>
		</goals>
		<phase>pre-integration-test</phase>
		<configuration>
			<portNames>
				<portName>tomcat.http.port</portName>
			</portNames>
		</configuration>
	</execution>
</executions>

Starting tomcat with the port

<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat6-maven-plugin</artifactId>
<configuration>
	<port>${tomcat.http.port}</port>
	<useTestClasspath>true</useTestClasspath>
</configuration>
....
</plugin>

Feeding the URL to the Java integration test that is run by the failsafe plugin

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.12.3</version>
....
<configuration>
	<systemPropertyVariables>
		<integration-test.url>http://localhost:${tomcat.http.port}/${project.build.finalName}/</integration-test.url>
	</systemPropertyVariables>
</configuration>
</plugin>

Java code

public class DownloadAreaIT {
    private static final String URL = System.getProperty("integration-test.url");
}

Solution 4 - Tomcat

Below worked for me:

		<properties>
			<maven.tomcat.port>9090</maven.tomcat.port>
		</properties>

		<plugin>
			<groupId>org.apache.tomcat.maven</groupId>
			<artifactId>tomcat7-maven-plugin</artifactId>
			<version>2.2</version>
			<configuration>
				<port>${maven.tomcat.port}</port>
			</configuration>
		</plugin>

		

Solution 5 - Tomcat

You coud add the port configuration permanently by adding the attribute port to it.

<build>
	<plugins>
		<plugin>
			<groupId>org.apache.tomcat.maven</groupId>
			<artifactId>tomcat7-maven-plugin</artifactId>
			<version>2.0</version>
			<configuration>
				<port>9090</port>
			</configuration>
		</plugin>
	</plugins>
</build>

Solution 6 - Tomcat

There is best and easy way to change Tomcat (not 8080) when starting with Maven

Just Edit your application.properties(if you have no application.properties file, then create an application.properties file in resources directory of your maven project) file and set below line
server.port=8181 //You can choose your port number.

Solution 7 - Tomcat

If you are using the maven tomcat plugin, you can specify a context.xml by adding a plugin configuration block to the pom.xml:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>tomcat-maven-plugin</artifactId>
        <version>1.0-beta-1</version>
        <configuration>
          <mode>both</mode>
        </configuration>
      </plugin>
    </plugins>
  </build>
  ...
</project>

The default context.xml file used is located at src/main/webapp/META-INF/context.xml.

Set different ports there.

Solution 8 - Tomcat

I think best and simplest is this (if your test are properly bind to integration phase):

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>reserve-network-port</id>
            <goals>
                <goal>reserve-network-port</goal>
            </goals>
            <phase>pre-integration-test</phase>
            <configuration>
                <portNames>      
                    <portName>maven.tomcat.port</portName>
                </portNames>
            </configuration>
        </execution>
    </executions>
</plugin>

Solution 9 - Tomcat

<plugin>
	<groupId>org.apache.tomcat.maven</groupId>
	<artifactId>tomcat7-maven-plugin</artifactId>
	<version>2.2</version>
	<configuration>
		<server>tomcat-development-server</server>
		<port>9090</port>
	</configuration>
</plugin>

Solution 10 - Tomcat

After spending about 3 hours on how to change the port in POM.xml, Here is my latest solution.

<plugin>
  <groupId>org.apache.tomcat.maven</groupId>
  <artifactId>tomcat7-maven-plugin</artifactId>
  <version>2.2</version>
  <configuration>
  <maven.tomcat.port>8081</maven.tomcat.port>
  </configuration>
 </plugin>

Using just port was not working since that is not a property that you can set in <configuration>. We need to understand what is causing the problem. In my case, error was that port 8080 is taken. I changed the port in server.xml to 8081 but maven does not take it from there. We need to specifically tell it in configuration field. This is where <maven.tomcat.port>8081</maven.tomcat.port> comes in rescue. Note: You can chnage the port 8081 to something else you like.

Solution 11 - Tomcat

I know it's a very old question which not have answer yet.

And I've got the similar question when I need to convert a old project which use outer tomcat to embedded tomcat use tomcat7-maven-plugin.

And what I need is build an executable jar. But existing answer cannot work for me... whatever i run java -jar project-name.jar after mvn package. it's always running on port 8080 which is not i wanted... Then I search the doc Executable War and what i fixed is just add a param in command

java -jar my_project.jar -httpPort 9091

which in doc:

> usage: java -jar [path to your exec war jar] ... > > -httpPort http port to use > > -httpsPort https port to use ...

hope it useful.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsammyoView Question on Stackoverflow
Solution 1 - TomcatVincent DemeesterView Answer on Stackoverflow
Solution 2 - TomcatGregView Answer on Stackoverflow
Solution 3 - TomcatredsoloView Answer on Stackoverflow
Solution 4 - TomcatSai Viswanath VKS PalaparthiView Answer on Stackoverflow
Solution 5 - TomcatjiahaoView Answer on Stackoverflow
Solution 6 - TomcatMd Hafezur RahmanView Answer on Stackoverflow
Solution 7 - TomcatPascal ThiventView Answer on Stackoverflow
Solution 8 - TomcatMariuszSView Answer on Stackoverflow
Solution 9 - TomcatPratik GauravView Answer on Stackoverflow
Solution 10 - TomcatCKMView Answer on Stackoverflow
Solution 11 - TomcatSe venView Answer on Stackoverflow