blob: c8e77b1c7e2868eabdc310443e2ef129635118af [file] [log] [blame]
Jeff Gaston20c9bbc2018-03-07 15:25:43 -05001set -e
2
3
4destRepo="$(cd $(dirname $0)/../.. && pwd)"
5tempDir="/tmp/import-temp-work"
6rm -rf $tempDir
7mkdir -p $tempDir
8cd $tempDir
9
10function usage() {
Andrew Lewisc05bbb62018-05-24 13:59:57 +010011 echo "Usage: $0 group:artifact:version[:classifier][@extension] [group:artifact:version[:classifier][@extension]...]
Jeff Gaston20c9bbc2018-03-07 15:25:43 -050012
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
Jeff Gaston796227a2018-07-13 16:52:32 -040025
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() {
Jeff Gaston20c9bbc2018-03-07 15:25:43 -050056 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>
Aurimas Liutikas82a739e2018-04-03 10:39:30 -070064 <repositories>
65 <repository>
66 <id>google</id>
67 <name>Google</name>
68 <url>https://maven.google.com</url>
69 </repository>
Jeff Gastonf6923e32018-04-18 16:03:33 -040070 <repository>
71 <id>jcenter</id>
72 <name>JCenter</name>
73 <url>https://jcenter.bintray.com</url>
74 </repository>
Aurimas Liutikas82a739e2018-04-03 10:39:30 -070075 </repositories>
Jeff Gaston20c9bbc2018-03-07 15:25:43 -050076 <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
Jeff Gaston796227a2018-07-13 16:52:32 -0400105
106 groupId="$1"
107 artifactId="$2"
108 version="$3"
109 classifier="$4"
110 extension="$5"
Jeff Gaston20c9bbc2018-03-07 15:25:43 -0500111 pomDependencies=""
112
Jeff Gaston20c9bbc2018-03-07 15:25:43 -0500113
Jeff Gaston796227a2018-07-13 16:52:32 -0400114 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}"
Jeff Gaston20c9bbc2018-03-07 15:25:43 -0500121
122 echo "${pomPrefix}${pomDependencies}${pomSuffix}" > $pomPath
123 echo done creating $pomPath
Jeff Gaston20c9bbc2018-03-07 15:25:43 -0500124
Jeff Gaston796227a2018-07-13 16:52:32 -0400125 echo downloading one dependency into $inputRepo
126 mvn -f "$pomPath" dependency:copy-dependencies
127 echo done downloading one dependency into $inputRepo
Jeff Gaston20c9bbc2018-03-07 15:25:43 -0500128}
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
Jeff Gaston8d9231c2018-03-26 19:53:27 -0400166 if stat $input > /dev/null 2>/dev/null; then
167 echo copying "$input" to "$output"
168 cp -rT $input $output
169 fi
Jeff Gaston20c9bbc2018-03-07 15:25:43 -0500170}
171
Yigit Boyardb69c242018-04-30 09:09:05 -0700172function exportArtifact() {
Jeff Gaston20c9bbc2018-03-07 15:25:43 -0500173 echo exporting
174 announceCopy $stageRepo/com/android $destAndroidRepo/com/android
175 rm -rf $stageRepo/com/android
Jeff Gastonf6923e32018-04-18 16:03:33 -0400176
177 announceCopy $stageRepo/androidx $destAndroidRepo/androidx
178 rm -rf $stageRepo/androidx
179
Jeff Gaston20c9bbc2018-03-07 15:25:43 -0500180 announceCopy $stageRepo $destThirdPartyRepo
181 echo done exporting
182}
183
184
185function main() {
Jeff Gaston796227a2018-07-13 16:52:32 -0400186 downloadArtifacts "$@"
Jeff Gaston20c9bbc2018-03-07 15:25:43 -0500187 stageRepo
Yigit Boyardb69c242018-04-30 09:09:05 -0700188 exportArtifact
Jeff Gaston20c9bbc2018-03-07 15:25:43 -0500189}
190
191main "$@"