为什么我不能使用父属性作为子POM文件

时间:2017-01-12 16:39:26

标签: maven-3 pom.xml

在parent.pom中,我有与子POM文件中相同的groupID:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.testing</groupId>
    <artifactId>master</artifactId>
    <version>11.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>Master</name>
    <description>The Master project with multiple sub-modules.</description>

    <properties>
        <!-- Project Versions -->
        <ear.jar.version>11.0-SNAPSHOT</ear.jar.version>
    </properties>

在儿童POM中,我有:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>master</artifactId>
        <groupId>com.testing</groupId>
        <version>${ear.jar.version}</version>
    </parent>

错误地说:"Project 'com.testing:master:${ear.jar.version}' not found

有人看到我做错了吗?

3 个答案:

答案 0 :(得分:2)

以下目录结构:

.
├── pom.xml
└── example-child
    └── pom.xml

使用以下父POM:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>example-parent</artifactId>
    <name>example-parent</name>
    <packaging>pom</packaging>
    <version>${example.version}</version>

    <properties>
        <example.version>1.0.0</example.version>
    </properties>

    <modules>
        <module>example-child</module>
    </modules>

</project>

和以下儿童POM:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.example</groupId>
        <artifactId>example-parent</artifactId>
        <version>${example.version}</version>
    </parent>

    <artifactId>example-child</artifactId>
    <name>example-child</name>
    <packaging>jar</packaging>

</project>

导致在顶层目录中成功构建并带有警告

$ mvn compile
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for com.example:example-child:jar:1.7.7
[WARNING] 'version' contains an expression but should be a constant. @ com.example:example-parent:${example.version}, C:\devel\cgi\itte\itte-client\pom-test\pom.xml, line 10, column 12
[WARNING]
[WARNING] Some problems were encountered while building the effective model for com.example:example-parent:pom:1.7.7
[WARNING] 'version' contains an expression but should be a constant. @ com.example:example-parent:${example.version}, C:\devel\cgi\itte\itte-client\pom-test\pom.xml, line 10, column 12
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] example-child
[INFO] example-parent
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building example-child 1.7.7
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ example-child ---
[WARNING] Using platform encoding (Cp1257 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\devel\cgi\itte\itte-client\pom-test\example-child\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ example-child ---
[INFO] No sources to compile
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building example-parent 1.7.7
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] example-child ...................................... SUCCESS [  2.403 s]
[INFO] example-parent ..................................... SUCCESS [  0.000 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.483 s
[INFO] Finished at: 2017-01-12T18:56:23+02:00
[INFO] Final Memory: 10M/309M
[INFO] ------------------------------------------------------------------------

所以,您想要做的是,但不推荐。更好地使用其他方法来持续管理Maven release plugin等版本。

答案 1 :(得分:1)

从Maven 3.5.0-beta-1开始,您可以在父元素中使用特殊的CI-friendly "revision" property。您需要确保Maven可以通过使用传统的目录结构或根据需要提供relativePath元素来找到您的父pom.xml。

使用传统的多项目目录布局:

.
├── pom.xml
└── child
    └── pom.xml

父pom.xml:

<project>
  <groupId>ima</groupId>
  <artifactId>parent</artifactId>
  <version>${revision}</version>    <--- NEW CI-friendly property
  <packaging>pom</packaging>

  <properties>
    <revision>0.1.2-SNAPSHOT</revision> <--- Defined like a normal property
  </properties>

  <modules>
    <module>child</module>
  </modules>

  <!-- NOTE! flatten-maven-plugin is needed if you have other 
       projects depending on the child -->
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>flatten-maven-plugin</artifactId>
        <version>1.0.0</version>
        <configuration>
          <updatePomFile>true</updatePomFile>
        </configuration>
        <executions>
          <execution>
            <id>flatten</id>
            <phase>process-resources</phase>
            <goals>
              <goal>flatten</goal>
            </goals>
          </execution>
          <execution>
            <id>flatten.clean</id>
            <phase>clean</phase>
            <goals>
              <goal>clean</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

Child pom.xml:

<project>
  <parent>
    <groupId>ima</groupId>
    <artifactId>parent</artifactId>
    <version>${revision}</version>  <--- Property can be used in parent element
  </parent>
  <artifactId>child</artifactId>
</project>

答案 2 :(得分:0)

真正批评你所发布的内容:

您正尝试使用来自父pom的动态填充版本信息来引用子pom中的父pom。

如何在包含父pom的版本之前检索它?

${ear.jar.version}仅是您尝试使用该信息引用的文件中的信息;这是不合逻辑的,也是不可能的。