Skip to content

Commit 3adb8f7

Browse files
committed
Ugh
1 parent 960de6a commit 3adb8f7

File tree

1 file changed

+49
-88
lines changed

1 file changed

+49
-88
lines changed

scripts/build-android-app.sh

Lines changed: 49 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -116,123 +116,84 @@ ba_log "Generating Codename One application skeleton via codenameone-maven-plugi
116116

117117
APP_DIR="$WORK_DIR/$ARTIFACT_ID"
118118

119-
# --- Safe CN1 normalization using xmlstarlet (no regex on XML) ---
120-
119+
# --- Namespace-aware CN1 normalization (xmlstarlet) ---
121120
ROOT_POM="$APP_DIR/pom.xml"
121+
NS="mvn=http://maven.apache.org/POM/4.0.0"
122122

123-
# 0) Ensure xmlstarlet is available
124123
if ! command -v xmlstarlet >/dev/null 2>&1; then
125124
sudo apt-get update -y && sudo apt-get install -y xmlstarlet
126125
fi
127126

128-
# 1) Ensure codenameone.version property exists/updated (root pom)
129-
# If <properties> missing, create; if present, upsert codenameone.version
130-
if ! xmlstarlet sel -t -v "count(/project/properties)" "$ROOT_POM" | grep -qxE '[1-9]'; then
131-
# create <properties> right after <modelVersion>
132-
xmlstarlet ed -L \
133-
-s "/project" -t elem -n properties -v "" \
134-
"$ROOT_POM"
127+
# Helper to run xmlstarlet with Maven namespace
128+
x() { xmlstarlet ed -L -N "$NS" "$@"; }
129+
q() { xmlstarlet sel -N "$NS" "$@"; }
130+
131+
# 1) Ensure <properties><codenameone.version> exists/updated (root pom)
132+
if [ "$(q -t -v 'count(/mvn:project/mvn:properties)' "$ROOT_POM" 2>/dev/null || echo 0)" = "0" ]; then
133+
x -s "/mvn:project" -t elem -n properties -v "" "$ROOT_POM"
135134
fi
136-
if xmlstarlet sel -t -v "count(/project/properties/codenameone.version)" "$ROOT_POM" | grep -qxE '[1-9]'; then
137-
xmlstarlet ed -L \
138-
-u "/project/properties/codenameone.version" -v "$CN1_VERSION" \
139-
"$ROOT_POM"
135+
if [ "$(q -t -v 'count(/mvn:project/mvn:properties/mvn:codenameone.version)' "$ROOT_POM" 2>/dev/null || echo 0)" = "0" ]; then
136+
x -s "/mvn:project/mvn:properties" -t elem -n codenameone.version -v "$CN1_VERSION" "$ROOT_POM"
140137
else
141-
xmlstarlet ed -L \
142-
-s "/project/properties" -t elem -n codenameone.version -v "$CN1_VERSION" \
143-
"$ROOT_POM"
138+
x -u "/mvn:project/mvn:properties/mvn:codenameone.version" -v "$CN1_VERSION" "$ROOT_POM"
144139
fi
145140

146-
# 2) Parent must use a literal version (no properties allowed)
147-
# Update any pom’s parent if it is CN1 parent
141+
# 2) Parent must be a LITERAL version (no property allowed)
148142
while IFS= read -r -d '' P; do
149-
xmlstarlet ed -L \
150-
-u "/project[parent/groupId='com.codenameone' and parent/artifactId='codenameone-maven-parent']/parent/version" \
151-
-v "$CN1_VERSION" \
152-
"$P" || true
143+
x -u "/mvn:project[mvn:parent/mvn:groupId='com.codenameone' and mvn:parent/mvn:artifactId='codenameone-maven-parent']/mvn:parent/mvn:version" -v "$CN1_VERSION" "$P" || true
153144
done < <(find "$APP_DIR" -type f -name pom.xml -print0)
154145

