-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtarget-platform-analysis.sh
More file actions
executable file
·169 lines (135 loc) · 5.61 KB
/
Copy pathtarget-platform-analysis.sh
File metadata and controls
executable file
·169 lines (135 loc) · 5.61 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env bash
# Compare Eclipse target file entries with Maven dependency tree output.
# Finds target file entries that are NOT present in the Maven dependencies.
# Works on Linux and Windows (Git Bash, WSL, Cygwin)
set -e
# Color codes for output (disable if not supported)
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to display usage
usage() {
echo "Usage: $0 <target-file.target> [maven-tree-output.txt]"
echo ""
echo "If maven-tree-output.txt is not provided, it will be generated automatically."
echo ""
echo "Example:"
echo " $0 my-target.target"
echo " $0 my-target.target existing-maven-tree.txt"
exit 1
}
# Check if target file is provided
if [ $# -lt 1 ]; then
usage
fi
TARGET_FILE="$1"
MAVEN_FILE="${2:-maven-tree.txt}"
GENERATE_MAVEN=false
# Check if target file exists
if [ ! -f "$TARGET_FILE" ]; then
echo -e "${RED}Error: Target file '$TARGET_FILE' not found.${NC}"
exit 1
fi
# Check if Maven output needs to be generated
if [ $# -lt 2 ]; then
echo -e "${YELLOW}Maven dependency tree output not provided. Generating...${NC}"
GENERATE_MAVEN=true
fi
# Generate Maven dependency tree if needed
if [ "$GENERATE_MAVEN" = true ]; then
if ! command -v mvn &> /dev/null; then
echo -e "${RED}Error: Maven (mvn) not found in PATH.${NC}"
exit 1
fi
echo "Running: mvn dependency:tree -DoutputFile=$MAVEN_FILE"
mvn dependency:tree -DoutputFile="$MAVEN_FILE"
if [ ! -f "$MAVEN_FILE" ]; then
echo -e "${RED}Error: Failed to generate Maven dependency tree.${NC}"
exit 1
fi
echo -e "${GREEN}Maven dependency tree generated: $MAVEN_FILE${NC}"
echo ""
fi
# Check if Maven file exists
if [ ! -f "$MAVEN_FILE" ]; then
echo -e "${RED}Error: Maven output file '$MAVEN_FILE' not found.${NC}"
exit 1
fi
# Create temporary files
TEMP_DIR=$(mktemp -d)
trap "rm -rf $TEMP_DIR" EXIT
TARGET_DEPS="$TEMP_DIR/target_deps.txt"
MAVEN_DEPS="$TEMP_DIR/maven_deps.txt"
MISSING_DEPS="$TEMP_DIR/missing_deps.txt"
# Parse target file and extract dependencies
echo -e "${BLUE}Parsing target file: $TARGET_FILE${NC}"
# Extract unit entries (format: id and version attributes)
grep -oP '(?<=<unit id=")[^"]+' "$TARGET_FILE" 2>/dev/null | while read -r unit_id; do
# Get the corresponding version
version=$(grep -A 0 "id=\"$unit_id\"" "$TARGET_FILE" | grep -oP '(?<=version=")[^"]+' | head -1)
# Convert unit id to Maven coordinates if it contains a dot
if [[ "$unit_id" == *.* ]]; then
# Split on last dot: group.id.artifact -> group.id:artifact
group_id="${unit_id%.*}"
artifact_id="${unit_id##*.}"
echo "${group_id}:${artifact_id}:${version}"
fi
done > "$TARGET_DEPS"
# Also look for explicit Maven-style entries in target file
grep -oP '<groupId>\K[^<]+' "$TARGET_FILE" 2>/dev/null | paste -d: - <(grep -oP '<artifactId>\K[^<]+' "$TARGET_FILE" 2>/dev/null) <(grep -oP '<version>\K[^<]+' "$TARGET_FILE" 2>/dev/null) >> "$TARGET_DEPS" || true
# Remove duplicates and sort
sort -u "$TARGET_DEPS" -o "$TARGET_DEPS"
target_count=$(wc -l < "$TARGET_DEPS")
echo -e "${GREEN}Found $target_count unique dependencies in target file${NC}"
# Parse Maven dependency tree output
echo -e "${BLUE}Parsing Maven dependency tree: $MAVEN_FILE${NC}"
# Extract Maven coordinates (format: groupId:artifactId:packaging:version:scope)
grep -oP '[a-zA-Z0-9._-]+:[a-zA-Z0-9._-]+:[a-zA-Z0-9._-]+:[a-zA-Z0-9._-]+' "$MAVEN_FILE" 2>/dev/null | while IFS=':' read -r group_id artifact_id packaging version scope; do
echo "${group_id}:${artifact_id}:${version}"
done | sort -u > "$MAVEN_DEPS"
maven_count=$(wc -l < "$MAVEN_DEPS")
echo -e "${GREEN}Found $maven_count unique dependencies in Maven tree${NC}"
# Function to normalize version (remove SNAPSHOT, qualifiers, etc.)
normalize_version() {
echo "$1" | sed -E 's/-SNAPSHOT.*//;s/\.v[0-9]+.*//;s/-[a-zA-Z]+.*//'
}
# Find missing dependencies
echo -e "\n${BLUE}Comparing dependencies...${NC}\n"
> "$MISSING_DEPS"
while IFS=':' read -r target_group target_artifact target_version; do
[ -z "$target_group" ] && continue
found=false
normalized_target_version=$(normalize_version "$target_version")
# Check if the exact artifact exists in Maven deps
while IFS=':' read -r maven_group maven_artifact maven_version; do
if [ "$target_group" = "$maven_group" ] && [ "$target_artifact" = "$maven_artifact" ]; then
# Found the artifact, now check version
normalized_maven_version=$(normalize_version "$maven_version")
if [ "$target_version" = "$maven_version" ] || \
[ "$normalized_target_version" = "$normalized_maven_version" ]; then
found=true
break
fi
fi
done < "$MAVEN_DEPS"
if [ "$found" = false ]; then
echo "${target_group}:${target_artifact}:${target_version}" >> "$MISSING_DEPS"
fi
done < "$TARGET_DEPS"
# Display results
echo "================================================================================"
echo "Dependencies in TARGET file NOT found in Maven dependency tree:"
echo "================================================================================"
if [ -s "$MISSING_DEPS" ]; then
cat "$MISSING_DEPS" | while IFS=':' read -r group artifact version; do
echo -e " ${YELLOW}${group}:${artifact}:${version}${NC}"
done
missing_count=$(wc -l < "$MISSING_DEPS")
echo ""
echo -e "${RED}Total missing: $missing_count${NC}"
else
echo -e " ${GREEN}None - all target dependencies are present in Maven tree!${NC}"
fi
echo ""