简体   繁体   English

如何使用maven更改tomcat服务器的端口号

[英]How to change port number for tomcat server using maven

I am writing a Rest service using spring mvc framework and maven.我正在使用 spring mvc 框架和 maven 编写一个 Rest 服务。 I am using tomcat server for now.我现在正在使用tomcat服务器。 My pom for the project is我对该项目的pom是

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0     http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.springframework</groupId>
<artifactId>gs-rest-service</artifactId>
<version>0.1.0</version>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.2.5.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>

    </dependency>
</dependencies>

<properties>
    <java.version>1.8</java.version>
</properties>


<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>

    </plugins>

</build>

<repositories>
    <repository>
        <id>spring-releases</id>
        <url>https://repo.spring.io/libs-release</url>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>spring-releases</id>
        <url>https://repo.spring.io/libs-release</url>
    </pluginRepository>
</pluginRepositories>
</project>

This project uses tomcat server and runs on port 8080 by default.本项目使用tomcat服务器,默认运行在8080端口。 can anyone help me understand from where it picks up this configuration and how to change the port on which tomcat runs.谁能帮助我了解它从哪里获取此配置以及如何更改运行 tomcat 的端口。

My preliminary analysis tells me there is some configuration done in spring.boot plugin that I need to override in my pom.我的初步分析告诉我在 spring.boot 插件中完成了一些配置,我需要在我的 pom 中覆盖这些配置。 Can anyone help me in overriding tomcat default port and run it on some other port.任何人都可以帮助我覆盖tomcat默认端口并在其他端口上运行它。

27.3.4 Customizing embedded servlet containers says (in part) 27.3.4自定义嵌入式servlet容器 (部分)

Common servlet container settings can be configured using Spring Environment properties. 可以使用Spring Environment属性配置公共servlet容器设置。 Usually you would define the properties in your application.properties file. 通常,您将在application.properties文件中定义属性。

Common server settings include: 常用服务器设置包括:

  • Network settings: listen port for incoming HTTP requests ( server.port ), interface address to bind to server.address , etc. 网络设置:侦听传入HTTP请求的端口( server.port ),绑定到server.address接口地址等。

So, create a src/main/resources/application.properties and add 所以,创建一个src/main/resources/application.properties并添加

 server.port=${port:8081}

(or whatever port you want). (或任何你想要的端口)。

we can change in application.properties of spring boot application like this 我们可以像这样改变spring boot应用程序的application.properties

server.port=${port:8080} server.port = $ {端口:8080}

  • Go to( your Tomcat destination) 转到(您的Tomcat目的地)
  • C:\\Program Files\\Apache Software Foundation\\Tomcat 8.0\\conf C:\\ Program Files \\ Apache Software Foundation \\ Tomcat 8.0 \\ conf
  • Inside conf you will find server.xml. 在conf中你会找到server.xml。
  • You can change the connector port to something like this. 您可以将连接器端口更改为此类似的内容。

Connector port="8081" (port number that you wish) 连接器端口=“8081”(您希望的端口号)

Make sure that you do Update your Maven Project as well when a new application.properties file is created at src/main/resources .确保在src/main/resources创建新的application.properties文件时也Update Maven 项目。 Otherwise, sometimes the changes may not get reflect at your end.否则,有时这些更改可能不会在您的最后得到体现。

Update Maven Project:更新 Maven 项目:
Right Click Your Maven Project -> Maven -> Update Project Right Click Your Maven Project -> Maven -> Update Project

Example port number setting in application.properties file: application.properties 文件中的示例端口号设置:

server.port = 4444

Note:笔记:
Update Maven project may not be required each time you change the port.每次更改端口时可能不需要更新 Maven 项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM