325

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?

2
  • 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. Jun 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.
    – jyoungdev
    Nov 1, 2011 at 17:07

5 Answers 5

550

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.

9
  • 46
    No, -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, 2011 at 18:16
  • 19
    Use a dot for relative paths. e.g. ./path/pom.xml
    – Snekse
    Sep 6, 2013 at 16:50
  • 8
    As 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, 2016 at 12:21
  • 1
    java.lang.IllegalArgumentException: Parameter 'directory' is not a directory
    – 3xCh1_23
    May 7, 2017 at 15:22
  • 3
    Works 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
18

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

2
  • 6
    Excellent. 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, 2015 at 16:08
  • 1
    Got 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, 2019 at 22:16
18

For me, works this way: mvn -f /path/to/pom.xml [goals]

1
  • 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, 2022 at 9:26
3

You can try this:

pushd ../
maven install [...]
popd
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.