Finding the root directory of a multi module maven reactor project

前端 未结 13 1512
庸人自扰
庸人自扰 2020-12-02 11:11

I want to use the maven-dependency-plugin to copy artifacts from all sub-modules of my multi-module project to a directory that is relative to the root directory of the enti

相关标签:
13条回答
  • 2020-12-02 11:26

    With Maven 3.6.1, from a child project, ${parent.basedir} returns the fully qualified directory of the parent project.

    0 讨论(0)
  • 2020-12-02 11:28

    Another solution would be to use an ant task to write "rootdir=${basedir}" to a target/root.properties in the root project, and then use the Properties Plugin to read that file back in. I haven't tried it myself, but I guess it should work..?

    0 讨论(0)
  • 2020-12-02 11:28

    so that: somewhere in properties of some parent Project I've the file which i need to relate later, what's why i need absolute path on it everywhere. So, i get it with help of groovy:

    <properties>    
    <source> import java.io.File; 
            String p =project.properties['env-properties-file']; 
            File f = new File(p); 
            if (!f.exists()) 
            { 
               f = new File("../" + p); 
              if (!f.exists()) 
              { 
                 f = new File("../../" + p); 
              } 
            } 
            // setting path together with file name in variable xyz_format 
            project.properties['xyz_format'] =f.getAbsolutePath() 
                                + File.separator 
                                + "abc_format.xml"; 
    </source>
    </properties>   
    

    and then:

      <properties>
         <snapshots>http://localhost:8081/snapshots<snapshots>
         <releases>http://localhost:8081/releases</releases>
         <sonar>jdbc:oracle:thin:sonar/sonar@localhost/XE</sonar>
         <sonar.jdbc.username>sonar</sonar.jdbc.username>
         <format.conf> ${xyz_format}</format.conf>  <---- here is it!
        </properties>
    

    it works!

    0 讨论(0)
  • 2020-12-02 11:30

    use ${session.executionRootDirectory}

    For the record, ${session.executionRootDirectory} works for me in pom files in Maven 3.0.3. That property will be the directory you're running in, so run the parent project and each module can get the path to that root directory.

    I put the plugin configuration that uses this property in the parent pom so that it's inherited. I use it in a profile that I only select when I know that I'm going to run Maven on the parent project. This way, it's less likely that I'll use this variable in an undesired way when I run Maven on a child project (because then the variable would not be the path to the parent).

    For example,

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <executions>
            <execution>
                <id>copy-artifact</id>
                <phase>package</phase>
                <goals>
                    <goal>copy</goal>
                </goals>
                <configuration>
                    <artifactItems>
                        <artifactItem>
                            <groupId>${project.groupId}</groupId>
                            <artifactId>${project.artifactId}</artifactId>
                            <version>${project.version}</version>
                            <type>${project.packaging}</type>
                        </artifactItem>
                    </artifactItems>
                    <outputDirectory>${session.executionRootDirectory}/target/</outputDirectory>
                </configuration>
            </execution>
        </executions>
    </plugin>
    
    0 讨论(0)
  • 2020-12-02 11:30

    There is a maven plugin that solves this particular problem: directory-maven-plugin

    It will assign the root path of your project to a property of your choosing. See highest-basedir goal in the docs.

    For example:

    <!-- Directory plugin to find parent root directory absolute path -->
    <plugin>
      <groupId>org.commonjava.maven.plugins</groupId>
      <artifactId>directory-maven-plugin</artifactId>
      <version>0.1</version>
      <executions>
        <execution>
          <id>directories</id>
          <goals>
            <goal>highest-basedir</goal>
          </goals>
          <phase>initialize</phase>
          <configuration>
            <property>main.basedir</property>
          </configuration>
        </execution>
      </executions>
    </plugin>
    

    Then use ${main.basedir} anywhere in your parent / child pom.xml.

    0 讨论(0)
  • 2020-12-02 11:31

    The following small profile worked for me. I needed such a configuration for CheckStyle, which I put into the config directory in the root of the project, so I can run it from the main module and from submodules.

    <profile>
        <id>root-dir</id>
        <activation>
            <file>
                <exists>${project.basedir}/../../config/checkstyle.xml</exists>
            </file>
        </activation>
        <properties>
            <project.config.path>${project.basedir}/../config</project.config.path>
        </properties>
    </profile>
    

    It won't work for nested modules, but I'm sure it can be modified for that using several profiles with different exists's. (I have no idea why there should be "../.." in the verification tag and just ".." in the overriden property itself, but it works only in that way.)

    0 讨论(0)
提交回复
热议问题