Gradle构建的Spring Boot应用本地正常但无法在Heroku运行求助
Hey there, let's tackle this Heroku deployment issue with your Spring Boot + Gradle app. I’ve seen this crop up a few times, so let’s walk through the most common fixes first, and then I’ll point out what details you should share to narrow it down further.
1. Confirm Heroku detects your Gradle project correctly
Heroku relies on specific files to recognize your app as a Gradle project. Double-check you’ve committed these to Git:
build.gradle(orsettings.gradlefor multi-module projects)gradlew(the Gradle wrapper script—Heroku uses this instead of a system-wide Gradle installation, so it’s critical)gradlew.bat(even if you’re on Windows, commit this for Heroku’s cross-platform build process)
If any of these are missing or in your .gitignore, Heroku might skip the Gradle build pipeline entirely.
2. Verify your JAR configuration in build.gradle
Since you suspect JAR-related issues, let’s make sure your Spring Boot plugin setup is correct. Here’s a minimal, working example of what your build.gradle should include to generate an executable JAR:
plugins { id 'java' id 'org.springframework.boot' version '3.2.0' id 'io.spring.dependency-management' version '1.1.4' } group = 'com.yourcompany' version = '0.0.1-SNAPSHOT' java { sourceCompatibility = '17' } repositories { mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' testImplementation 'org.springframework.boot:spring-boot-starter-test' } tasks.named('test') { useJUnitPlatform() }
The bootJar task (auto-added by the Spring Boot plugin) creates an executable JAR with all dependencies bundled in—this is exactly what Heroku needs to run your app.
3. Fix your Procfile (if you’re using one)
A correct Procfile for a Spring Boot executable JAR follows this format:
web: java -jar build/libs/<your-app-name>-<version>.jar
Replace <your-app-name> and <version> with the actual values from your build.gradle (the JAR filename matches your group, name, and version settings). If you tried skipping the Procfile and it still failed, Heroku might have struggled to auto-detect the correct startup command—check the logs for clues here.
4. Dig into Heroku’s logs for specific errors
Run this command locally to pull and stream your full Heroku logs:
heroku logs --tail
Look for these common red flags:
Error: Could not find or load main class→ Your JAR might not have the correct main class configured, or the build produced a corrupted JAR.Permission denied→ Ensure yourgradlewscript has execute permissions (runchmod +x gradlewbefore committing to Git).Out of memory→ Heroku’s default dyno size might be too small; you can upgrade the dyno or tweak your app’s JVM memory settings.
What to share next for a precise fix
To diagnose this faster, please add:
- The full content of your
build.gradle(redact any sensitive info like API keys or database credentials) - The exact content of your Procfile (if you used one)
- The relevant error snippets from Heroku’s logs (focus on lines around the startup failure)
Once you share those details, we can pinpoint the exact issue!
内容的提问来源于stack exchange,提问作者Martin

