Maven2属性,指示父目录

Rom*_*las 98 maven-2 properties

我有一个多模块项目,像这样:

main-project/
    module1/
    module2/
        sub-module1/
        sub-module2/
        sub-module3/
        ...
    module3/
    module4/
    ...
Run Code Online (Sandbox Code Playgroud)

我需要在Maven2中定义一组属性(取决于我想要释放项目的环境).我不会使用<properties>因为有很多属性...因此,我使用Properties Maven2插件.

属性文件位于main-project/目录中.如何在主pom.xml中设置正确的目录,以便为任何子项指定在哪里找到属性文件?

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>properties-maven-plugin</artifactId>
    <version>1.0-alpha-1</version>
    <executions>
        <execution>
            <phase>initialize</phase>
            <goals>
                <goal>read-project-properties</goal>
            </goals>
            <configuration>
                <files>
                    <file>???/env_${env}.properties</file>
                </files>
            </configuration>
        </execution>
    </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)

如果我只设置<file>env_${env}.properties</file>,那么当Maven2编译第一个模块时,它将找不到该main-project/env_dev.properties文件.如果我设置<file>../env_${env}.properties</file>,那么将在父级别或任何子模块级别引发错误...

Cla*_*lay 153

尝试在每个pom中设置属性以查找主项目目录.

在父母:

<properties>
    <main.basedir>${project.basedir}</main.basedir>
</properties>
Run Code Online (Sandbox Code Playgroud)

在孩子们:

<properties>
    <main.basedir>${project.parent.basedir}</main.basedir>
</properties>
Run Code Online (Sandbox Code Playgroud)

在孙子孙女:

<properties>
    <main.basedir>${project.parent.parent.basedir}</main.basedir>
</properties>
Run Code Online (Sandbox Code Playgroud)

  • 叹.令人难以置信的是,Maven让这么难. (78认同)
  • 我已成功使用`$ {project.basedir}/..`但这实际上只适用于严格目录层次结构中的多模块项目. (18认同)
  • 是否删除了这些预设属性?`$ {parent.basedir}`不再解析3.0.4中的任何内容...... (14认同)
  • 是的,这不起作用.$ {project.parent.basedir}的计算结果为null. (4认同)
  • 和上面的Jonathan一样,我必须使用相对路径,但是觉得最好使用`file.separator`变量,例如`<main.basedir> $ {project.basedir} $ {file.separator} .. </ main. BASEDIR>` (4认同)
  • 请参阅https://jira.codehaus.org/browse/MNG-5522,它似乎是一个错误. (3认同)
  • 拍好.是否有适用于3.0.4及更高版本的解决方案? (2认同)

qoo*_*mon 18

至少在当前的Maven版本(3.6.0)中,您可以使用 ${maven.multiModuleProjectDirectory}

  • 不知道为什么这个不被投票更多:| (3认同)
  • 试图找到关于此的文档,似乎这是作为内部用法,将来可能会被删除/更改 [MNG-6589](https://issues.apache.org/jira/browse/MNG-6589) (2认同)
  • 我不确定我们应该使用这个答案。根据[这张票](https://issues.apache.org/jira/browse/MNG-6589),它是内部的,可能随时消失。这也是为什么它没有在任何地方记录的原因。仍然没有干净的解决方案 (2认同)

小智 15

directory-maven-plugin与目标目录一起使用.

与其他建议不同:

  • 此解决方案适用于多模块项目.
  • 无论您是构建整个项目还是子模块,它都有效.
  • 无论您是从根文件夹还是子模块运行maven,它都有效.
  • 不需要在每个子模块中设置相对路径属性!

该插件允许您将所选属性设置为任何项目模块的绝对路径.在我的情况下,我将它设置为根模块...在我的项目根pom:

<plugin>
    <groupId>org.commonjava.maven.plugins</groupId>
    <artifactId>directory-maven-plugin</artifactId>
    <version>0.1</version>
    <executions>
        <execution>
            <id>directories</id>
            <goals>
                <goal>directory-of</goal>
            </goals>
            <phase>initialize</phase>
            <configuration>
                <property>myproject.basedir</property>
                <project>
                    <groupId>com.my.domain</groupId>
                    <artifactId>my-root-artifact</artifactId>
                </project>
            </configuration>
        </execution>
    </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)

从那时起,任何子模块pom中的$ {myproject.basedir}始终具有项目根模块的路径.当然,您可以将属性设置为任何模块,而不仅仅是根...


Rom*_*las 14

我找到了解决问题的解决方案:我使用Groovy Maven插件搜索属性文件.

由于我的属性文件必须在当前目录中,在../或../..中,我编写了一个小的Groovy代码来检查这三个文件夹.

这是我的pom.xml的摘录:

<!-- Use Groovy to search the location of the properties file. -->
<plugin>
    <groupId>org.codehaus.groovy.maven</groupId>
    <artifactId>gmaven-plugin</artifactId>
    <version>1.0-rc-5</version>
    <executions>
        <execution>
            <phase>validate</phase>
            <goals>
                <goal>execute</goal>
            </goals>
            <configuration>
                <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);
                        }
                    }
                    project.properties['env-properties-file-by-groovy'] = f.getAbsolutePath();
            </source>
            </configuration>
        </execution>
    </executions>
