You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@maven.apache.org by "gnodet (via GitHub)" <gi...@apache.org> on 2026/09/24 21:52:10 UTC

[PR] Print usage when no goals and no POM are specified [maven]

gnodet opened a new pull request, #13269:
URL: https://github.com/apache/maven/pull/13269

   ## Problem
   
   When a user types `mvn` without any arguments and there is no `pom.xml` in the current directory, Maven currently fails with:
   
   ```
   [ERROR] No goals have been specified for this build. You must specify a valid lifecycle phase or a goal...
   [ERROR] BUILD FAILURE
   ```
   
   This is surprising and unhelpful — especially for new users. Tools like `java` and `javac` print usage in this situation.
   
   The discussion on the dev list (["Print usage when no arguments are given"](https://lists.apache.org/thread/...) Sep 2026) confirmed broad agreement that the current behavior should be improved, with the key constraint that default goals must still work.
   
   ## Solution
   
   Override `postCommands()` in `MavenInvoker` to detect the following narrow condition:
   
   1. **No goals or lifecycle phases were given** on the command line.
   2. **No POM file is present** in the current directory (or via `-f`).
   
   When both are true, print the same help output as `mvn -h` and exit with code 0.
   
   If a POM is found, Maven proceeds as before: the default goal (if any) is executed, or `NoGoalSpecifiedException` is reported as today. **Existing behavior for all normal project builds is unchanged.**
   
   ## Why this approach
   
   - **No shell script changes needed** — shell-script patching would break default goals and require identical changes to `mvn.cmd`, `mvnDebug`, etc.
   - **No full model resolution** — we reuse `determinePom()` which is a cheap file-existence check via `ModelProcessor.locateExistingPom()`. No parent POM downloads, no DI warmup.
   - **Right hook point** — `postCommands()` fires after `lookup()` (DI is ready, `context.lookup` is available), so `determinePom()` can be called safely.
   - **Consistent exit code** — exits 0, matching the `mvn -h` convention.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Print usage when no goals and no POM are specified [maven]

Posted by "gnodet (via GitHub)" <gi...@apache.org>.
gnodet commented on PR #13269:
URL: https://github.com/apache/maven/pull/13269#issuecomment-6147610985

   Thanks for the suggestion, @raupach-e2n! I'd actually prefer to keep the current phrasing for a couple of reasons:
   
   1. **Actionable path**: The message includes the actual path the user typed (e.g. `-f myproject/custom.xml`), not a hardcoded `pom.xml`. Without it the user has to guess which file was not found.
   2. **Context**: The `-f/--file` reference tells them exactly which argument to fix.
   3. **Consistency**: The `[ERROR] Error executing Maven.` header is standard Maven error framing used across the board — dropping it here would be inconsistent.
   
   The current output:
   ```
   [ERROR] Error executing Maven.
   [ERROR] POM file X specified with the -f/--file command line argument does not exist
   ```
   …is already a significant improvement over the previous double-printing. I think it strikes the right balance between brevity and actionability.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Print usage when no goals and no POM are specified [maven]

Posted by "ascheman (via GitHub)" <gi...@apache.org>.
ascheman commented on PR #13269:
URL: https://github.com/apache/maven/pull/13269#issuecomment-5828911460

   Heads-up: CI is red on this PR — `initial-build` fails at the `test-mvn-path-conversion` exec (project `apache-maven`), and it's caused by this change.
   
   All the path-*conversion* assertions pass (`ok - POSIX/CYGWIN/MINGW/MSYS …`); the script then aborts (it runs under `set -e`) at the first `-f` scenario:
   
   ```sh
   output=`run_mvn_with_args CYGWIN_NT-10.0 "$stub_dir" -f /cygdrive/c/temp/pom.xml validate`
   ```
   
   Root cause: this PR makes a non-existent `-f`/`--file` a **fatal early exit** in the launcher — `find_file_argument_basedir`'s `exit 1` is now propagated up (the correct fix for the double-error). Previously that "POM file … does not exist" message was printed but **non-fatal**, so the launcher continued. `test-mvn-path-conversion.sh` passes **simulated, non-existent** `-f` paths — it only checks path *conversion*, with `java` stubbed, so the file never had to exist — and under `set -e` the now-fatal exit aborts the test before it can assert the conversion.
   
   The behavior change is right; the test just needs to catch up — e.g. `touch` a real file at each `-f` path the test passes (or point them at an existing stub) so the existence check passes and the conversion assertions still run. (`-s` scenarios are unaffected — settings files aren't existence-checked in the launcher.)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Print usage when no goals and no POM are specified [maven]

Posted by "gnodet (via GitHub)" <gi...@apache.org>.
gnodet merged PR #13269:
URL: https://github.com/apache/maven/pull/13269


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Print usage when no goals and no POM are specified [maven]

Posted by "raupach-e2n (via GitHub)" <gi...@apache.org>.
raupach-e2n commented on PR #13269:
URL: https://github.com/apache/maven/pull/13269#issuecomment-5828425864

   I would like to suggest a better phrasing. Instead of
   
   ```
   POM file X does not exist
   [ERROR] Error executing Maven.
   [ERROR] Error determining top directory
   [ERROR] Caused by: POM file X ... does not exist
   ```
   
   Maybe this is sufficient:
   
   ```
   [ERROR] Caused by: pom.xml does not exist
   ```
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Print usage when no goals and no POM are specified [maven]

Posted by "Bukama (via GitHub)" <gi...@apache.org>.
Bukama commented on PR #13269:
URL: https://github.com/apache/maven/pull/13269#issuecomment-5832180590

   Approve for the intended change, but build needs to be checked ofc ;)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Print usage when no goals and no POM are specified [maven]

Posted by "gnodet-bot (via GitHub)" <gi...@apache.org>.
gnodet-bot commented on code in PR #13269:
URL: https://github.com/apache/maven/pull/13269#discussion_r4267831735


##########
impl/maven-core/src/main/java/org/apache/maven/project/ProjectModelResolver.java:
##########
@@ -263,6 +266,9 @@ public ModelSource resolveModel(final Dependency dependency) throws Unresolvable
         } catch (VersionRangeResolutionException e) {
             throw new UnresolvableModelException(
                     e.getMessage(), dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion(), e);
+        } catch (IllegalArgumentException e) {
+            throw new UnresolvableModelException(
+                    e.getMessage(), dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion(), e);
         }

Review Comment:
   💡 **Style:** Same multi-catch opportunity in the `Dependency` overload.
   
   ```suggestion
           } catch (VersionRangeResolutionException | IllegalArgumentException e) {
               throw new UnresolvableModelException(
                       e.getMessage(), dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion(), e);
           }
   ```



##########
impl/maven-core/src/main/java/org/apache/maven/project/ProjectModelResolver.java:
##########
@@ -220,6 +220,9 @@ public ModelSource resolveModel(final Parent parent) throws UnresolvableModelExc
         } catch (final VersionRangeResolutionException e) {
             throw new UnresolvableModelException(
                     e.getMessage(), parent.getGroupId(), parent.getArtifactId(), parent.getVersion(), e);
+        } catch (final IllegalArgumentException e) {
+            throw new UnresolvableModelException(
+                    e.getMessage(), parent.getGroupId(), parent.getArtifactId(), parent.getVersion(), e);
         }

