由于已接受答案中的某些链接在 2020 年不再可用或不再适用,距问题提出 8 年,我想分享我今天确认有效的发现。
java自带一个工具javapackager,可以为你打包Windows、Linux、macOS上的java应用。
这里是官方手册:https://docs.oracle.com/javase/8/docs/technotes/tools/unix/javapackager.html
(这里还有其他有用的工具:https://docs.oracle.com/javase/8/docs/technotes/tools/)
为了打包一个mac应用程序,我使用了这个命令:
javapackager \
-deploy \
-native image \
-srcdir ./csv-encrypt-tool-mac \
-srcfiles csv-encrypt-tool.jar \
-srcfiles dict \
-srcfiles config \
-srcfiles log \
-outdir ./dist \
-outfile csv-encrypt-tool \
-appclass some.package.CsvEncToolApp \
-name "csv-encrypt-tool" \
-title "csv-encrypt-tool" \
-nosign \
-v \
-BjvmOptions=-Xmx4096m \
-BmainJar=csv-encrypt-tool.jar \
-Bicon=icon.icns
这就是解释:
-deploy \ # Assembles the application package for redistribution with sys JRE
-native image \ # Build a .app file. If you want a .dmg, use -native dmg
-srcdir ./csv-encrypt-tool-mac \ # directory where my jar and resource files in
-srcfiles csv-encrypt-tool.jar \ # my executable jar, path relative to -srcdir
-srcfiles dict \ # one of my resource directories, path relative to -srcdir
-srcfiles config \ # another one of my resource directories, path relative to -srcdir
-srcfiles log \ # again, one of my resource directories, path relative to -srcdir
-outdir ./dist \ # where I want the package to be put
-outfile csv-encrypt-tool \ # the output file name without extension, the final file (or bundle which is a directory actually) will be csv-encrypt-tool.app
-appclass some.package.CsvEncToolApp \ # the class with main method, which is the entry point of the whole app
-name "csv-encrypt-tool" \ # the name (not very sure what this is for)
-title "csv-encrypt-tool" \ # the title (not sure what it is either)
-nosign \ # not sign the app since this is just an internal tool, if you want to publish it, signing is necessary
-v \ # verbose
-BjvmOptions=-Xmx4096m \ # I need 4GB max heap size
-BmainJar=csv-encrypt-tool.jar \ # the jar file with main class
-Bicon=icon.icns # the icon
命令执行后,会在dist中创建一些文件和目录,我想要打包的地方,其中一个目录是bundles。应用程序放在那里。
由于我只是构建了一个内部工具,因此无需其他生产就绪选项即可签名和打包。可以参考官方手册获取帮助。
希望这可以帮助那些像我一样在 2020 年不知道如何在 macOS 上打包应用程序的人。