155-
# 3) For com.codenameone deps/plugins, use the property ${codenameone.version}
156-
# (Plugins still need a version element present)
146+
# 3) Point com.codenameone deps/plugins to ${codenameone.version}
157147
while IFS= read -r -d '' P; do
158148
# Dependencies
159-
xmlstarlet ed -L \
160-
-u "/project//dependencies/dependency[groupId[starts-with(.,'com.codenameone')]]/version" \
161-
-v '${codenameone.version}' \
162-
"$P" 2>/dev/null || true
163-
164-
# Plugins: set version value to property where version element exists
165-
xmlstarlet ed -L \
166-
-u "/project//build//plugins/plugin[groupId[starts-with(.,'com.codenameone')]]/version" \
167-
-v '${codenameone.version}' \
168-
"$P" 2>/dev/null || true
169-
xmlstarlet ed -L \
170-
-u "/project//build//pluginManagement//plugins/plugin[groupId[starts-with(.,'com.codenameone')]]/version" \
171-
-v '${codenameone.version}' \
172-
"$P" 2>/dev/null || true
149+
x -u "/mvn:project//mvn:dependencies/mvn:dependency[starts-with(mvn:groupId,'com.codenameone')]/mvn:version" -v '${codenameone.version}' "$P" 2>/dev/null || true
150+
# Plugins (regular)
151+
x -u "/mvn:project//mvn:build/mvn:plugins/mvn:plugin[starts-with(mvn:groupId,'com.codenameone')]/mvn:version" -v '${codenameone.version}' "$P" 2>/dev/null || true
152+
# Plugins (pluginManagement)
153+
x -u "/mvn:project//mvn:build/mvn:pluginManagement/mvn:plugins/mvn:plugin[starts-with(mvn:groupId,'com.codenameone')]/mvn:version" -v '${codenameone.version}' "$P" 2>/dev/null || true
173154
done < <(find "$APP_DIR" -type f -name pom.xml -print0)
174155

