Skip to content

Commit f8d1467

Browse files
committed
Yet another attempt
1 parent 8f79013 commit f8d1467

File tree

1 file changed

+97
-69
lines changed

1 file changed

+97
-69
lines changed

scripts/build-android-app.sh

Lines changed: 97 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -116,94 +116,122 @@ ba_log "Generating Codename One application skeleton via codenameone-maven-plugi
116116

117117
APP_DIR="$WORK_DIR/$ARTIFACT_ID"
118118

119-
# --- Robust CN1 version normalization & plugin version injection ---
119+
# --- Safe CN1 normalization using xmlstarlet (no regex on XML) ---
120120

121121
ROOT_POM="$APP_DIR/pom.xml"
122122

123-
# 0) Ensure property exists for deps/plugins (not used for parent)
124-
ensure_property() {
125-
local pom="$1" name="$2" value="$3"
126-
if ! grep -q "<properties>" "$pom"; then
127-
awk -v v="$value" -v n="$name" '
128-
BEGIN{ins=0}
129-
/<project[^>]*>/ && !ins { print; print " <properties>\n <" n ">" v "</" n ">\n </properties>"; ins=1; next }
130-
{print}
131-
' "$pom" > "$pom.tmp" && mv "$pom.tmp" "$pom"
132-
elif ! grep -q "<${name}>" "$pom"; then
133-
awk -v v="$value" -v n="$name" '
134-
/<properties>/ && !done { print; print " <" n ">" v "</" n ">"; done=1; next }
135-
{print}
136-
' "$pom" > "$pom.tmp" && mv "$pom.tmp" "$pom"
137-
else
138-
perl -0777 -pe "s|(<${name}>)[^<]+(</${name}>)|\$1${value}\$2|s" -i "$pom"
139-
fi
140-
}
141-
ensure_property "$ROOT_POM" "codenameone.version" "$CN1_VERSION"
123+
# 0) Ensure xmlstarlet is available
124+
if ! command -v xmlstarlet >/dev/null 2>&1; then
125+
sudo apt-get update -y && sudo apt-get install -y xmlstarlet
126+
fi
142127

143-
# 1) Parent must be literal version (Maven cannot resolve a property here)
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"
135+
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"
140+
else
141+
xmlstarlet ed -L \
142+
-s "/project/properties" -t elem -n codenameone.version -v "$CN1_VERSION" \
143+
"$ROOT_POM"
144+
fi
145+
146+
# 2) Parent must use a literal version (no properties allowed)
147+
# Update any pom’s parent if it is CN1 parent
144148
while IFS= read -r -d '' P; do
145-
perl -0777 -pe 's!(<parent>\s*<groupId>com\.codenameone</groupId>\s*<artifactId>codenameone-maven-parent</artifactId>\s*<version>)[^<]+(</version>)!$1'"$CN1_VERSION"'$2!s' -i "$P"
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
146153
done < <(find "$APP_DIR" -type f -name pom.xml -print0)
147154

148-
# 2) CN1 deps/plugins -> property
155+
# 3) For com.codenameone deps/plugins, use the property ${codenameone.version}
156+
# (Plugins still need a version element present)
149157
while IFS= read -r -d '' P; do
150-
perl -0777 -pe 's!(<dependency>\s*<groupId>com\.codenameone[^<]*</groupId>\s*<artifactId>[^<]+</artifactId>\s*<version>)[^<]+(</version>)!${1}${codenameone.version}${2}!sg' -i "$P"
151-
perl -0777 -pe 's!(<plugin>\s*<groupId>com\.codenameone[^<]*</groupId>\s*<artifactId>[^<]+</artifactId>\s*<version>)[^<]+(</version>)!${1}${codenameone.version}${2}!sg' -i "$P"
158+
# 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
152173
done < <(find "$APP_DIR" -type f -name pom.xml -print0)
153174

154-
# 3) Inject versions for plugins that have none (or update existing ones) — no lookaheads
155-
156-
declare -A PLUG_VERSIONS=(
157-
[org.apache.maven.plugins:maven-compiler-plugin]=3.11.0
158-
[org.apache.maven.plugins:maven-surefire-plugin]=3.2.5
159-
[org.apache.maven.plugins:maven-failsafe-plugin]=3.2.5
160-
[org.apache.maven.plugins:maven-jar-plugin]=3.3.0
161-
[org.apache.maven.plugins:maven-resources-plugin]=3.3.1
162-
[org.apache.maven.plugins:maven-install-plugin]=3.1.2
163-
[org.apache.maven.plugins:maven-deploy-plugin]=3.1.2
164-
[org.apache.maven.plugins:maven-clean-plugin]=3.3.2
165-
[org.apache.maven.plugins:maven-site-plugin]=4.0.0-M15
166-
[org.apache.maven.plugins:maven-assembly-plugin]=3.6.0
167-
[com.codenameone:codenameone-maven-plugin]="$CN1_VERSION"
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'
188+
[com.codenameone:codenameone-maven-plugin]='${codenameone.version}'
168189
)
169190

170-
inject_plugin_versions_file() {
171-
local pom="$1"
172-
for ga in "${!PLUG_VERSIONS[@]}"; do
173-
local g="${ga%%:*}" a="${ga##*:}" v="${PLUG_VERSIONS[$ga]}"
174-
175-
# Pass 1: update existing <version>...</version> for this plugin
176-
perl -0777 -i -pe \
177-
"s!(<plugin>\\s*<groupId>\\Q$g\\E</groupId>\\s*<artifactId>\\Q$a\\E</artifactId>\\s*<version>)[^<]+(</version>)!\$1$v\$2!sg" \
178-
"$pom"
179-
180-
# Pass 2: if there is NO version yet, insert it right after </artifactId>
181-
perl -0777 -i -pe \
182-
"s!(<plugin>\\s*<groupId>\\Q$g\\E</groupId>\\s*<artifactId>\\Q$a\\E</artifactId>\\s*)(?!.*?<version>)(?:(?:(?!</plugin>).)*</plugin>)!${1}<version>$v</version>\n!sg" \
183-
"$pom" 2>/dev/null || true
184-
185-
# The line above still uses a tiny lookahead; if you want *zero* lookaheads at all,
186-
# use this alternative pure two-step approach (slower, but bulletproof):
187-
# if ! grep -zq "<groupId>$g</groupId>.*<artifactId>$a</artifactId>.*<version>" "$pom"; then
188-
# perl -0777 -i -pe \
189-
# "s!(<plugin>\\s*<groupId>\\Q$g\\E</groupId>\\s*<artifactId>\\Q$a\\E</artifactId>\\s*)!\\1<version>$v</version>\n!s" \
190-
# "$pom"
191-
# fi
192-
done
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"
203+
fi
193204
}
194205

195206
while IFS= read -r -d '' P; do
196-
inject_plugin_versions_file "$P"
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
226+
done
197227
done < <(find "$APP_DIR" -type f -name pom.xml -print0)
198228

199-
# 4) Keep this so any remaining CN1 refs resolve to your local snapshot
229+
# 5) Make sure CN1 resolves everywhere even if some modules didn’t get rewritten
200230
EXTRA_MVN_ARGS+=("-Dcodenameone.version=${CN1_VERSION}")
201231

202-
# 5) Non-fatal debug (won’t fail the build)
203-
grep -n -A3 -B3 '<parent>' "$ROOT_POM" || true
204-
grep -n 'artifactId>maven-compiler-plugin' -n -A2 -B2 "$ROOT_POM" || true
205-
206-
nl -ba "$ROOT_POM" | sed -n '1,140p' || true
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
207235

208236

209237

0 commit comments

Comments
 (0)