napstablook napstablook - 6 months ago 82
Java Question

Change the port of a Spring boot application without changing the code

I have two applications that I need to run simultaneously, and both are trying to run on port

8080
. I'd like to change one of them to port
9000
. The application I'm trying to change has spring security, so it runs on port
8443
when using
https
and port
8080
when using
http
. I have to move it from port
8080
without changing any
.java
files. Also, I need to run the other application on port
8080
as well, so changing the default tomcat port wouldn't be a good idea.

I tried adding to
application.properties
the lines
server.port=9000
,
spring.main.server.port=9000
, then running
mvn install
, and then
java -jar target/app.jar
.

I also tried running
java -jar target/app.jar
with different flags:
-Dserver.port=9000
and
--server.port=9000
.

Regardless, I get
- Tomcat started on port(s): 8443 (https) 8080 (http)
.

So, my questions are:


  • How do I get it to run on a port different from
    8080
    ?

  • And, what could be causing the configuration files to not change the port?


Answer

You can do this:

 mvn spring-boot:run -Drun.jvmArguments='-Dserver.port=8088'

Add this to your pom.xml file

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