Supposing my maven project is located in /some/location/project
and my current location is /another/location/
how can I run maven build without changing to project location cd /some/location/project
?

- 11,553
- 8
- 64
- 88

- 20,549
- 18
- 61
- 88
-
4@khmarbaise - I'm trying to automate build and deployments using some shell scripts and it's not straight to always use cd to change dir. – Ali Shakiba Jun 25 '11 at 17:59
-
@khmarbaise - you may want to refer to multiple files or folders in or near one directory, but your pom and files it refers to are in another. – jyoungdev Nov 01 '11 at 17:07
5 Answers
You can use the parameter -f
(or --file
) and specify the path to your pom file, e.g. mvn -f /path/to/pom.xml
This runs maven "as if" it were in /path/to
for the working directory.
-
45No, -f starts Maven with that specific pom, and by definition the directory where the pom is, is the working directory for Maven. I use this in my CI-Server to build specific modules in subdirectories, and i can assure you, that it works. – dunni Jun 25 '11 at 18:16
-
19
-
8As dunni says, this doesn't work. The current dir is where you are. U're just using a pom in another dir. Maven doesn't support this. – mist Nov 27 '16 at 12:21
-
1java.lang.IllegalArgumentException: Parameter 'directory' is not a directory – 3xCh1_23 May 07 '17 at 15:22
-
3Works for most of my projects. Unfortunately, some projects using the exec plugin cannot be build this way because the plugin does not find the executable even though the working directory is configured. – user2043553 Jan 24 '19 at 12:32
-
1+1. At least in my case, I found that I could simply provide the path to the directory containing `pom.xml`, rather than `pom.xml` itself. In other words, for the given example, `mvn -f /path/to` and `mvn -f /path/to/pom.xml` behave the same way. (Using Maven 3.5.4. Admittedly, this might not have been the case when the answer was written.) – Aaron Sep 05 '20 at 00:30
-
Also, for those who are curious, the long form of the `-f` switch is `--file` (see [here](https://stackoverflow.com/a/47906431/1098302) and [here](https://maven.apache.org/ref/3-LATEST/maven-embedder/cli.html)). Wanted to mention it because [apparently](https://maven.apache.org/archives/maven-1.x/reference/command-line.html) that hasn't always been the case. – Aaron Sep 05 '20 at 00:36
For me, works this way: mvn -f /path/to/pom.xml [goals]

- 189
- 2
- 6
-
I had to use ./path/to/pom.xml, If this fails for you use . for the path. I have tried in Jenkins pipeline and it works fine. – Rudra Mar 27 '22 at 09:26
I don't think maven supports this. If you're on Unix, and don't want to leave your current directory, you could use a small shell script, a shell function, or just a sub-shell:
user@host ~/project$ (cd ~/some/location; mvn install)
[ ... mvn build ... ]
user@host ~/project$
As a bash function (which you could add to your ~/.bashrc):
function mvn-there() {
DIR="$1"
shift
(cd $DIR; mvn "$@")
}
user@host ~/project$ mvn-there ~/some/location install)
[ ... mvn build ... ]
user@host ~/project$
I realize this doesn't answer the specific question, but may provide you with what you're after. I'm not familiar with the Windows shell, though you should be able to reach a similar solution there as well.
Regards

- 26,788
- 9
- 50
- 60
-
6Excellent. mvn -f param doesn't work with multimodule projects using relative path to reference the child poms. Thanks, that's what I was looking for. – redochka Apr 11 '15 at 16:08
-
1Got this working on Mac (and I know it will work on Linux). On Windows (or any platform) you should be able to save the current path, cd to your project, do mvn stuff there and cd back in a script. The only issue would be if the script stopped halfway. – Erk Sep 16 '19 at 22:16
If you want to run maven without this command "mvn -f path/to/pom.xml" you can right click on your folder project (in intellij) and click on Rebuild module "name of your artifactId" (corresponding in your pom.xml). It worked for me.