Skip to content

Commit 26a49aa

Browse files
committed
Fixing replace logic for pom files
1 parent 8919eff commit 26a49aa

File tree

1 file changed

+59
-24
lines changed

1 file changed

+59
-24
lines changed

scripts/build-android-app.sh

Lines changed: 59 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,65 @@ ba_log "Generating Codename One application skeleton via codenameone-maven-plugi
115115
)
116116

117117
APP_DIR="$WORK_DIR/$ARTIFACT_ID"
118+
119+
# --- Normalize Codename One versions post-generation (no versions-maven-plugin) ---
120+
121+
ROOT_POM="$APP_DIR/pom.xml"
122+
123+
# 1) Ensure a codenameone.version property exists in the root pom
124+
ensure_property() {
125+
local pom="$1" name="$2" value="$3"
126+
127+
if ! grep -q "<properties>" "$pom"; then
128+
# Insert a <properties> block near the top-level <project> block
129+
awk -v v="$value" -v n="$name" '
130+
BEGIN{inserted=0}
131+
/<project[^>]*>/ && inserted==0 {
132+
print;
133+
print " <properties>";
134+
print " <" n ">" v "</" n ">";
135+
print " </properties>";
136+
inserted=1; next
137+
}
138+
{print}
139+
' "$pom" > "$pom.tmp" && mv "$pom.tmp" "$pom"
140+
elif ! grep -q "<${name}>" "$pom"; then
141+
# Add the property inside existing <properties>
142+
awk -v v="$value" -v n="$name" '
143+
/<properties>/ && !done {
144+
print;
145+
print " <" n ">" v "</" n ">";
146+
done=1; next
147+
}
148+
{print}
149+
' "$pom" > "$pom.tmp" && mv "$pom.tmp" "$pom"
150+
else
151+
# Update existing property value
152+
perl -0777 -pe "s|(<${name}>)[^<]+(</${name}>)|\$1${value}\$2|s" -i "$pom"
153+
fi
154+
}
155+
156+
ensure_property "$ROOT_POM" "codenameone.version" "$CN1_VERSION"
157+
158+
# 2) Rewrite com.codenameone dependency versions to ${codenameone.version}
159+
rewrite_versions() {
160+
local pom="$1"
161+
# Dependencies
162+
perl -0777 -pe 's!(<dependency>\s*<groupId>com\.codenameone[^<]*</groupId>\s*<artifactId>[^<]+</artifactId>\s*<version>)[^<]+(</version>)!${1}${codenameone.version}${2}!sg' -i "$pom"
163+
# Plugins
164+
perl -0777 -pe 's!(<plugin>\s*<groupId>com\.codenameone[^<]*</groupId>\s*<artifactId>[^<]+</artifactId>\s*<version>)[^<]+(</version>)!${1}${codenameone.version}${2}!sg' -i "$pom"
165+
}
166+
167+
export codenameone_version_placeholder='${codenameone.version}'
168+
169+
# Walk all poms under the generated app (skip non-poms like cn1libs/)
170+
while IFS= read -r -d '' P; do
171+
rewrite_versions "$P"
172+
done < <(find "$APP_DIR" -type f -name pom.xml -print0)
173+
174+
# 3) From now on, build with the property explicitly set too (helps any missed spots)
175+
EXTRA_MVN_ARGS+=("-Dcodenameone.version=${CN1_VERSION}")
176+
118177
[ -d "$APP_DIR" ] || { ba_log "Failed to create Codename One application project" >&2; exit 1; }
119178
[ -f "$APP_DIR/build.sh" ] && chmod +x "$APP_DIR/build.sh"
120179

@@ -165,30 +224,6 @@ tail -c1 "$SETTINGS_FILE" | read -r _ || echo >> "$SETTINGS_FILE"
165224

166225
# --- Normalize Codename One versions (use Maven Versions Plugin) ---
167226
ba_log "Normalizing Codename One Maven coordinates to $CN1_VERSION"
168-
# Set property codenameone.version where present
169-
xvfb-run -a "${MAVEN_CMD[@]}" -q -f "$APP_DIR/pom.xml" \
170-
versions:set-property \
171-
-Dproperty=codenameone.version \
172-
-DnewVersion="$CN1_VERSION" \
173-
-DgenerateBackupPoms=false \
174-
-DprocessAllModules=true || true
175-
176-
# Force dependencies on com.codenameone:* to the detected version
177-
xvfb-run -a "${MAVEN_CMD[@]}" -q -f "$APP_DIR/pom.xml" \
178-
versions:use-dep-version \
179-
-Dincludes='com.codenameone:*' \
180-
-DdepVersion="$CN1_VERSION" \
181-
-DforceVersion=true \
182-
-DgenerateBackupPoms=false \
183-
-DprocessAllModules=true || true
184-
185-
# Force plugins on com.codenameone:* to the detected version
186-
xvfb-run -a "${MAVEN_CMD[@]}" -q -f "$APP_DIR/pom.xml" \
187-
versions:use-plugin-version \
188-
-Dincludes='com.codenameone:*' \
189-
-DpluginVersion="$CN1_VERSION" \
190-
-DgenerateBackupPoms=false \
191-
-DprocessAllModules=true || true
192227

193228
# --- Build Android gradle project ---
194229
ba_log "Building Android gradle project using Codename One port"

0 commit comments

Comments
 (0)