Maven: How to change path to target directory from command line?
(I want to use another target directory in some cases)
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
-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.
Oct 12, 2010 at 7:31
settings.xml
rather than command line, for even more portability?
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.
-Dalt.build.dir=~/mytarget
equivalent to using -D
for some other property?
Apr 15, 2013 at 0:34
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.src/it/my-integration-test-project/pom.xml
from the command line without creating atarget
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.