</plugin>
<!-- Now, I can load the properties file using the new 'env-properties-file-by-groovy' property. -->
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>properties-maven-plugin</artifactId>
    <version>1.0-alpha-1</version>
    <executions>
        <execution>
            <phase>initialize</phase>
            <goals>
                <goal>read-project-properties</goal>
            </goals>
            <configuration>
                <files>
                    <file>${env-properties-file-by-groovy}</file>
                </files>
            </configuration>
        </execution>
    </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)

这很有用,但我真的不喜欢它.

所以,如果你有更好的解决方案,请不要犹豫发布!


Jar*_*red 11

所以我看到的问题是你无法获得maven中父目录的绝对路径.

<rant> 我听说这是一种反模式,但是对于每种反模式都有真实合法的用例,而且我厌倦了maven告诉我我只能遵循他们的模式.</咆哮>

所以我发现的工作是使用antrun.在子pom.xml中尝试这个:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.7</version>
    <executions>
        <execution>
            <id>getMainBaseDir</id>
            <phase>validate</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <exportAntProperties>true</exportAntProperties>
                <target>
                    <!--Adjust the location below to your directory structure -->
                    <property name="main.basedir" location="./.." />
                    <echo message="main.basedir=${main.basedir}"/>
                </target>
            </configuration>
        </execution>
    </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)

如果你跑步,mvn verify你应该看到这样的事情:

main:
     [echo] main.basedir=C:\src\parent.project.dir.name
Run Code Online (Sandbox Code Playgroud)

然后你可以${main.basedir}在任何其他插件中使用,等等.花了一些时间来解决这个问题,所以希望它可以帮助别人.


use*_*153 6

在我的情况下它的工作原理如下:

...
<properties>
  <main_dir>${project.parent.relativePath}/..</main_dir>
</properties>
...

<plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>properties-maven-plugin</artifactId>
        <version>1.0-alpha-1</version>
        <executions>
          <execution>
            <phase>initialize</phase>
            <goals>
              <goal>read-project-properties</goal>
            </goals>
            <configuration>
              <files>
                 <file>${main_dir}/maven_custom.properties</file>
              </files>
            </configuration>
          </execution>
        </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)


Vic*_*Vic 6

以下小配置文件对我有用.我需要CheckStyle这样的配置,我把它放在config项目根目录的目录中,所以我可以从主模块和子模块运行它.

<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>
Run Code Online (Sandbox Code Playgroud)

它不适用于嵌套模块,但我确信可以使用不同的配置文件修改它exists.(我不知道为什么在验证标签中应该有"../ .."而在overriden属性本身中只有"..",但它只能以这种方式工作.)


Fra*_*rot 6

另一种选择:

在父pom中,使用:

<properties>
   <rootDir>${session.executionRootDirectory}</rootDir>
<properties>
Run Code Online (Sandbox Code Playgroud)

在儿童poms中,您可以引用此变量.

主要警告:它强制您始终从主父pom目录执行命令.然后,如果您只想为某些特定模块运行命令(例如测试),请使用以下语法:

mvn test --projects

然后,用于参数化"path_to_test_data"变量的surefire的配置可以是:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>${surefire.plugin.version}</version>
    <configuration>
        <systemPropertyVariables>
            <path_to_test_data>${rootDir}/../testdata</path_to_test_data>
        </systemPropertyVariables>
    </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)