Review Comment:
   💡 **Style:** Both catch blocks have identical bodies — collapse into a multi-catch to avoid duplication (this pattern is used extensively in the codebase, e.g. `DefaultMavenPluginManager`, `DefaultGraphBuilder`).
   
   ```suggestion
           } catch (final VersionRangeResolutionException | IllegalArgumentException e) {
               throw new UnresolvableModelException(
                       e.getMessage(), parent.getGroupId(), parent.getArtifactId(), parent.getVersion(), e);
           }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Print usage when no goals and no POM are specified [maven]

Posted by "ascheman (via GitHub)" <gi...@apache.org>.
ascheman commented on PR #13269:
URL: https://github.com/apache/maven/pull/13269#issuecomment-5828683788

   Looks good, and it lines up with the dev@ consensus on the "Print usage when no arguments are given" thread — POM present → the default goal (if any) still runs, otherwise print usage. Keying the Java check on POM presence rather than trying to detect a default goal also neatly side-steps the "detecting a default goal needs the built model, very early in startup" concern raised there. 👍
   
   One cross-platform gap: the `[ERROR]` reformatting of the `-f`/`--file` messages landed in the Unix `mvn` script but not in the Windows launcher `mvn.cmd`, which carries the same two messages (`:process_file_arg`):
   
   ```bat
   echo POM file "%FILE_ARG%" specified the -f/--file command-line argument does not exist >&2
   echo Directory "%POM_DIR%" extracted from the -f/--file command-line argument "%FILE_ARG%" does not exist >&2
   ```
   
   So Windows users still get the un-prefixed form. Those should become:
   
   ```bat
   echo [ERROR] POM file "%FILE_ARG%" specified with the -f/--file command-line argument does not exist >&2
   echo [ERROR] Directory "%POM_DIR%" extracted from the -f/--file command-line argument "%FILE_ARG%" does not exist >&2
   ```
   
   (also fixes a stray `specified the` → `specified with the`, matching the Unix wording). I'd have made these one-click `suggestion`s, but `mvn.cmd` isn't part of this PR's diff, so GitHub won't let me anchor a suggestion there — if you add the file they can be applied directly.
   
   The harder half of your Unix change needs no Windows counterpart, though: the subshell-exit propagation (so a bad `-f` aborts instead of continuing and printing the error twice) has nothing to fix in `mvn.cmd` — it already aborts on a bad `-f` via `goto error`, so it never had the double-error behavior. Only the `[ERROR]` prefix is missing there.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Print usage when no goals and no POM are specified [maven]

Posted by "gnodet (via GitHub)" <gi...@apache.org>.
gnodet commented on PR #13269:
URL: https://github.com/apache/maven/pull/13269#issuecomment-6214321349

   <!-- maven-backport-bot -->
   
   **Conflicts — manual resolution needed:**
   - ❌ `maven-3.10.x`: Cherry-pick conflict in: apache-maven/src/assembly/maven/bin/mvn, apache-maven/src/assembly/maven/bin/mvn.cmd, impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/BaseParser.java, impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/MavenInvoker.java, impl/maven-core/src/test/java/org/apache/maven/project/ProjectModelResolverTest.java
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org