How to run Maven from another directory (without cd to project dir)?
Asked Answered
A

5

329

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?

Ammonate answered 25/6, 2011 at 14:58 Comment(2)
@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.Ammonate
@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.Egest
V
556

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.

Verily answered 25/6, 2011 at 17:56 Comment(9)
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.Verily
Use a dot for relative paths. e.g. ./path/pom.xmlPellmell
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.Alcaeus
java.lang.IllegalArgumentException: Parameter 'directory' is not a directoryMissy
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.Appreciate
+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.)Tadio
Also, for those who are curious, the long form of the -f switch is --file (see here and here). Wanted to mention it because apparently that hasn't always been the case.Tadio
As of 2023, this probably works in most cases. But it definitely does not change the current directory. Simply use something like something like Maven Enforcer's evaluateBeanshell rule and evaluate something like System.out.println(new File(".").getAbsolutePath()); there before returning true, then you see it on the console. Soif you have any plugin actions somehow depending on the current directory (due to poor implementation), better cd into the directory housing the POM first.Calcifuge
Indeed, it does not change de working directory. When using --settings (by CLI or .mvn/maven.config) the path is not relative to the POM.Lines
D
19

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

Drillstock answered 14/2, 2019 at 18:47 Comment(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.Demigod
P
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

Protomartyr answered 25/6, 2011 at 16:6 Comment(2)
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.Thurston
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.Veiled
G
3

You can try this:

pushd ../
maven install [...]
popd
Greening answered 1/10, 2019 at 7:38 Comment(0)
I
-1

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.

Inwrap answered 5/1, 2023 at 15:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.