【发布时间】:2019-04-16 21:08:00
【问题描述】:
使用终端,是否可以清理和安装 POM 项目中在 git merge/pull 之后发生更改的项目,换句话说,自上次全新安装以来的更改?
编辑
感谢 VonC 的回答,我已经完成了一个符合我要求的脚本。详情见我的回答。
【问题讨论】:
使用终端,是否可以清理和安装 POM 项目中在 git merge/pull 之后发生更改的项目,换句话说,自上次全新安装以来的更改?
编辑
感谢 VonC 的回答,我已经完成了一个符合我要求的脚本。详情见我的回答。
【问题讨论】:
既然可以detect the list of pom.xml changes between two commits:
git diff --name-status <commit1> <commit2>
(另见“Detail change after Git pull”)
,您可以编写一个脚本,为每个模块执行一个mvn clean install。
【讨论】:
感谢 VonC 的 answer,我完成了一个符合我要求的脚本。
该项目是一个 POM 项目,其中包含包含应用程序主要项目的子 POM 项目。通过让当前工作分支调用git diff --name-status HEAD@{1} <current_branch>,我达到了预期的结果。
这得到了项目中已更改文件的列表,然后我将其拆分为一个数组。不幸的是,我无法让拆分正常工作,因此数组被组织为更改类型,后跟文件路径。
然后我检查了字符串大小,跳过了单个字符串。下一步是将字符串拆分为一个数组,由/ 分隔。如果根路径检查它是否存在于数组中,如果不存在则添加。
最后,遍历根路径数组并执行 maven 调用。
################################################################################
#
# License:.....GNU General Public License v3.0
# Author:......CodeMonkey
# Date:........14 November 2018
# Title:.......GitMavenCleanInstall.sh
# Description: This script is designed to cd to a set Maven POM Project,
# perform a git remote update and pull, and clean install the changed
# files projects.
# Notice:......The project structure this script was originally set to target
# is structured as a Maven POM Project that contains several sub-POM Projects.
# The sub-POM Projects contain Maven Java Application projects. The targets
# should be easy to change, and allow for others to target other structures.
#
################################################################################
#
# Change History: N/A
#
################################################################################
#!/bin/bash
#Function to check if array has element
containsElement () {
local e match="$1"
shift
for e; do [[ "$e" == "$match" ]] && return 0; done
return 1
}
#Navigate to the POM Project
cd PATH/TO/POM/PROJECT
#Remote update
git remote update -p
#Pull
git pull
#Get the current working branch
CURRENT_BRANCH="$(git branch | sed -n -e 's/^\* \(.*\)/\1/p')"
#Get the output of the command git diff
GIT_DIFF_OUTPUT="$(git diff --name-status HEAD@{1} ${CURRENT_BRANCH})"
#Split the diff output into an array
read =a GIT_DIFF_OUTPUT_ARY <<< $GIT_DIF_OUTPUT
#Declare empty array for root path
declare -a GIT_DIFF_OUTPUT_ARY_ROOT_PATH=()
FORWARD='/'
#Loop diff output array
for i in "$GIT_DIFF_OUTPUT_ARY[@]}"
do
#Check that the string is not 1 Character
if [[ "$(echo -n $1 | wc -m)" != 1 ]]
then
#Split the file path by /
IFS='/' read -ra SPLIT <<< $i
#Concatenate first path + / + second path
path=${SPLIT[0]}$FORWARD${SPLIT[1]}
#Call function to see if it already exists in the root path array
containsElement "$path" "${GIT_DIFF_OUTPUT_ARY_ROOT_PATH[@]}"
if [[ $? != 0 ]]
then
#Add the path since it was not found
GIT_DIFF_OUTPUT_ARY_ROOT_PATH+=($path)
fi
fi
done
#Loop root path array
for val in ${GIT_DIFF_OUTPUT_ARY_ROOT_PATH[@]}
do
#CD into root path
cd $val
#Maven call to clean install
mvn -DskipTests=true --errors -T 8 -e clean install
#CD back up before next project
cd ../../
done
【讨论】: