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

JavaMaven 2Maven

Java Problem Overview


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

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

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionIgor MukhinView Question on Stackoverflow
Solution 1 - JavaColin HebertView Answer on Stackoverflow
Solution 2 - JavaKricketView Answer on Stackoverflow