1#!/bin/bash 2 3set -e 4 5readonly DEST_REPO="$(dirname $(readlink -f "$0"))/../.." 6readonly INPUT_REPO="m2repository" 7readonly STAGE_REPO="m2staged" 8readonly DEST_ANDROID_REPO="${DEST_REPO}/prebuilts/manifest-merger" 9 10if ! TEMP_DIR=$(mktemp -d); then 11 echo "ERROR: failed to create dir" 12 exit 1 13fi 14readonly TEMP_DIR 15 16function cleanup() { 17 rm -rf "${TEMP_DIR}" 18} 19 20trap cleanup EXIT 21 22cd "${TEMP_DIR}" 23 24function usage() { 25 cat <<EOF 26Usage: $(basename $0) group:artifact:version[:classifier][@extension] [group:artifact:version[:classifier][@extension]...] 27 28Downloads the specified artifacts into the appropriate subdirectory of ${DEST_REPO}/prebuilts 29 30EOF 31 exit 1 32} 33 34# usage: downloadArtifacts "$group:$artifact:$version[:classifier][@extension]..." 35function downloadArtifacts() { 36 [ -z "$1" ] && usage 37 38 echo "downloading dependencies into ${INPUT_REPO}" 39 rm -rf "${INPUT_REPO}" 40 for arg in $*; do 41 echo "importing ${arg}" 42 IFS=@ read -r dependency extension <<< "${arg}" 43 IFS=: read -ra FIELDS <<< "${dependency}" 44 local -r groupId="${FIELDS[0]}" 45 local -r artifactId="${FIELDS[1]}" 46 local -r version="${FIELDS[2]}" 47 local -r classifier="${FIELDS[3]}" 48 49 # download the actual artifact 50 downloadArtifact "${groupId}" "${artifactId}" "${version}" "${classifier}" "${extension}" 51 52 # try to download the sources jar 53 downloadArtifact "${groupId}" "${artifactId}" "${version}" "sources" "jar" || true 54 55 # go to next artifact 56 done 57 echo "done downloading dependencies" 58} 59 60# usage: generatePomFile "$pomFile" "$group" "$artifact" "$version" "$classifier" "$extension" 61function generatePomFile() { 62 local -r pomFile="$1" 63 local -r groupId="$2" 64 local -r artifactId="$3" 65 local -r version="$4" 66 local -r classifier="$5" 67 local -r extension="$6" 68 69 local pomDependencies="" 70 71 echo "creating ${pomFile}" 72 cat <<EOF > "${pomFile}" 73<?xml version="1.0" encoding="UTF-8"?> 74<project xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 75 <modelVersion>4.0.0</modelVersion> 76 <groupId>com.google.android.build</groupId> 77 <artifactId>m2repository</artifactId> 78 <version>1.0</version> 79 <repositories> 80 <repository> 81 <id>google</id> 82 <name>Google</name> 83 <url>https://maven.google.com</url> 84 </repository> 85 <repository> 86 <id>jcenter</id> 87 <name>JCenter</name> 88 <url>https://jcenter.bintray.com</url> 89 </repository> 90 </repositories> 91 <dependencies> 92 <dependency> 93 <groupId>${groupId}</groupId> 94 <artifactId>${artifactId}</artifactId> 95 <version>${version}</version> 96EOF 97 98if [ -n "${classifier}" ]; then 99 cat <<EOF >> "${pomFile}" 100 <classifier>${classifier}</classifier> 101EOF 102fi 103 104if [ -n "${extension}" ]; then 105 cat <<EOF >> "${pomFile}" 106 <type>${extension}</type> 107EOF 108fi 109 110cat <<EOF >> "${pomFile}" 111 </dependency> 112 </dependencies> 113 <build> 114 <plugins> 115 <plugin> 116 <groupId>org.apache.maven.plugins</groupId> 117 <artifactId>maven-dependency-plugin</artifactId> 118 <version>2.8</version> 119 <executions> 120 <execution> 121 <id>default-cli</id> 122 <configuration> 123 <includeScope>runtime</includeScope> 124 <addParentPoms>true</addParentPoms> 125 <copyPom>true</copyPom> 126 <useRepositoryLayout>true</useRepositoryLayout> 127 <outputDirectory>m2repository</outputDirectory> 128 </configuration> 129 </execution> 130 </executions> 131 </plugin> 132 </plugins> 133 </build> 134</project> 135EOF 136 137 echo "done creating ${pomFile}" 138} 139 140# usage: downloadArtifact "$group" "$artifact" "$version" "$classifier" "$extension" 141function downloadArtifact() { 142 local -r pomFile="${PWD}/pom.xml" 143 144 generatePomFile "${pomFile}" "$@" 145 146 echo "downloading one dependency into ${INPUT_REPO}" 147 mvn -f "${pomFile}" dependency:copy-dependencies 148 echo "done downloading one dependency into ${INPUT_REPO}" 149} 150 151# generates an appropriately formatted repository for merging into existing repositories, 152# by computing artifact metadata 153function createStageRepo() ( 154 echo "staging to ${STAGE_REPO}" 155 rm -rf "${STAGE_REPO}" 156 157 for f in $(find "${INPUT_REPO}" -type f ! -name "*.sha1" ! -name "*.md5"); do 158 local relPath="${f##${INPUT_REPO}}" 159 local relDir=$(dirname "${relPath}") 160 161 local fileName=$(basename "${relPath}") 162 local writeChecksums=true 163 164 local destDir="${STAGE_REPO}/${relDir}" 165 local destFile="${STAGE_REPO}/${relPath}" 166 if [ "${fileName}" == "maven-metadata-local.xml" ]; then 167 writeChecksums=false 168 destFile="${destDir}/maven-metadata.xml" 169 fi 170 171 mkdir -p "${destDir}" 172 if ${writeChecksums}; then 173 local md5=$(md5sum "${f}" | sed 's/ .*//') 174 local sha1=$(sha1sum "${f}" | sed 's/ .*//') 175 echo -n ${md5} > "${destFile}.md5" 176 echo -n ${sha1} > "${destFile}.sha1" 177 fi 178 cp "${f}" "${destFile}" 179 done 180 181 echo "done staging to ${STAGE_REPO}" 182) 183 184function announceCopy() { 185 local -r input="$1" 186 local -r output="$2" 187 if [ -e "${input}" ]; then 188 echo "copying '${input}' to '${output}'" 189 cp -rT "${input}" "${output}" 190 fi 191} 192 193function exportArtifact() { 194 echo "exporting" 195 196 mkdir -p "${DEST_ANDROID_REPO}/com/android" 197 announceCopy "${STAGE_REPO}/com/android" "${DEST_ANDROID_REPO}/com/android" 198 rm -rf "${STAGE_REPO}/com/android" 199 200 echo "done exporting" 201} 202 203 204function main() { 205 downloadArtifacts "$@" 206 createStageRepo 207 exportArtifact 208} 209 210main "$@" 211