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

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

+67
maven-2 tomcat
Mar 14 '09 at 20:22
source share
10 answers

I know this thread is outdated, but ...

An interesting link to the documentation provided by Greg is:

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

An expression is what maven uses to get the value in its code. This can come from a configuration file or from the command line.

You can run

 mvn -Dmaven.tomcat.port=8181 tomcat:run-war 
+94
Jul 01 2018-11-12T00:
source share

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

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

Running tomcat with port

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

Passing a URL into a Java Integration Test, which is run by a fail-safe 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"); } 
+25
Sep 26 '12 at 19:44
source share

Using the syntax specified in tomcat-maven-plugin , you can directly specify the port:

 <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>tomcat-maven-plugin</artifactId> <configuration> <server>tomcat-development-server</server> <port>9966</port> </configuration> </plugin> 
+22
Apr 09 2018-10-09T00:
source share

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> 
+9
Dec 02 '15 at 17:23
source share

You can add a port configuration forever by adding an 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> 
+4
Aug 6 '13 at 15:43
source share

If you are using the maven tomcat plugin, you can specify context.xml by adding the plugin configuration block to 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 is located in src/main/webapp/META-INF/context.xml .

Install different ports there.

+2
Mar 14 '09 at 21:12
source share

I think the best and easiest is (if your test is correctly tied to the 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> 
+1
Jun 22 '13 at 21:33
source share

There is a better and easier way to change Tomcat (not 8080) when starting from Maven

Just edit the application.properties application (if you do not have the application.properties file, then create the application.properties file in the resources directory of your maven project) and set it below the line
server.port = 8181 // You can select your port number.

+1
Nov 02 '16 at 10:15
source share

After spending about 3 hours on how to change the port in POM.xml, here is my last 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 only port did not work, as this is not a property that you can set in <configuration> . We need to understand what causes the problem. In my case, the error was that port 8080 accepted. I changed the port in server.xml to 8081 , but maven did not take it from there. We need to specify this in the configuration field. Here <maven.tomcat.port>8081</maven.tomcat.port> comes to the rescue. Note. You can move port 8081 to something else that you like.

0
Jun 30 '17 at 11:54 on
source share
 <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> 
0
Aug 05 '17 at 3:57 on
source share



All Articles