一、修改Maven配置文件
为支持混合加载远程与本地包,需在maven配置文件中分别指定远程和本地地址:
- 远程:
https://maven.aliyun.com/repository/central
- 本地:
file://${HOME}/.m2/repository_backup
vim ~/.m2/settings.xml
<?xml version="1.0" encoding="UTF-8"?>
<settings>
<profiles>
<profile>
<id>m2-mix-remote-local</id>
<repositories>
<repository>
<id>m2-remote</id>
<url>https://maven.aliyun.com/repository/central</url>
</repository>
<repository>
<id>m2-local</id>
<url>file://${HOME}/.m2/repository_backup</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>m2-remote</id>
<url>https://maven.aliyun.com/repository/central</url>
</pluginRepository>
<pluginRepository>
<id>m2-local</id>
<url>file://${HOME}/.m2/repository_backup</url>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>m2-mix-remote-local</activeProfile>
</activeProfiles>
</settings>
二、收集本地依赖包
(1)重命名原来的maven本地仓库,后续将从该目录下提取无法从公网下载的依赖包。
mv ~/.m2/repository ~/.m2/repository_origin
(2)进入代码根目录,获取缺失的依赖包信息。
# 二选一
mvn dependency:go-offline
mvn compile
(3)将上述依赖包保存到dependencies.txt
文件中:
vim dependencies.txt
com.openjaw.certifiedschemas:openjaw-certifiedschemas:jar:7.24.0-RL-SNAPSHOT
com.oracle:ojdbc6:jar:11.2.0.3.0
(4)编写并执行收集本地依赖包脚本:
vim collect_repo.sh
#!/bin/bash
repo_origin="$HOME/.m2/repository_origin"
repo_backup="$HOME/.m2/repository_backup"
repo_current="$HOME/.m2/repository"
while IFS=: read -r group artifact ext version; do
real_group=$(echo "$group" | tr '.' '/')
dir="$real_group/$artifact/$version"
pom_file="$artifact-$version.pom"
jar_file="$artifact-$version.jar"
zip_file="$artifact-$version.zip"
echo "-> $repo_backup/$dir"
mkdir -p "$repo_backup/$dir"
if [ $ext = 'pom' ]; then
cp "$repo_origin/$dir/$pom_file" "$repo_backup/$dir/"
sha1sum "$repo_backup/$dir/$pom_file" > "$repo_backup/$dir/$pom_file.sha1"
fi
if [ $ext = 'jar' ]; then
cp "$repo_origin/$dir/$pom_file" "$repo_backup/$dir/"
cp "$repo_origin/$dir/$jar_file" "$repo_backup/$dir/"
sha1sum "$repo_backup/$dir/$pom_file" > "$repo_backup/$dir/$pom_file.sha1"
sha1sum "$repo_backup/$dir/$jar_file" > "$repo_backup/$dir/$jar_file.sha1"
fi
if [ $ext = 'zip' ]; then
cp "$repo_origin/$dir/$pom_file" "$repo_backup/$dir/"
cp "$repo_origin/$dir/$zip_file" "$repo_backup/$dir/"
sha1sum "$repo_backup/$dir/$pom_file" > "$repo_backup/$dir/$pom_file.sha1"
sha1sum "$repo_backup/$dir/$zip_file" > "$repo_backup/$dir/$zip_file.sha1"
fi
rm -rf "$repo_current/$dir"
done < dependencies.txt
./collect_repo.sh
(4)重复上述步骤2-4,直至所有缺失的依赖包均收集成功。
三、打包本地依赖包
经过上述流程,所有的本地依赖包均被收集到了$HOME/.m2/repository_backup
目录,可使用tar与gpg对其进行压缩与解压。
(1)压缩
vim encrypt.sh
#!/bin/bash
tar cJvhf - repository_backup | gpg -co repository_backup.txz.gpg
(2)解压
vim decrypt.sh
#!/bin/bash
gpg -d repository_backup.txz.gpg | tar -xJvf -