95

Maven: How to change path to target directory from command line?

(I want to use another target directory in some cases)

Igor Mukhin
  • 15,014
  • 18
  • 52
  • 61
  • Similar question http://stackoverflow.com/questions/13173063/out-of-tree-build-with-maven-is-it-possible – gavenkoa Nov 01 '12 at 08:22
  • 3
    Simple answer: I have an IDE such as Eclipse building into `target/`, and I want to be able to build from the command line as well without the two processes stepping on each other's toes. – john sullivan Dec 17 '13 at 19:57
  • Another use case that I've found for this -- building integration tests like `src/it/my-integration-test-project/pom.xml` from the command line without creating a `target` directory in the source tree which will get copied over when the integration tests are run as part of the containing project's verify phase. – Mike Samuel Aug 22 '16 at 14:33
  • 1
    Does this answer your question? [Out-of-tree build with maven. Is it possible?](https://stackoverflow.com/questions/13173063/out-of-tree-build-with-maven-is-it-possible) – 030 May 22 '20 at 12:40

2 Answers2

106

You should use profiles.

<profiles>
    <profile>
        <id>otherOutputDir</id>
        <build>
            <directory>yourDirectory</directory>
        </build>
    </profile>
</profiles>

And start maven with your profile

mvn compile -PotherOutputDir

If you really want to define your directory from the command line you could do something like this (NOT recommended at all) :

<properties>
    <buildDirectory>${project.basedir}/target</buildDirectory>
</properties>

<build>
    <directory>${buildDirectory}</directory>
</build>

And compile like this :

mvn compile -DbuildDirectory=test

That's because you can't change the target directory by using -Dproject.build.directory

Colin Hebert
  • 91,525
  • 15
  • 160
  • 151
  • 1
    Thank you very much. Why is the second solution not recommended? – Igor Mukhin Oct 12 '10 at 07:11
  • 1
    @iimuhin, the first solution is the correct usage of possibilities given by maven configuration, the second one is more a trick to make it work. If the options `-Dproject.build.directory` was meant to be used, it would be useable; and this is a workaround for the `-Dproject.build.directory` problem. Plus with the first solution, you specify paths once and for all, you can't do a typo in the directory name when you launch the command line, you can easily use this solution even if you work from an IDE, etc. – Colin Hebert Oct 12 '10 at 07:31
  • 11
    The problem with the profiles solution is that many of the use cases for changing the target directory are user-specific (like wanting to build to a ram disk) and don't belong in the pom. Profiles in the user-specific settings.xml cannot contain a build element, so that is not an option. – EricS Jul 15 '14 at 22:52
  • I love this answer, but maybe include that you can activate a profile via `settings.xml` rather than command line, for even more portability? – Rogue Apr 05 '15 at 21:31
  • 1
    I like your "not recommended solution" as it enabled me to build on the command line in alternative directories, thus without disturbing Eclipse that points to the default ${project.basedir}/target. Hence I can code in Eclipse while building on the command line at the same time without waiting for Eclipse to refresh/rebuild – Francois Marot Jun 30 '17 at 15:41
  • Why "not recommended"? For build automation it is just necessary. Imagine, you build production and debug in parallel and you don't want these two builds to clash. – Alexander Samoylov Jul 20 '23 at 10:44
31

Colin is correct that a profile should be used. However, his answer hard-codes the target directory in the profile. An alternate solution would be to add a profile like this:

    <profile>
        <id>alternateBuildDir</id>
        <activation>
            <property>
                <name>alt.build.dir</name>
            </property>
        </activation>
        <build>
            <directory>${alt.build.dir}</directory>
        </build>
    </profile>

Doing so would have the effect of changing the build directory to whatever is given by the alt.build.dir property, which can be given in a POM, in the user's settings, or on the command line. If the property is not present, the compilation will happen in the normal target directory.

Kricket
  • 4,049
  • 8
  • 33
  • 46
  • Sorry for the n00b question, but what's the appropriate way to set the property in the user's settings file for this to be advantageous? I understand from the [Settings Reference](http://maven.apache.org/settings.html) states that properties set in the settings file won't be interpolated in the settings file. – Michael Scheper Apr 15 '13 at 00:26
  • Also, what advantage would this offer? I'm probably missing something, but assuming the OP's 'cases' are specified at the command line, wouldn't this just make the parameter `-Dalt.build.dir=~/mytarget` equivalent to using `-D` for some other property? – Michael Scheper Apr 15 '13 at 00:34
  • Ah, I've just realised that everyone here is talking about adding profiles to the POM, and not settings.xml. That would make my questions at least as m00t as n00b. Sorry about that. – Michael Scheper Apr 15 '13 at 01:14