175-
# 4) Ensure a version exists for common plugins that often omit it.
176-
# Prefer property if present; else pin a stable version.
177-
declare -A PLUGIN_FALLBACK=(
178-
[org.apache.maven.plugins:maven-compiler-plugin]='${maven-compiler-plugin.version:-3.11.0}'
179-
[org.apache.maven.plugins:maven-resources-plugin]='3.3.1'
180-
[org.apache.maven.plugins:maven-surefire-plugin]='3.2.5'
181-
[org.apache.maven.plugins:maven-failsafe-plugin]='3.2.5'
182-
[org.apache.maven.plugins:maven-jar-plugin]='3.3.0'
183-
[org.apache.maven.plugins:maven-clean-plugin]='3.3.2'
184-
[org.apache.maven.plugins:maven-deploy-plugin]='3.1.2'
185-
[org.apache.maven.plugins:maven-install-plugin]='3.1.2'
186-
[org.apache.maven.plugins:maven-assembly-plugin]='3.6.0'
187-
[org.apache.maven.plugins:maven-site-plugin]='4.0.0-M15'
156+
# 4) Ensure common Maven plugins have a version (Maven requires it even if parent not yet resolved)
157+
declare -A PIN=(
158+
[org.apache.maven.plugins:maven-compiler-plugin]=3.11.0
159+
[org.apache.maven.plugins:maven-resources-plugin]=3.3.1
160+
[org.apache.maven.plugins:maven-surefire-plugin]=3.2.5
161+
[org.apache.maven.plugins:maven-failsafe-plugin]=3.2.5
162+
[org.apache.maven.plugins:maven-jar-plugin]=3.3.0
163+
[org.apache.maven.plugins:maven-clean-plugin]=3.3.2
164+
[org.apache.maven.plugins:maven-deploy-plugin]=3.1.2
165+
[org.apache.maven.plugins:maven-install-plugin]=3.1.2
166+
[org.apache.maven.plugins:maven-assembly-plugin]=3.6.0
167+
[org.apache.maven.plugins:maven-site-plugin]=4.0.0-M15
188168
[com.codenameone:codenameone-maven-plugin]='${codenameone.version}'
189169
)
190170

191-
# Helper to resolve bash-like ${prop:-fallback} to either ${prop} or literal
192-
resolve_value() {
193-
local spec="$1"
194-
if [[ "$spec" == '${'maven-compiler-plugin.version':-'* ]]; then
195-
# if property exists in the POM, use ${maven-compiler-plugin.version}, otherwise fallback literal
196-
if xmlstarlet sel -t -v "count(/project/properties/maven-compiler-plugin.version)" "$ROOT_POM" | grep -qxE '[1-9]'; then
197-
echo '${maven-compiler-plugin.version}'
198-
else
199-
echo "${spec#*\:-}" | tr -d '}'
200-
fi
201-
else
202-
echo "$spec"
171+
add_version_if_missing() {
172+
local pom="$1" g="$2" a="$3" v="$4"
173+
# build/plugins
174+
if [ "$(q -t -v "count(/mvn:project/mvn:build/mvn:plugins/mvn:plugin[mvn:groupId='$g' and mvn:artifactId='$a']/mvn:version)" "$pom" 2>/dev/null || echo 0)" = "0" ] &&
175+
[ "$(q -t -v "count(/mvn:project/mvn:build/mvn:plugins/mvn:plugin[mvn:groupId='$g' and mvn:artifactId='$a'])" "$pom" 2>/dev/null || echo 0)" != "0" ]; then
176+
x -s "/mvn:project/mvn:build/mvn:plugins/mvn:plugin[mvn:groupId='$g' and mvn:artifactId='$a']" -t elem -n version -v "$v" "$pom" || true
177+
fi
178+
# pluginManagement/plugins
179+
if [ "$(q -t -v "count(/mvn:project/mvn:build/mvn:pluginManagement/mvn:plugins/mvn:plugin[mvn:groupId='$g' and mvn:artifactId='$a']/mvn:version)" "$pom" 2>/dev/null || echo 0)" = "0" ] &&
180+
[ "$(q -t -v "count(/mvn:project/mvn:build/mvn:pluginManagement/mvn:plugins/mvn:plugin[mvn:groupId='$g' and mvn:artifactId='$a'])" "$pom" 2>/dev/null || echo 0)" != "0" ]; then
181+
x -s "/mvn:project/mvn:build/mvn:pluginManagement/mvn:plugins/mvn:plugin[mvn:groupId='$g' and mvn:artifactId='$a']" -t elem -n version -v "$v" "$pom" || true
203182
fi
204183
}
205184

206185
while IFS= read -r -d '' P; do
207-
for ga in "${!PLUGIN_FALLBACK[@]}"; do
208-
g="${ga%%:*}"; a="${ga##*:}"
209-
val="$(resolve_value "${PLUGIN_FALLBACK[$ga]}")"
210-
211-
# build/plugins: add <version> if missing
212-
if [ "$(xmlstarlet sel -t -v "count(/project/build/plugins/plugin[groupId='$g' and artifactId='$a']/version)" "$P" 2>/dev/null || echo 0)" = "0" ] && \
213-
[ "$(xmlstarlet sel -t -v "count(/project/build/plugins/plugin[groupId='$g' and artifactId='$a'])" "$P" 2>/dev/null || echo 0)" != "0" ]; then
214-
xmlstarlet ed -L \
215-
-s "/project/build/plugins/plugin[groupId='$g' and artifactId='$a']" -t elem -n version -v "$val" \
216-
"$P" || true
217-
fi
218-
219-
# pluginManagement/plugins: add <version> if missing
220-
if [ "$(xmlstarlet sel -t -v "count(/project/build/pluginManagement/plugins/plugin[groupId='$g' and artifactId='$a']/version)" "$P" 2>/dev/null || echo 0)" = "0" ] && \
221-
[ "$(xmlstarlet sel -t -v "count(/project/build/pluginManagement/plugins/plugin[groupId='$g' and artifactId='$a'])" "$P" 2>/dev/null || echo 0)" != "0" ]; then
222-
xmlstarlet ed -L \
223-
-s "/project/build/pluginManagement/plugins/plugin[groupId='$g' and artifactId='$a']" -t elem -n version -v "$val" \
224-
"$P" || true
225-
fi
186+
for ga in "${!PIN[@]}"; do
187+
add_version_if_missing "$P" "${ga%%:*}" "${ga##*:}" "${PIN[$ga]}"
226188
done
227189
done < <(find "$APP_DIR" -type f -name pom.xml -print0)
228190

229-
# 5) Make sure CN1 resolves everywhere even if some modules didn’t get rewritten
191+
# 5) Build with the property set so any lingering refs resolve to the local snapshot
230192
EXTRA_MVN_ARGS+=("-Dcodenameone.version=${CN1_VERSION}")
231193

232-
# Optional: quick, non-fatal dump around the two sections that used to fail
233-
xmlstarlet sel -t -c "/project/build/plugins" -n "$ROOT_POM" || true
234-
xmlstarlet sel -t -c "/project/build/pluginManagement/plugins" -n "$ROOT_POM" || true
235-
nl -ba "$ROOT_POM" | sed -n '1,140p' || true
194+
# (Optional) quick non-fatal checks
195+
xmlstarlet sel -N "$NS" -t -v "/mvn:project/mvn:properties/mvn:codenameone.version" -n "$ROOT_POM" || true
196+
xmlstarlet sel -N "$NS" -t -c "/mvn:project/mvn:build/mvn:plugins" -n "$ROOT_POM" | head -n 60 || true
236197

237198

238199

0 commit comments

Comments
 (0)