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
?
-
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 ShakibaJun 25, 2011 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.– jyoungdevNov 1, 2011 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.
-
46No, -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.– dunniJun 25, 2011 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.– mistNov 27, 2016 at 12:21
-
1java.lang.IllegalArgumentException: Parameter 'directory' is not a directory– 3xCh1_23May 7, 2017 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. Jan 24, 2019 at 12:32
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
-
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.– redochkaApr 11, 2015 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.– ErkSep 16, 2019 at 22:16
For me, works this way: mvn -f /path/to/pom.xml [goals]
-
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.– RudraMar 27, 2022 at 9:26
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.