views:

35

answers:

1

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

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

+3  A: 

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
good idea ( +1 )
seanizer
Thank you very much. Why is the second solution not recommended?
iimuhin
@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