2

我有以下 Maven 项目结构:

xyz
  |
  -props
  |  |
  |  - root.properties
  |
  -module_a
  |  |
  |  -pom.xml
  |
  -pom.xml

我的根工件定义了一个子模块(module_a),这是引用父工件。在根 pom.xml 中,我正在使用 org.codehaus.mojo:properties-maven-plugin:1.0-alpha-2插件从 common props 文件夹中读取一个属性文件。

当我发出 mvn clean package 命令时,我得到以下 putput:

[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] my_artifact
[INFO] module_a
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building my_artifact 1.0.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ my_artifact ---
[INFO]
[INFO] --- properties-maven-plugin:1.0-alpha-2:read-project-properties (default) @ my_artifact ---
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building module_a 1.0.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ module_a ---
[INFO]
[INFO] --- properties-maven-plugin:1.0-alpha-2:read-project-properties (default) @ module_a ---
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] my_artifact ....................................... SUCCESS [0.234s]
[INFO] module_a .......................................... FAILURE [0.000s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.344s
[INFO] Finished at: Thu Aug 09 15:37:46 CEST 2012
[INFO] Final Memory: 2M/5M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:properties-maven-plugin:1.0-alpha-2:read-project-properties (default) on project module_a: Properties file not found: G:\java\xyz\module_a\props\root.properties -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
[ERROR]
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR]   mvn <goals> -rf :module_a

在子模块中,Maven 尝试从不存在的子文件夹中读取属性文件。有人可以帮我解决这个问题吗?谢谢

4

2 回答 2

2

当 maven 评估 pom 文件并创建有效的 pom(与继承聚合)时,某些属性被排除在继承之外。这些是以 project.* 开头的,因此也是 project.basedir。这就是为什么您无法定义从属性文件加载到父级的属性的原因。

这种行为的原因是隔离。每个模块都应该与除 Maven 存储库之外的所有内容隔离。即使使用 settings.xml 配置文件也违反了这个不变量,并且您失去了构建的可移植性。

这个不变量在很多情况下是不必要的,所以如果你有很多模块并且你想使用一个属性文件,你可以做的是:

  • 在你的根 pom 中定义一个属性,例如“filters.dir”到 ${project.basedir}/src/main/filters
  • 配置插件以从 ${filters.dir}/my.properties 加载属性文件
  • 在任何子模块中,您现在可以将 filters.dir 覆盖为 ../src/main/filters
  • 如果您有许多子模块,您可以在根目录中为 filters.dir 定义错误的路径“../src/main/filters”,然后您不需要在任何子模块中定义该属性。为了防止构建失败,您需要将插件设置为忽略失败。

另一种方法是通过扩展 AbstractMavenLifecycleParticipant 编写 maven 扩展,并使用 maven api 加载属性文件以确定项目的根目录。

于 2012-11-08T16:32:39.123 回答
0

我面临着类似的问题。

  • 我有一个父项目 pom.xml 有一个build.properties文件
  • 父项目有几个模块。
  • 当我创建一个新模块并想要构建整个父项目时,我得到了:

[ERROR] Failed to execute goal org.codehaus.mojo:properties-maven-plugin:1.0-alpha-2:read-project-properties (default) on project attachment-services: Properties file not found: /home/raz/git/parent/attachment-services/build.properties -> [Help 1]

为了解决这个问题,我在新模块中添加pom.xml了以下内容

<properties>
        <build.properties.location>..</build.properties.location>
    </properties>

当然,这build.properties.location已经在父 pom 中定义了,例如:

<properties>
<build.properties.location>${project.basedir</build.properties.location
</properties>
于 2018-11-27T13:41:16.223 回答