-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsortOutExternalJavaJarFiles.sh
executable file
·65 lines (51 loc) · 2.27 KB
/
sortOutExternalJavaJarFiles.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env bash
# Sorts out jar files that don't contain one of the given package names (e.g. external libraries) and moves them into the IGNORED_JARS_DIRECTORY.
# This script was initially generated by ChatGPT 4o and then manually adapted.
# Notice that this scripts is intended to be executed inside the jar files folder (e.g. the "artifacts" directory).
# Fail on any error ("-e" = exit on first error, "-o pipefail" exist on errors within piped commands)
set -o errexit -o pipefail
# Overrideable Defaults
IGNORED_JARS_DIRECTORY=${IGNORED_JARS_DIRECTORY:-"./../ignored-jars"} # Directory to move the filtered out .jar files to
# Check if there is at least one input parameter
if [ "$#" -lt 1 ]; then
echo "sortOutExternalJavaLarFiles: Error: Usage: $0 com.example.package1 com.example.package2 org.example.package3" >&2
exit 1
fi
# Get the package names to search for
packages="${*}"
echo "sortOutExternalJavaLarFiles: Moving all jar files into '${IGNORED_JARS_DIRECTORY}' that don't contains these packages: ${packages}"
# Directory containing the .jar files
search_dir="."
# Create the directory where the filtered out jars are moved to if it doesn't exist
if [ ! -d "${IGNORED_JARS_DIRECTORY}" ]; then
mkdir -p "${IGNORED_JARS_DIRECTORY}" || exit 1
fi
# Count the total number of .jar files
jar_files=$(find "$search_dir" -maxdepth 1 -name "*.jar")
total_files=$(echo "$jar_files" | wc -l | awk '{print $1}')
processed_files=0
# Function to update the progress bar
update_progress() {
processed_files=$((processed_files + 1))
progress=$((processed_files * 100 / total_files))
echo -ne "Progress: [$progress%] ($processed_files/$total_files)\r"
}
# Iterate over all .jar files in the search directory
for jar_file in "${search_dir}"/*.jar; do
# Skip if no .jar files are found
[ -e "${jar_file}" ] || continue
# Variable to mark if a package name is found
found_package=false
# Iterate over all package names
for package in ${packages}; do
if jar --list --file "${jar_file}" | grep --quiet "${package}"; then
found_package=true
break
fi
done
# If no package name is found, move the .jar file to the "others" directory
if [ "${found_package}" = false ]; then
mv -- "${jar_file}" "${IGNORED_JARS_DIRECTORY}" || exit 1
fi
update_progress
done