6

I start learning packaging for several distros (currently Cygwin and Debian).

They have requirement to build system to allow out-of-tree build (synonym out-of-source build):

http://wiki.debian.org/UpstreamGuide#Out-of-Tree_Builds

To work-around "dumb" build system for example cygport recommend use lndir (from xutils project):

  lndir ${S} ${B}
  cd {B}
  ...build-commands...

I read mvn(1) man page but doesn't found anything appropriated. Next I just try:

  $ mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
  ...
  $ pwd
  /maven/simple
  $ ls 
  my-app
  $ mvn -f my-app/pom.xml compile
  ...
  [INFO] --- maven-compiler-plugin:2.0.2:compile (default-compile) @ my-app ---
  [INFO] Compiling 1 source file to /maven/simple/my-app/target/classes

As you can see target directory created in source root hierarchy while I look for a way to avoid this.

Is it possible out-of-tree build with maven? And how?

Charles
  • 50,943
  • 13
  • 104
  • 142
gavenkoa
  • 45,285
  • 19
  • 251
  • 303
  • Please describe what you would expect. It's not clear. – Puce Nov 01 '12 at 08:02
  • 1
    **out-of-build** have strict meaning: you can cd to any directory and build project from it and all build artifact come exclusively to this directory. Setting target directory may satisfy this requiment. I look dipper if it work well. – gavenkoa Nov 01 '12 at 08:08
  • Please describe why you put vote to close question? It satisfy SO guide. – gavenkoa Nov 01 '12 at 08:10

2 Answers2

8

You could do like this to get it in your current working directory:

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.stackoverflow</groupId>
    <artifactId>Q13173063</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>${project.artifactId}-${project.version}</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <buildDir>${user.dir}</buildDir>
    </properties>

    <build>
        <directory>${buildDir}</directory>
    </build>
</project>

Then you can issue

mvn -f my-app/pom.xml compile

And it will give you your classes in the current working directory.

And easily change to another output directory:

mvn -f my-app/pom.xml -DbuildDir=/tmp/build compile
maba
  • 47,113
  • 10
  • 108
  • 118
3

It might be as simple as having a

<build>
    <directory>/your/build/directory</directory>
</build>

in your pom.xml. /your/build/directory need not be in the source tree and can be parameterized using the usual ${...} syntax.

Cheers,

Anders R. Bystrup
  • 15,729
  • 10
  • 59
  • 55
  • So there no any standard for build dir naming, so I must describe **-Dbuild.dir** key in project **INSTALL** file? – gavenkoa Nov 01 '12 at 08:11
  • Also if I build from **any** directory I need **fix** build+directory value to workaround relative paths... – gavenkoa Nov 01 '12 at 08:13
  • I think that this technique can be applied to Ant build system too... I just wander how it complicate to support project with subprojects... – gavenkoa Nov 01 '12 at 08:20