如何在Heroku平台同时部署.jar文件与.properties文件?
Got it, let's work through getting both your server.jar and server.properties deployed to Heroku properly. The heroku deploy:jar command only uploads the specific JAR you specify, which is why your properties file didn't come along for the ride. Here are two reliable, common approaches to fix this:
Option 1: Package the Properties File Into Your JAR
If you don't mind bundling your config directly with the application, this is the simplest method. Most Java build tools (Maven, Gradle) automatically include files from the src/main/resources directory in the final JAR.
For Maven projects:
- Move your
server.propertiesinto thesrc/main/resourcesfolder of your project. - Rebuild your JAR with
mvn clean package—this embeds the properties file inside the JAR. - Deploy as before with
heroku deploy:jar server.jar --app myapp. - In your code, read the properties file using the classpath (e.g.,
getClass().getResourceAsStream("/server.properties")).
- Move your
For Gradle projects:
- Place
server.propertiesinsrc/main/resources. - Run
./gradlew buildto generate the JAR with the embedded config. - Deploy using the same
heroku deploy:jarcommand.
- Place
Option 2: Deploy via Git (Keep JAR and Properties Separate)
If you want to keep the JAR and properties file separate (maybe for easier config updates without rebuilding the JAR), use Git to deploy all files together.
- Create a new empty directory on your local machine.
- Copy your
server.jarandserver.propertiesinto this directory. - Create a Procfile (no file extension) in the same directory. Add a line defining how to start your app—adjust the command to match how your app reads the properties file:
Note: Replaceweb: java -jar server.jar --config=server.properties--config=server.propertieswith the actual argument your application expects to load the properties file. - Initialize a Git repo in this directory:
git init git add . git commit -m "Add JAR and config file" - Link the repo to your Heroku app:
heroku git:remote -a myapp - Deploy to Heroku:
git push heroku main
Heroku will detect the Procfile and deploy all files in your repo, including the properties file.
Quick Notes
- If you previously used
heroku deploy:jar, switching to Git deployment will overwrite the existing app on Heroku—this is expected behavior. - If your
server.propertiescontains sensitive data (like API keys), consider using Heroku's config vars instead (set them withheroku config:set KEY=valueand read them as environment variables in your code) to avoid committing sensitive info to Git.
内容的提问来源于stack exchange,提问作者Maximax67

