100

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

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

4
  • Similar question stackoverflow.com/questions/13173063/… Commented Nov 1, 2012 at 8:22
  • 4
    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. Commented Dec 17, 2013 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. Commented Aug 22, 2016 at 14:33
  • 1
    Does this answer your question? Out-of-tree build with maven. Is it possible? Commented May 22, 2020 at 12:40

2 Answers 2

111

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

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you very much. Why is the second solution not recommended?
@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.
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.
I love this answer, but maybe include that you can activate a profile via settings.xml rather than command line, for even more portability?
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
|
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.

3 Comments

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 states that properties set in the settings file won't be interpolated in the settings file.
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?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.