【发布时间】:2025-12-23 12:40:06
【问题描述】:
是否有一种简单的方法可以在 pom 或命令行中为 Tomcat 指定备用端口。我想在同一台机器上运行多个项目。
【问题讨论】:
-
mvn -Dmaven.tomcat
.port= tomcat :run Example mvn -Dmaven.tomcat7.port=8585 tomcat7:run OR in POM.XML ... 8585
是否有一种简单的方法可以在 pom 或命令行中为 Tomcat 指定备用端口。我想在同一台机器上运行多个项目。
【问题讨论】:
我知道这是一个非常古老的问题,还没有答案。
当我需要将使用外部 tomcat 的旧项目转换为使用 tomcat7-maven-plugin 的嵌入式 tomcat 时,我遇到了类似的问题。
而我需要的是构建一个可执行的 jar。
但是现有的答案对我不起作用......
无论我在mvn package 之后运行java -jar project-name.jar。它总是在端口8080 上运行,这不是我想要的......
然后我搜索文档Executable War
我修复的只是在命令中添加一个参数
java -jar my_project.jar -httpPort 9091
在文档中:
用法:java -jar [你的 exec war jar 的路径] ...
-httpPort 使用的http端口
-httpsPort https 使用的端口...
希望它有用。
【讨论】:
java -Dserver.port=8888 -jar target/demo-0.0.1-SNAPSHOT.war 或java -Dserver.port=8888 -jar target/demo-0.0.1-SNAPSHOT.jar (docs.spring.io/spring-boot/docs/current/reference/htmlsingle/…)
使用tomcat-maven-plugin上给出的语法,可以直接指定端口:
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>tomcat-maven-plugin</artifactId> <configuration> <server>tomcat-development-server</server> <port>9966</port> </configuration> </plugin>
【讨论】:
<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>
【讨论】:
花了大约 3 个小时研究如何更改 POM.xml 中的端口,这是我的最新解决方案。
<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>
仅使用port 不起作用,因为这不是您可以在<configuration> 中设置的属性。我们需要了解导致问题的原因。就我而言,错误是端口8080 被占用。我将server.xml 中的端口更改为8081,但maven 并没有从那里获取它。我们需要在configuration 字段中具体说明。这就是<maven.tomcat.port>8081</maven.tomcat.port> 救援的地方。注意:您可以将端口 8081 更改为您喜欢的其他端口。
【讨论】:
从 Maven 开始时,有一种最好和最简单的方法来更改 Tomcat(不是 8080)
只需编辑您的 application.properties(如果您没有 application.properties 文件,则在 maven 项目的资源目录中创建一个 application.properties 文件)文件并设置以下行
server.port=8181 //你可以选择你的端口号。
【讨论】:
以下对我有用:
<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>
【讨论】:
当我有几个小的 servlet 同时运行它们的集成测试阶段时,我遇到了类似的问题,这成为一个问题,因为它们被配置为使用相同的端口。但是感谢build-helper-maven-plugin:reserve-network-port 目标,可以获得可用的随机端口号。然后我可以创建一个包含http://localhost:[port]/[servletname]的URL,即feed intoJava测试类。
检索随机端口:
<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>
用端口启动tomcat
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat6-maven-plugin</artifactId>
<configuration>
<port>${tomcat.http.port}</port>
<useTestClasspath>true</useTestClasspath>
</configuration>
....
</plugin>
将 URL 提供给故障安全插件运行的 Java 集成测试
<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 代码
public class DownloadAreaIT {
private static final String URL = System.getProperty("integration-test.url");
}
【讨论】:
您可以通过向其添加属性端口来永久添加端口配置。
<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>
【讨论】:
我认为最好和最简单的是(如果您的测试正确绑定到集成阶段):
<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>
【讨论】:
我知道这个帖子很旧,但是……
Greg 提供的文档链接很有趣:
port:
The port to run the Tomcat server on.
Type: int
Required: No
Expression: ${maven.tomcat.port}
Default: 8080
表达式是 maven 用来在其代码中获取值的。它可以来自配置文件,也可以来自命令行。
你可以跑
mvn -Dmaven.tomcat.port=8181 tomcat:run-war
【讨论】:
mvn -Dmaven.tomcat.port=8181 tomcat:run
如果你使用的是maven tomcat插件,你可以specify a context.xml在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>
使用的默认 context.xml 文件位于 src/main/webapp/META-INF/context.xml。
在那里设置不同的端口。
【讨论】: