artifacts.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #!/bin/sh
  2. # Description:
  3. # --------------
  4. # An ugly and improvised script to get artifacts than maven can't get due are too old
  5. # or archived. JDEE run `mvn dependency:build-classpath -Dclassifier=sources` to get
  6. # the sources to maven project but when maven can't get the sources this script helps
  7. # to look for the artifact on some repositories or install from manually download
  8. # --------------
  9. # Usage: /artifacts.sh PATH:PACKAGE:TYPE:VERSION PATH_TO_DOWNLOAD (under maven project)
  10. # Example: ./artifacts.sh com.psddev:forms:jar:1.0-20141021.141426-97 $HOME/Downloads/
  11. # ---------------
  12. #
  13. # Copyright (C) 2018 Distopico <distopico@riseup.net>
  14. #
  15. # This program is free software: you can redistribute it and/or modify
  16. # it under the terms of the GNU Affero General Public License as
  17. # published by the Free Software Foundation, either version 3 of the
  18. # License, or (at your option) any later version.
  19. #
  20. # This program is distributed in the hope that it will be useful,
  21. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. # GNU Affero General Public License for more details.
  24. #
  25. # You should have received a copy of the GNU Affero General Public License
  26. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  27. #
  28. CP=/usr/bin/cp
  29. WGET=/usr/bin/wget
  30. MVN=/usr/bin/mvn
  31. REPOS=("http://central.maven.org/maven2/" "https://artifactory.psdops.com/public/")
  32. # get package values
  33. PARTS=()
  34. IFS=':' read -ra ADDR <<< "$1"
  35. for i in "${ADDR[@]}"; do
  36. PARTS+=("$i")
  37. done
  38. version=${PARTS[3]}
  39. numberVersion=(${version//-/ })
  40. file="${PARTS[1]}-${PARTS[3]}.${PARTS[2]}"
  41. packagePath=${PARTS[0]}
  42. packageUrlPath=${packagePath//./\/}
  43. urlPath="$packageUrlPath/${PARTS[1]}/${numberVersion[0]}/$file"
  44. urlPathFallback="$packageUrlPath/${PARTS[1]}/$version/$file"
  45. urlPathSnapshot="$packageUrlPath/${PARTS[1]}/${numberVersion[0]}-SNAPSHOT/$file"
  46. localPath="$HOME/.m2/repository/$packageUrlPath/${PARTS[1]}/$version"
  47. localPathSnapshot="$HOME/.m2/repository/$packageUrlPath/${PARTS[1]}/${numberVersion[0]}-SNAPSHOT"
  48. # Custom download path
  49. tempDir=/tmp/
  50. if [ ! -z "$2" ]; then
  51. tempDir=$2
  52. fi
  53. # Install maven command
  54. COMMAND="install:install-file -DgroupId=${PARTS[0]} -DartifactId=${PARTS[1]} -Dversion=$version -Dclassifier=sources -Dpackaging=jar -Dfile=$tempDir/$file"
  55. ensureSource () {
  56. $CP "$localPath/${PARTS[1]}-$version-sources.${PARTS[2]}" "$localPath/${PARTS[1]}-$version-sources.jar"
  57. if [ $? -ne 0 ]; then
  58. $CP "$localPathSnapshot/${PARTS[1]}-${numberVersion[0]}-SNAPSHOT-sources.${PARTS[2]}" "$localPathSnapshot/${PARTS[1]}-$version-sources.jar"
  59. return $?
  60. fi
  61. return 0
  62. }
  63. download () {
  64. URL=$1
  65. $WGET -q --spider $URL
  66. if [ $? -eq 0 ]; then
  67. echo "Get file from $URL"
  68. $WGET $URL -P $tempDir
  69. $MVN $COMMAND
  70. ensureSource
  71. return 0
  72. fi
  73. return 1
  74. }
  75. for repo in ${REPOS[@]}; do
  76. echo "Try with: $repo"
  77. download "$repo/$urlPath"
  78. if [ $? -eq 0 ]; then
  79. echo "Success!!"
  80. exit 0
  81. break
  82. fi
  83. download "$repo/$urlPathFallback"
  84. if [ $? -eq 0 ]; then
  85. echo "Success!!"
  86. exit 0
  87. break
  88. fi
  89. echo "Look for SNAPSHOT:"
  90. download "$repo/$urlPathSnapshot"
  91. if [ $? -eq 0 ]; then
  92. echo "Success!!"
  93. exit 0
  94. break
  95. fi
  96. echo "Not found"
  97. echo ""
  98. done
  99. # Downloaded manually but helps to add and copy the source to a correct name
  100. echo "Not found"
  101. echo ""
  102. if [ ! -f "$tempDir/$file" ]; then
  103. echo "$tempDir/$file not found!"
  104. exit 1
  105. fi
  106. echo "Try inatall manually: $file"
  107. $MVN $COMMAND
  108. ensureSource
  109. if [ $? -ne 0 ]; then
  110. echo "Error install artefact $file"
  111. exit 1
  112. fi
  113. echo "Success!!"