5

I need to get the absolute path to a subdirectory located just below where my root pom is. For example the pom is at: c:\somepath\pom.xml I need to get the absolute path to: c:\somepath\myIncludes\ I need this while building in a pom with a path like one these: c:\somepath\subdir1\pom.xml c:\somepath\subdir1\evendeeperdir\pom.xml

To try to stave off comments about how this is a dumb thing to need, I need this because:

  • The C++ code that gets built by the pom needs a path to includes.
  • There are multiple C++ projects with different relative paths ( so even though relative paths can work, it is too tedious and error prone to use multiple relative paths )
  • The project is mostly java (so I want to build it all with maven)
  • The whole thing is giant and legacy, so I cannot make sweeping changes in the structure.
Solx
  • 4,611
  • 5
  • 26
  • 29
  • Make it a property in the pom.xml – duffymo Jun 09 '16 at 15:39
  • Try with ${project.basedir} [here](https://maven.apache.org/guides/introduction/introduction-to-the-pom.html) there are some information about pom – giuseppe straziota Jun 09 '16 at 15:42
  • That almost works, but, I just realized I need a the directory a parent pom is in. So that I can set the path once from it and use it in child poms that are at different depths in the directory structure ( I will modify the question ) – Solx Jun 09 '16 at 15:48
  • @Solx Well then simply use `${project.basedir}/../myIncludes`. It is messy but will work. – Tunaki Jun 09 '16 at 16:46
  • I think you should take a look at https://github.com/maven-nar/nar-maven-plugin – khmarbaise Jun 09 '16 at 17:42
  • @Tunaki I believe I found that ${project.basedir} varies by which pom you are 'running' so if you are using ${project.basedir} in the parent pom but running maven from a child pom, then ${project.basedir} gets the child poms path. – Solx Jun 14 '16 at 21:00
  • @Solx That is correct, but you should probably not run Maven from a child pom to begin with ;). – Tunaki Jun 14 '16 at 21:01

3 Answers3

2

Declare a property in root pom as <properties><rootdirectory>${project.basedir}\myIncludes</rootdirectory> </properties>

For child pom's, override the the value of rootdirectory as <properties> <rootdirectory>${project.parent.basedir}\myIncludes</rootdirectory> </properties>

0

Create an env variable by adding a property to the maven profile settings`

<profile>
        <id>local-properties</id>
        <properties>
            <SOME_PATH>yourPath</SOME_PATH>
        </properties>
</profile>
kyserslick
  • 591
  • 1
  • 10
  • 27
  • 1
    This will be use by various people with varying absolute paths. I am looking for a method that automatically determines the path. – Solx Jun 09 '16 at 15:52
  • @Solx have you found this solution? – ypriverol Dec 12 '17 at 11:18
  • The solution I ended up using was to put a file with a specific name in the top directory and then running a groovy script to walk up directories to find it. – Solx Jan 11 '18 at 03:05
0

Once upon a time I needed to do something similar, but I needed a version. Perhaps you can adapt the procedure in this answer to determine a root directory.

Community
  • 1
  • 1
user944849
  • 14,524
  • 2
  • 61
  • 83
  • This looks like it might do it, but I cannot figure out what other attributes rootPom has. I am really hoping one is its path. – Solx Jun 10 '16 at 13:36
  • rootPom is just a Maven POM with all the properties a POM has in the [Maven API](http://maven.apache.org/ref/3.3.9/maven-core/apidocs/index.html?org/apache/maven/Maven.html). As I read the question, I think `rootPom.basedir` is what you're looking for. – user944849 Jun 10 '16 at 15:52
  • I used your idea to work out a solution. I used gmaven (groovy) to get the path to the pom is being built. Then I used goovy to extract the part of the path I needed. The solution is a real mess, but will give me the path. – Solx Jun 10 '16 at 20:02
  • But I will try your suggestion for 'rootPom.basedir' as soon as I get a chance, if it works it will be much cleaner. I am going to mark this an the answer because it pointed me to the means to do what I needed, and may help someone else in the future. – Solx Jun 10 '16 at 20:03