Java Telegram Bot在IntelliJ IDEA运行正常,部署Heroku失败求助
部署问题描述
我的Telegram Java机器人在IntelliJ IDEA中运行完全正常,但部署到Heroku后无法正常工作。我推测问题出在manifest配置上,由于我从未有过项目部署经验,无法理解manifest文件和pom.xml的配置规则,恳请协助排查。
现有配置
当前pom.xml内容
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>PhotoBot</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> </project>
注意:你当前的pom未闭合<project>标签,也缺失所有依赖、打包配置
主类当前代码
public class Main { public static void main(String[] args) { ApiContextInitializer.init(); TelegramBotsApi botsApi = new TelegramBotsApi(); try{ botsApi.registerBot(new PhotoBot()); }catch (TelegramApiException e){ e.printStackTrace(); } } }
配套参考素材
- 主类结构截图
- 项目结构截图
- pom.xml配置截图
排查与修改步骤
1. 补全pom.xml核心配置
不需要手动编写MANIFEST.MF文件,以下maven插件配置会自动在打包时生成符合要求的manifest条目,指定主类和依赖加载规则:
- 补全Telegram Bot API依赖:
<dependencies> <dependency> <groupId>org.telegram</groupId> <artifactId>telegrambots</artifactId> <version>6.5.0</version> </dependency> </dependencies>
- 补全可执行胖JAR打包配置(复制到
</properties>标签后):
<build> <plugins> <!-- 打包含所有依赖的可执行JAR --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.3.0</version> <configuration> <archive> <manifest> <!-- 替换为你主类的全限定名,例如org.example.Main --> <mainClass>org.example.Main</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
2. Heroku 项目根目录新增必备配置文件
Procfile(无后缀名)
写入启动命令,替换为你打包生成的JAR文件名(默认路径在target目录下):
worker: java -jar target/PhotoBot-1.0-SNAPSHOT-jar-with-dependencies.jar
Telegram Bot为长连接后台服务,使用worker进程类型即可,不需要绑定公网端口
system.properties(无后缀名)
指定Heroku运行的JDK版本,匹配你编译的1.8版本:
java.runtime.version=1.8
3. 上线前校验
- 本地执行
mvn clean package打包后,先运行java -jar target/PhotoBot-1.0-SNAPSHOT-jar-with-dependencies.jar,确认JAR包能正常启动、机器人能正常收发消息,再上传到Heroku - Bot Token、API密钥等敏感信息不要硬编码在代码里,配置为Heroku环境变量后通过
System.getenv()读取 - 部署后通过Heroku控制台查看运行日志,定位具体报错
内容的提问来源于stack exchange,提问作者Waxzoda Rasulmetova
相关产品推荐
相关产品推荐

