╔·│╔jar░№.sh 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/usr/bin/env bash
  2. #在Linux、macOS系统终端中运行这个文件,自动完成java文件编译和打包成jar
  3. dir=`pwd`; dir=`basename $dir`;
  4. if [ "$dir" != "scripts" ]; then echo "请到scripts目录中运行本脚本。"; exit; fi
  5. clear
  6. cd ../
  7. #修改这里指定需要使用的JDK(/结尾bin目录完整路径),否则将使用已安装的默认JDK
  8. jdkBinDir=""
  9. #jdkBinDir="/home/download/jdk-19.0.1/bin/"
  10. if [ "$jdkBinDir" == "" ]; then
  11. echo "正在读取JDK版本(如需指定JDK为特定版本或目录,请修改本sh文件内jdkBinDir为JDK bin目录):"
  12. else
  13. echo "正在读取JDK(${jdkBinDir})版本:"
  14. fi
  15. function err(){ echo -e "\e[31m$1\e[0m"; }
  16. ${jdkBinDir}javac -version
  17. [ ! $? -eq 0 ] && { err "需要安装JDK才能编译java文件"; exit; }
  18. function JarN(){
  19. echo ""
  20. echo "请选择需要的生成操作:"
  21. echo " 1. 仅生成依赖jar文件(放到其他项目中Java代码调用,不含Test.java)"
  22. echo " 2. 生成可运行jar文件(包含Test.java控制台程序)"
  23. echo " 3. 退出"
  24. read -p "请输入序号:" step
  25. echo ""
  26. if [ "$step" == 1 ]; then Jar1;
  27. elif [ "$step" == 2 ]; then Jar2;
  28. elif [ "$step" == 3 ]; then exit;
  29. else echo "序号无效!请重新输入"; fi
  30. read -s -n1 -p "按任意键继续...";
  31. echo ""
  32. JarN;
  33. }
  34. function Clazz(){
  35. echo 编译中...
  36. ${jdkBinDir}javac -encoding utf-8 -cp "./*" $1
  37. [ ! $? -eq 0 ] && { err "java文件编译失败"; return 1; }
  38. dir="target/classes/com/github/xiangyuecn/areacity/query"
  39. if [ -e $dir ]; then rm -r target/classes > /dev/null 2>&1; fi
  40. mkdir -p $dir
  41. mv *.class $dir
  42. echo 编译完成,正在生成jar...
  43. }
  44. function Jar1(){
  45. Clazz AreaCityQuery.java
  46. [ ! $? -eq 0 ] && { return 1; }
  47. dir="target/jarLib/"
  48. if [ ! -e $dir ]; then mkdir -p $dir; fi
  49. jarPath="${dir}areacity-query-geometry.lib.jar"
  50. ${jdkBinDir}jar cf $jarPath -C target/classes/ com
  51. [ ! $? -eq 0 ] && { err "生成jar失败"; return 1; }
  52. cp jts-core-*.jar $dir
  53. echo "已生成jar,文件在源码根目录:${jarPath},请copy这个jar + jts-core-xxx.jar 到你的项目中使用。"
  54. }
  55. function Jar2(){
  56. Clazz "*.java"
  57. [ ! $? -eq 0 ] && { return 1; }
  58. dir=target/jarConsole/
  59. dir_libs=${dir}libs/
  60. [ ! -e $dir ] && { mkdir -p $dir; }
  61. [ ! -e $dir_libs ] && { mkdir -p $dir_libs; }
  62. jarPath=${dir}areacity-query-geometry.console.jar
  63. cp *.jar $dir_libs
  64. jarArr=""
  65. for a in `ls $dir_libs`; do jarArr="${jarArr} libs/${a}"; done
  66. echo Class-Path: $jarArr
  67. MANIFEST=target/classes/MANIFEST.MF
  68. echo Manifest-Version: 1.0>$MANIFEST
  69. echo Class-Path:${jarArr}>>$MANIFEST
  70. echo Main-Class: com.github.xiangyuecn.areacity.query.Test>>$MANIFEST
  71. ${jdkBinDir}jar cfm $jarPath target/classes/MANIFEST.MF -C target/classes/ com
  72. [ ! $? -eq 0 ] && { err "生成jar失败"; return 1; }
  73. echo "已生成jar,文件在源码根目录:${jarPath},libs内已包含依赖的其他jar文件,使用时请全部复制。"
  74. echo "请到这个文件夹里面后,执行命令运行这个jar:"
  75. echo " java -jar areacity-query-geometry.console.jar"
  76. }
  77. JarN;