In Maven, can I specify a relative path above my current project?

62,015

Solution 1

With Maven, things are relative to the directory containing the pom.xml (which is represented by the ${basedir} property and is called the base directory). There are however some situations where you could have to specify a relative path:

  • if a <parent> pom is not directly above a given module using a <relativePath> element (see this example)
  • if modules are not nested (i.e. if you use a flat layout) using a relative path in the various <module> elements (like in this example).

Having all that said, I'm not sure to understand what your situation exactly is or what you are describing.

Maybe you should show the relevant parts of your POM if this is possible.

Solution 2

Ideally the pom.xml should be in the same directory as your src directory. For eg suppose you have checked out your project from svn into a folder C:/work/project, then keep the main pom.xml in C:/work/project. src should also be in C:/work/project.

Your other subprojects should be in C:/work/subproject1, C:/work/subproject2 and so on each having their own pom.xml. Then in the main pom.xml you can refer to the other projects in the

<modules>      
    <module>../subproject1<module>
    <module>../subproject2<module>
</modules>
Share:
62,015
Bill K
Author by

Bill K

Long time Java programmer. Currently work on software for cable boxes and DVRs, mostly Java some C/C++

Updated on October 20, 2020

Comments

  • Bill K
    Bill K over 3 years

    I'm learning maven on the fly while doing work on a project with a large set of projects to build.

    Currently a line in the main build uses an absolute path to specify a directory that is part of the subversion repository but "above" it's directory.

    as in: "C:/work/project/eclipse" where "project" is the checked-in directory, and the pom.xml is in "C:/work/project/src/subproject/pom.xml"

    I'd like to make that line a relative address instead.

    I tried specifying "../../Eclipse....", put that didn't seem to work.

    It could also be because that same variable is being used by a sub-sub-project's pom file.

    Any advice (aside from rewrite the whole mess, which I just don't know enough about maven to do yet)?

  • Jaime Hablutzel
    Jaime Hablutzel about 10 years
    You say things are relative to the directory containing the pom.xml, but shouldn't it be things are relative to the directory containing the pom.xml being built?, because for example (and maybe I'm wrong) if you use somewhere in a parent module a variable like ${project.basedir} and then you build the child module, ${project.basedir} becomes the directory for the child, not the parent, so that variable usage wouldn't be actually relative to parent's pom.xml
  • Matteo Codogno
    Matteo Codogno over 9 years
    Can I pass to <relativePath> a variable ? For example: <relativePath>${parentPath}</relativePath>where my parentPath variable was defined inside properties tag?
  • inetphantom
    inetphantom about 6 years
    @JaimeHablutzel Can you provide that as an answr?
  • granadaCoder
    granadaCoder almost 6 years
    <modules> <module>../my-module</module> </modules>