Skip to content

Commit de5827e

Browse files
jeffjensenclaude
andcommitted
fix(spring): Resolve archetype working dir from user.dir when PWD is unusable
The post-generate script took the shell working directory from the PWD environment variable to resolve a relative -DprojectDirectory. Under Git Bash on Windows, PWD is a Unix-style path (e.g. /c/Users/...) that java.io.File mis-resolves to a bogus drive-relative path, so the generated tests were copied outside the project and a stray demo project (its pom.xml included) could be left behind. Trust PWD only when it names an existing directory on this platform; otherwise fall back to user.dir, the real JVM working directory. Also correct the README and archetype docs to use a cross-platform relative scratch directory (target/audit-scratch) instead of a Unix /tmp path that is mis-resolved on Windows. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UkRk16pW4VEM3E6vrDoG5y
1 parent 3929562 commit de5827e

3 files changed

Lines changed: 11 additions & 9 deletions

File tree

README.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ mvn archetype:generate -DinteractiveMode=false \
2020
-DartifactId=demo-audit-tests \
2121
-Dpackage=com.example.demo \
2222
-DschemaPropertyName=database.datasource.schema-name \
23-
-DoutputDirectory=/tmp/audit-scratch \
23+
-DoutputDirectory=target/audit-scratch \
2424
-DprojectDirectory=.
2525
----
2626

archetype/src/main/resources/META-INF/archetype-post-generate.groovy

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ if (!targetDirProp) {
1313
targetDirProp = System.getProperty('projectDirectory')
1414
}
1515

16-
// A relative projectDirectory resolves against the shell's working directory. Prefer the PWD
17-
// environment variable: the shell exports it and, unlike user.dir, it cannot be mutated by a
18-
// System.setProperty call inside the running JVM. Fall back to user.dir when PWD is absent (e.g. on
19-
// Windows). Tests inject _pwd through the Groovy Binding to stand in for PWD.
20-
def pwdOverride = binding.hasVariable('_pwd') ? (String) binding.getVariable('_pwd') : null
21-
def workingDir = pwdOverride ?: System.getenv('PWD') ?: System.getProperty('user.dir')
16+
// A relative projectDirectory resolves against the shell's working directory, taken from the PWD
17+
// environment variable (the shell exports it and, unlike user.dir, it cannot be mutated by a
18+
// System.setProperty call inside the running JVM; tests inject _pwd through the Groovy Binding to
19+
// stand in for it). Under Git Bash on Windows, though, PWD is a Unix-style path (e.g. /c/Users/...)
20+
// that java.io.File resolves to a bogus drive-relative path, so trust PWD only when it names an
21+
// existing directory on this platform; otherwise fall back to user.dir (the real JVM working dir).
22+
def pwd = binding.hasVariable('_pwd') ? (String) binding.getVariable('_pwd') : System.getenv('PWD')
23+
def workingDir = (pwd && new File(pwd).isDirectory()) ? pwd : System.getProperty('user.dir')
2224

2325
def resolveDestRoot = { prop ->
2426
if (!prop) {

integration/src/site/asciidoc/archetype.adoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ mvn archetype:generate -DinteractiveMode=false \
5353

5454
== Generate tests only (for an existing project)
5555

56-
*Point `-DoutputDirectory` at a pom-less scratch directory* (e.g. `/tmp/audit-scratch`) — the plugin refuses
56+
*Point `-DoutputDirectory` at a pom-less scratch directory* (e.g. `target/audit-scratch`; use a path valid on your OS — a Unix-style `/tmp` path is mis-resolved on Windows) — the plugin refuses
5757
to generate into a directory containing a `pom.xml`, and when run from inside a Maven project it tries to add
5858
the result as a `<module>`. The scratch location avoids both pitfalls; only the post-generate copy step writes
5959
to your project.
@@ -86,7 +86,7 @@ mvn archetype:generate -DinteractiveMode=false \
8686
-DpostgresImage=postgres:16 \
8787
-DdisabledTests=false \
8888
-DdataSourceNames=none \
89-
-DoutputDirectory=/tmp/audit-scratch \
89+
-DoutputDirectory=target/audit-scratch \
9090
-DprojectDirectory=.
9191
----
9292

0 commit comments

Comments
 (0)