【问题标题】:Why can't I use Parent properties for child POM file为什么我不能对子 POM 文件使用父属性
【发布时间】:2017-01-12 16:39:25
【问题描述】:

在 parent.pom 中,我的 groupID 与子 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

有人看到我做错了吗?

【问题讨论】:

    标签: maven-3 pom.xml


    【解决方案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>
    

    子 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】:

      认真思考您发布的内容:

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

      在被包含之前如何获取父pom的版本?

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

      【讨论】:

        【解决方案3】:

        如下目录结构:

        .
        ├── 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

        【讨论】:

        • 因此,请勿执行此建议。 在为 com.example:example-parent:pom:1.7.7 构建有效模型时遇到了一些问题 [WARNING] 'version' 包含一个表达式,但应该是一个常量。因此,未来的 Maven 版本可能不再支持构建此类格式错误的项目。,
        • 因此最后有明确的指导方针:你做的事情是可能的,但不推荐
        猜你喜欢
        • 1970-01-01
        • 2018-08-09
        • 2012-03-26
        • 2018-03-05
        • 1970-01-01
        • 1970-01-01
        • 2012-03-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多