1set -e 2 3 4destRepo="$(cd $(dirname $0)/../.. && pwd)" 5tempDir="/tmp/import-temp-work" 6rm -rf $tempDir 7mkdir -p $tempDir 8cd $tempDir 9 10function usage() { 11 echo "Usage: $0 group:artifact:version[:classifier][@extension] [group:artifact:version[:classifier][@extension]...] 12 13This script downloads the specified artifacts copies them into the appropriate subdirectory of $destRepo/prebuilts/" 14 exit 1 15} 16 17 18 19 20inputRepo=m2repository 21stageRepo=m2staged 22destAndroidRepo=$destRepo/prebuilts/gradle-plugin 23destThirdPartyRepo=$destRepo/prebuilts/tools/common/m2/repository 24 25 26# usage: downloadArtifacts "$group:$artifact:$version[:classifier][@extension]..." 27function downloadArtifacts() { 28 if [ "$1" == "" ]; then 29 usage 30 fi 31 echo downloading dependencies into $inputRepo 32 rm -rf $inputRepo 33 while [ "$1" != "" ]; do 34 echo importing $1 35 IFS=@ read -r dependency extension <<< "$1" 36 IFS=: read -ra FIELDS <<< "${dependency}" 37 groupId="${FIELDS[0]}" 38 artifactId="${FIELDS[1]}" 39 version="${FIELDS[2]}" 40 classifier="${FIELDS[3]}" 41 42 # download the actual artifact 43 downloadArtifact "$groupId" "$artifactId" "$version" "$classifier" "$extension" 44 45 # try to download the sources jar 46 downloadArtifact "$groupId" "$artifactId" "$version" "sources" "jar" || true 47 48 # go to next artifact 49 shift 50 done 51 echo done downloading dependencies 52} 53 54# usage: downloadArtifact "$group" "$artifact" "$version" "$classifier" "$extension" 55function downloadArtifact() { 56 pomPath="$PWD/pom.xml" 57 echo creating $pomPath 58 pomPrefix='<?xml version="1.0" encoding="UTF-8"?> 59<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"> 60 <modelVersion>4.0.0</modelVersion> 61 <groupId>com.google.android.build</groupId> 62 <artifactId>m2repository</artifactId> 63 <version>1.0</version> 64 <repositories> 65 <repository> 66 <id>google</id> 67 <name>Google</name> 68 <url>https://maven.google.com</url> 69 </repository> 70 <repository> 71 <id>jcenter</id> 72 <name>JCenter</name> 73 <url>https://jcenter.bintray.com</url> 74 </repository> 75 </repositories> 76 <dependencies> 77' 78 79 pomSuffix=' 80 </dependencies> 81 <build> 82 <plugins> 83 <plugin> 84 <groupId>org.apache.maven.plugins</groupId> 85 <artifactId>maven-dependency-plugin</artifactId> 86 <version>2.8</version> 87 <executions> 88 <execution> 89 <id>default-cli</id> 90 <configuration> 91 <includeScope>runtime</includeScope> 92 <addParentPoms>true</addParentPoms> 93 <copyPom>true</copyPom> 94 <useRepositoryLayout>true</useRepositoryLayout> 95 <outputDirectory>m2repository</outputDirectory> 96 </configuration> 97 </execution> 98 </executions> 99 </plugin> 100 </plugins> 101 </build> 102</project> 103' 104 105 106 groupId="$1" 107 artifactId="$2" 108 version="$3" 109 classifier="$4" 110 extension="$5" 111 pomDependencies="" 112 113 114 dependencyText=$(echo -e "\n <dependency>\n <groupId>${groupId}</groupId>\n <artifactId>${artifactId}</artifactId>\n <version>${version}</version>") 115 [ $classifier ] && dependencyText+=$(echo -e "\n <classifier>${classifier}</classifier>") 116 [ $extension ] && dependencyText+=$(echo -e "\n <type>${extension}</type>") 117 dependencyText+=$(echo -e "\n </dependency>") 118 119 120 pomDependencies="${pomDependencies}${dependencyText}" 121 122 echo "${pomPrefix}${pomDependencies}${pomSuffix}" > $pomPath 123 echo done creating $pomPath 124 125 echo downloading one dependency into $inputRepo 126 mvn -f "$pomPath" dependency:copy-dependencies 127 echo done downloading one dependency into $inputRepo 128} 129 130# generates an appropriately formatted repository for merging into existing repositories, 131# by computing artifact metadata 132function stageRepo() { 133 echo staging to $stageRepo 134 rm -rf $stageRepo 135 136 for f in $(find $inputRepo -type f | grep -v '\.sha1$' | grep -v '\.md5'); do 137 md5=$(md5sum $f | sed 's/ .*//') 138 sha1=$(sha1sum $f | sed 's/ .*//') 139 relPath=$(echo $f | sed "s|$inputRepo/||") 140 relDir=$(dirname $relPath) 141 142 fileName=$(basename $relPath) 143 writeChecksums="true" 144 145 destDir="$stageRepo/$relDir" 146 destFile="$stageRepo/$relPath" 147 if [ "$fileName" == "maven-metadata-local.xml" ]; then 148 writeChecksums="false" 149 destFile="$destDir/maven-metadata.xml" 150 fi 151 152 mkdir -p $destDir 153 if [ "$writeChecksums" == "true" ]; then 154 echo -n $md5 > "${destFile}.md5" 155 echo -n $sha1 > "${destFile}.sha1" 156 fi 157 cp $f $destFile 158 done 159 160 echo done staging to $stageRepo 161} 162 163function announceCopy() { 164 input=$1 165 output=$2 166 if stat $input > /dev/null 2>/dev/null; then 167 echo copying "$input" to "$output" 168 cp -rT $input $output 169 fi 170} 171 172function exportArtifact() { 173 echo exporting 174 announceCopy $stageRepo/com/android $destAndroidRepo/com/android 175 rm -rf $stageRepo/com/android 176 177 announceCopy $stageRepo/androidx $destAndroidRepo/androidx 178 rm -rf $stageRepo/androidx 179 180 announceCopy $stageRepo $destThirdPartyRepo 181 echo done exporting 182} 183 184 185function main() { 186 downloadArtifacts "$@" 187 stageRepo 188 exportArtifact 189} 190 191main "$@" 192