Snapcraft打包JavaFX Maven项目失败,寻求解决方案
问题:Snapcraft打包JavaFX Maven应用失败
系统信息
Ubuntu 22.04.3 LTS Apache Maven 3.6.3 Snapcraft 7.5.4 java 21.0.1 2023-10-17 LTS
尝试的配置及报错
配置1:用nil插件打包已编译Jar包
yaml配置:
name: green-collectives base: core22 version: '1.0' summary: Green Collectives - JavaFX Application description: | A Desktop Application built with javaFx with the intention of solving relevant problems and inefficiencies that U.S. Army Service members, and gun enthusiasts face on a daily basis. grade: stable confinement: devmode apps: green-collectives: command: java -jar $SNAP/target/greenSociety-1.0-SNAPSHOT-shaded.jar environment: JAVA_HOME: "$SNAP/home/tyty/.sdkman/candidates/java/21.0.1-oracle/bin" parts: green-collectives: plugin: nil stage-packages: - default-jre
报错:
Command '['snap', 'pack', '--filename', 'green-collectives_1.0_amd64.snap', '--compression', 'xz', PosixPath('/root/prime'), PosixPath('/root/project')]' returned non-zero exit status 1. Failed to execute pack in instance. Recommended resolution: Run the same command again with --debug to shell into the environment if you wish to introspect this failure.
配置2:用Maven插件拉取源码编译
yaml配置片段:
parts: green-collectives: plugin: maven source: https://github.com/tcdickson/Green-Collectives.git source-type: git source-tag: master
报错:
Environment validation failed for part 'green-collectives': 'mvn' not found and part 'green-collectives' does not depend on a part named 'maven-deps' that would satisfy the dependency.
说明:本地已安装Maven,项目可正常编译打包。
解决方案
修复配置1的问题
- 修正JAVA_HOME路径:Snapcraft构建环境无法访问宿主的SDKMAN路径,改用stage-packages引入的JRE路径,同时完善PATH:
environment: JAVA_HOME: "$SNAP/usr/lib/jvm/default-java" PATH: "$JAVA_HOME/bin:$PATH" - 确保Jar包被正确复制:nil插件不会自动复制项目文件,添加
override-stage步骤手动复制Jar包到stage目录:
同时修改command路径为parts: green-collectives: plugin: nil stage-packages: - default-jre override-stage: | cp $SNAPCRAFT_PROJECT_DIR/target/greenSociety-1.0-SNAPSHOT-shaded.jar $SNAPCRAFT_STAGE/java -jar $SNAP/greenSociety-1.0-SNAPSHOT-shaded.jar,因为stage后的文件会放在$SNAP根目录。
修复配置2的问题
使用Maven插件时,必须引入maven-deps依赖提供Maven环境,修改parts配置:
parts: maven-deps: plugin: nil stage-packages: - maven green-collectives: plugin: maven source: https://github.com/tcdickson/Green-Collectives.git source-type: git source-tag: master after: [maven-deps] maven-options: ["-DskipTests"] # 可选,跳过测试加快构建
另外,JavaFX应用需要额外依赖图形库,添加stage-packages引入OpenJFX:
stage-packages: - openjfx
通用注意事项
- 构建时加
--debug参数进入shell,检查文件路径和环境变量:snapcraft --debug - JavaFX应用在devmode下需添加桌面权限,在apps部分补充:
plugs: - desktop - desktop-legacy - wayland - x11 - 确保shaded Jar包包含所有JavaFX依赖,或者通过snap的stage-packages引入完整的OpenJFX库。
内容的提问来源于stack exchange,提问作者tcdickson
相关产品推荐
相关产品推荐

