本文介绍了无法从父pom.xml解析子pom.xml中的$ {project.version}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目层次结构为:

A - pom.xml
|__ B - pom.xml
    |__ C - pom.xml

属性project.version是在A中定义的pom.xml中定义的.其他两个pom指定了父标记以及到各个父pom的相对路径.

The property project.version is defined in pom.xml defined in A. Other two pom's specify the parent tag and the corresponding relative path to the respective parent pom's.

<parent>
        <groupId>com.GRP.id</groupId>
        <artifactId>ARTIFACT_ID</artifactId>
        <version>${project.version}</version>
        <relativePath>../pom.xml</relativePath>
</parent>

这里的问题是maven无法解析 $ {project.version} 并按原样使用它.从A/B/C执行时,这将引发以下异常:

The issue here is that maven is not able to resolve ${project.version} and is using it as is. This is throwing the following exception when executed from A/B/C:

[ERROR] The build could not read 1 project -> [Help 1]
org.apache.maven.project.ProjectBuildingException: Some problems were encountered while processing the POMs:
[FATAL] Non-resolvable parent POM for com.project_name.module_name:sub_module_name:[unknown-version]: Could not transfer artifact com.project_name.module_name:module_name:pom:${project.version} from
to env-module_name-all-repos (REPO_URL): Illegal character in path at index 96: https://DEMO
/artifactory/env-module_name-all-repos/com/project_name/module_name/module_name/${project.version}/module_name-${project.version}.pom and 'parent.relativePath' points at wrong
local POM @ com.project_name.module_name:sub_module_name:[unknown-version], C:\WorkSpaces\Repository\sub_module_name\pom.xml, line 10, column 10

关于如何从子POM访问相同内容的任何建议.

Any suggestion on how to access the same from child POMs.

推荐答案

@Sumit,

Maven在项目自己的groupIdartifactIdversion之前检查<parent>块及其包含的groupIdartifactIdversion.

Maven inspects the <parent> block and its contained groupId, artifactId, and version before the project's own groupId, artifactId and version.

因此,应避免在<parent>块内使用任何类似于${...}的内容.但是可以采用相反的情况:可以在pom文件中的其他位置引用父级的属性:

So you should avoid anything that looks like ${...} inside the <parent> block. The reverse situation is OK though: parent's properties can be referenced elsewhere in the pom file:

<project>

    <!-- parent's GAV: inspected first, cannot use tokens -->
    <parent>
        <groupId>com.GRP.id</groupId>
        <artifactId>parent-id</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <!-- project's GAV: inspected second, may reference parent -->
    <groupId>${parent.groupId}</groupId>
    <artifactId>child-id</artifactId>
    <version>${parent.version}</version>
    <properties>
        <some.prop.name>${parent.artifactId}</some.prop.name>     <!-- parent-id -->
        <some.other.prop>${project.artifactId}</some.other.prop>  <!-- child-id -->
    </properties>
</project>

这样想:如果您有儿子或女儿,则可以选择以自己的名字命名-但以您的孩子命名是没有道理的em>自从您第一次出现以来!

Think of it this way: if you have a son or daughter, you might choose to name them after yourself - but it would make no sense to name you after your child since you came into existence first!

希望有帮助.

让我们再次看一下示例pom:

Let's look at your example pom again:

<parent>
    <groupId>com.GRP.id</groupId>
    <artifactId>ARTIFACT_ID</artifactId>
    <version>${project.version}</version>      <!-- value doesn't exist yet -->
    <relativePath>../pom.xml</relativePath>
</parent>

因此,在您孩子的pom.xml文件中,<parent>块包含对${project.version}的引用.但是如上所述,该值尚不存在,因为<parent>块是我们要评估的第一件事.

So in your child's pom.xml file, the <parent> block contains a reference to the ${project.version}. But as mentioned above, that value doesn't exist yet since the <parent> block is the first thing we're evaluating.

如果您进行如下更改,一切都会好起来的:

If you change it as follows, things will be fine:

<parent>
    <groupId>com.GRP.id</groupId>
    <artifactId>ARTIFACT_ID</artifactId>

  <!-- EDIT 3A: parent.version is mandatory and must be a static value -->
    <version>1.0-SNAPSHOT</version>
    <relativePath>../pom.xml</relativePath>
</parent>
<groupId>com.GRP.id</groupId>
<artifactId>CHILD_ARTIFACT_ID</artifactId>

<!-- EDIT 3B: project.version is optional, can be deleted
<version>${parent.version}</version>
-->

编辑2

最后一点信息.

请记住,当您最初在命令行上运行mvn compile时,您是从子pom目录运行的.

Remember, when you initially run mvn compile on the command line, you're running from the directory of the child pom.

Maven尚不知道父代的pom.xml文件在哪里.

必须使用<parent>块找出pom文件的位置,然后才能读取文件内容.

It has to use the <parent> block to figure out where the pom file is, only then can it read the contents of the file.

希望能澄清一些事情.

这篇关于无法从父pom.xml解析子pom.xml中的$ {project.version}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 07:05