簡體   English   中英

Maven屬性問題

[英]Issue with Maven 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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.parent.pom</groupId>
    <artifactId>parent-pom</artifactId>
    <version>1.0.0</version>
    <name>parentpom</name>
    <packaging>pom</packaging>
    <properties>
        <parentversion>1.0.0</parentversion>
    </properties>
</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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
    <groupId>com.parent.pom</groupId>
    <artifactId>parent-pom</artifactId>
    <version>${parentversion}</version>
    <relativePath>../pom.xml</relativePath>
  </parent>
    <groupId>com.child.pom</groupId>
    <artifactId>child-pom</artifactId>
    <version>2.0.0</version>
    <name>Childpom</name>
    <packaging>pom</packaging>
</project>

我的文件夾結構像

POM-test(具有父pom的根文件夾)--- child(具有子pom)

當我在孩子中運行mvn clean install時,出現如下異常。

ERROR] The build could not read 1 project -> [Help 1]
ERROR]
ERROR]   The project com.child.pom:child-pom:2.0.0 (C:\Users\kk\Desktop\POM-Test\child\pom.xml) has 1 error
ERROR]     Non-resolvable parent POM: Could not transfer artifact com.parent.pom:parent-pom:pom:${parentversion} from/to central (http://repo.maven.apache.org/
maven2): Illegal character in path at index 63: http://repo.maven.apache.org/maven2/com/parent/pom/parent-pom/${parentversion}/parent-pom-${parentversion}.pom 
and 'parent.relativePath' points at wrong local POM @ line 5, column 9 -> [Help 2

為什么我無法安裝此pom?

您不能在<parent>部分中擁有屬性

Maven需要在處理構建之前加載父對象以設置所有屬性(用於項目繼承等)

每個項目均由groupId,artifactId和版本標識。 在父級部分,您需要設置這3個屬性以明確標識父級。

為了在您的所有pom上輕松更新版本,請考慮使用release:update-versions插件versions:set目標

編輯:請參見有關類似問題的此其他線程: Maven項目版本繼承-我必須指定父版本嗎?

Maven 3.2.1開始如果您願意,可以在版本中使用屬性${revision}${sha1}${changelist}

因此,您可以像這樣使用它:

  <groupId>com.soebes.examples.j2ee</groupId>
  <artifactId>parent</artifactId>
  <version>${revision}</version>
  <packaging>pom</packaging>

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

您可以像這樣在兒童中使用它:

  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>com.soebes.examples.j2ee</groupId>
    <artifactId>parent</artifactId>
    <version>${revision}</version>
  </parent>

  <artifactId>child</artifactId>

現在您必須這樣調用Maven:

mvn -Drevision=1.2.3-SNAPSHOT clean package
mvn -Drevision=1.2.3 clean package

您可以在這里查看完整的示例。 如果需要更改版本,我建議使用versions-maven-plugin將內部版本更改為所需的任何版本(例如,在CI解決方案中)或在發布版本時,可以使用maven-release-plugin處理這個。 對於開發,您應該使用類似以下內容的方法: 1.2.3.45-SNAPSHOT (強調-SNAPSHOT )來確定當前正在開發中。

此外,不需要給定的<relativePath>../pom.xml</relatvePath> ,因為它是默認設置,因此可以省略。

很重要。 如果您具有如上所述的結構,則子代應始終與父代具有相同的版本(如果像給定示例那樣使用多模塊構建),或者父代(企業pom)應為單獨的項目,這也意味着單獨的git庫。 此外,對於其他解釋,請在這里查看: https : //stackoverflow.com/a/35057833/296328

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM