Build Spring project for run on another system

2019-09-15 07:22发布

问题:

I Successfully create a spring boot project on my own local system. I want to build a jar file so I can install it on remote server. so I had to configure server address and mySql address of remote server but I can not Build and it have many errors, and they all right cause my system can not see the remote server address and database.

this is my .properties file:

spring.datasource.url=jdbc:mysql://localhost:8081/aths
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.hibernate.ddl-auto=create
server.address=192.168.24.250
server.port=8080

how can handle it for running on another configurations? ( another IP, datasource, and ...) Am I doing it right or not? thanks

回答1:

You can use spring profiles here : Create different property files for different profiles using application-{profile}.properties format, e.g. application-dev.properties for dev, and application-prod.properties for production put your profile specific configurations in them. Then when you're running the spring boot application, activate your intended profile using the SPRING_PROFILES_ACTIVE environment variable or spring.profiles.active system property.

and at the end, you will run your jar file with command java -jar -Dspring.profiles.active=prod application.jar



回答2:

You can have different application.properties within your resources folder and use spring profiles for example application-{profile}.properties and run the application with the specified profile. However this still limits the configuration items to what has been hard coded within the properties files. When running the application, if it was to be distributed to other people, where non of the profiles are supported you can provide a properties file at start up.

So in the same directory for example as the .jar file create a file named application.properties with empty place holders for all the variables required for the application so the admin can enter the details correct for them. Then they will be required to start the application with the following command

java -jar 'applicaitonname.jar -Dspring.config.name="file:/path/to/application.properties"

Or springboot will load properties from application.properties files in the following locations:

  1. A /config subdirectory of the current directory.
  2. The current directory
  3. Failing that the default application.properties from the resources folder will be loaded.