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)
qoo*_*mon 18
至少在当前的Maven版本(3.6.0)中,您可以使用 ${maven.multiModuleProjectDirectory}
小智 15
将directory-maven-plugin与目标目录一起使用.
与其他建议不同:
该插件允许您将所选属性设置为任何项目模块的绝对路径.在我的情况下,我将它设置为根模块...在我的项目根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}
在任何其他插件中使用,等等.花了一些时间来解决这个问题,所以希望它可以帮助别人.
在我的情况下它的工作原理如下:
...
<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)
以下小配置文件对我有用.我需要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属性本身中只有"..",但它只能以这种方式工作.)
另一种选择:
在父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)