Ali Yılmaz
Ali Yılmaz

Reputation: 1695

maven: can't override server.port for tomcat server

I am new with Maven/SpringBoot and trying to deploy a repository with different Tomcat Server port.

By default, I would be happy to run tomcat on :8080. But today, I wanted to add Jenkins pipelines to my project and I deployed tomcat on :8080 (with jenkins on it) before my spring cloud gateway repository.

Now, once I try to deploy gateway, compiler obviously says address :8080 already in use.

Now, I want my gateway to deploy Tomcat on another port, (or use already-existing tomcat on :8080 if possible?) so I wanted to deploy it using this command:

$ mvn spring-boot:run -Dserver.port=8181

However, same error based on :8080 happens to appear:

[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.8.RELEASE:run (default-cli) on project crw-gateway: An exception occurred while running. null: InvocationTargetException: Connector configured to listen on port 8080 failed to start.

I tried putting server.port=8080 to application.properties or application-dev.properties files but I cant override it.

Any ideas? How can I override the port? Is there a possibility that I can use already existing tomcat-server on :8080?

Thank you for your time!

EDIT: I had my configurations under ~/config folder. There, I had gateway.properties, which included the line server.port=8080. It has overridden the command line interface as the accepted answer asserts. Changing it to 8888 worked.

Upvotes: 3

Views: 1607

Answers (1)

Mickael
Mickael

Reputation: 4558

According to Spring Boot documentation :

Spring Boot uses a very particular PropertySource order that is designed to allow sensible overriding of values. Properties are considered in the following order:

  1. Devtools global settings properties on your home directory (~/.spring-boot-devtools.properties when devtools is active).
  2. @TestPropertySource annotations on your tests.
  3. @SpringBootTest#properties annotation attribute on your tests.
  4. Command line arguments.
  5. Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property).
  6. ServletConfig init parameters.
  7. ServletContext init parameters.
  8. JNDI attributes from java:comp/env.
  9. Java System properties (System.getProperties()).
  10. OS environment variables.
  11. A RandomValuePropertySource that has properties only in random.*.
  12. Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants).
  13. Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants).
  14. Application properties outside of your packaged jar (application.properties and YAML variants).
  15. Application properties packaged inside your jar (application.properties and YAML variants).
  16. @PropertySource annotations on your @Configuration classes.
  17. Default properties (specified by setting SpringApplication.setDefaultProperties).

So your problem is that your command line (4.) can not override the application.properties file configuration (13., 14., 15.).

If you want to override the server.port property, you need to ensure to respect this order in your configuration.

Upvotes: 2

Related Questions