aar添加到内建倉庫使用腳本 publishlocalmaven.sh:
#!/bin/bash
##############################使用方式##############################
# 需安裝 shyaml https://github.com/0k/shyaml
HELP="
用法:
1. 将要添加到倉庫的 aar 包放在 lib_aar 檔案夾下;
2. 配置 config.yaml, 格式:
groupId: com.thirdparty.aar
artifacts:
-
file: ambilWarna.aar
artifactId: ambilWarna
version: 1.0.0
3. 運作 publishlocalmaven.sh 腳本:
./publishlocalmaven.sh
4. 添加成功後将原始 aar 檔案删除;
5. 需依賴 aar 的子產品引用:
implementation 'com.thirdparty.aar:ambilWarna:1.0.0'
"
CONFIG=config.yaml # 配置檔案
if [ -n $CONFIG ]; then
current_dir=$(cd `dirname $0`;pwd) # 腳本所在目錄
config_file=${current_dir}/${CONFIG} # 配置檔案全路徑
groupId=$(cat $config_file | shyaml get-value groupId)
echo groupId: $groupId
j=$(cat $config_file | shyaml get-length artifacts)
for ((i=0; i<=$j-1; i++))
do
file=$current_dir/$(cat $config_file | shyaml get-value artifacts.$i.file)
artifactId=$(cat $config_file | shyaml get-value artifacts.$i.artifactId)
version=$(cat $config_file | shyaml get-value artifacts.$i.version)
if [ -f $file ];then
mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file \
-Dfile=$file \
-DgroupId=$groupId -DartifactId=$artifactId \
-Dversion=$version -Dpackaging=aar \
-DlocalRepositoryPath=$current_dir
else
echo "檔案不存在!!! -> [$file]"
fi
done
fi
配置檔案 config.yaml:
groupId: com.thirdparty.aar
artifacts:
-
file: ambilWarna.aar
artifactId: ambilWarna
version: 1.0.0
使用方式
1. 主工程的 build.gradle 中添加項目内建倉庫位置
repositories {
maven {
// 把 lib_aar 當成本地倉庫存放離線使用的 aar
url("file://${project.rootDir.absolutePath}/lib_aar")
}
}
2. 将要添加到倉庫的 aar 包(本地依賴包) 放在 lib_aar 檔案夾下;
project
|__ app
|__ lib_aar # 内建倉庫位置
|__ com.thirdparty.aar # 添加到内建倉庫的倉庫 aar 依賴
|__ ambilWarna
|__ 1.0.0
|__ ambilWarna-1.0.0.aar
|__ ambilWarna-1.0.0.pom
|__ maven-metadata-local.xml
|__ config.yaml # 添加本地依賴 aar 到内建倉庫的倉庫的腳本使用的配置檔案
|__ publishlocalmaven.sh # 添加本地依賴 aar 到内建倉庫的倉庫的腳本
|__ ambilWarna.aar # 要添加到内建倉庫的本地依賴 aar, 添加完成後删除
|__ build.gradle
3. 配置 config.yaml, 格式:
groupId: com.thirdparty.aar # 添加依賴的 groupId, 不需要修改
artifacts:
-
file: ambilWarna.aar # 要添加的本地依賴 aar 檔案名, 檔案要放在腳本目前目錄下
artifactId: ambilWarna # 要添加的本地依賴 aar 添加後的 artifactId
version: 1.0.0 # 要添加的本地依賴 aar 添加後的 version
4. 運作 publishlocalmaven.sh 腳本:
安裝 shyaml
https://github.com/0k/shyamlcd lib_aar
chmod a+x publishlocalmaven.sh
./publishlocalmaven.sh
5. 添加成功後将原始 aar 檔案删除;
6. 需依賴 aar 的子產品 build.gradle 中引用内建倉庫的 aar:
dependencies {
implementation 'com.thirdparty.aar:ambilWarna:1.0.0'
}