1. Overview

In this quick tutorial, we're going to see how to run the mvn command from any directory outside of the pom.xml.

2. mvn From Another Directory

If we run any mvn sub-command from a directory that does not contain a pom.xml file, the command will fail:

$ mvn clean compile
The goal you specified requires a project to execute but there is no POM in this directory.
Please verify you invoked Maven from the correct directory

As shown above, Maven complains about the absence of a pom.xml file in the current directory.

To fix this issue and call a Maven phase or goal from another directory, we can use the -f or –file option:

$ mvn -f tutorials/ clean compile

Since there is a pom.xml file inside the specified directory, this command will actually compile the code.

Basically, this option forces the use of an alternate POM file or directory with pom.xml. So we can also use a full file path:

$ mvn -f tutorials/pom.xml clean compile

3. Conclusion

In this short tutorial, we saw how to run the mvn command from another directory.

Generic bottom

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

Comments are closed on this article!