IT박스

Maven 프로젝트 버전 상속-상위 버전을 지정해야합니까?

itboxs 2020. 6. 3. 21:50
반응형

Maven 프로젝트 버전 상속-상위 버전을 지정해야합니까?


부모 프로젝트 : A, 하위 프로젝트 : B

A / pom.xml :

<groupId>com.dummy.bla</groupId>
<artifactId>parent</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>pom</packaging>

그리고 B / pom.xml에는 다음이 있습니다.

    <parent>
        <groupId>com.dummy.bla</groupId>
        <artifactId>parent</artifactId>
        <version>0.1-SNAPSHOT</version>     
    </parent>

    <groupId>com.dummy.bla.sub</groupId>
    <artifactId>kid</artifactId>

B가 부모로부터 버전을 상속 받기를 원하므로 필자의 경우 유일한 위치 0.1-SNAPSHOTA/pom.xml입니다. 그러나 부모 섹션 <version>0.1-SNAPSHOT</version>에서 B/pom.xml에서를 제거하면 maven은 부모의 누락 버전에 대해 불평합니다.

두 치어 리딩 ${project.version}을 피하기 위해 내가 사용할 수있는 방법 이나 이와 비슷한 것이 01.-SNAPSHOT있습니까?


편집 : Maven 3.5.0부터 ${revision}자리 표시자를 사용하는 좋은 해결책이 있습니다. 자세한 내용은 FrVaBe의 답변 을 참조하십시오. 이전 Maven 버전의 경우 아래의 원래 답변을 참조하십시오.


아닙니다. 항상 부모의 버전을 지정해야합니다. 다행히도 대부분의 경우 바람직한 버전으로 모듈 버전으로 상속됩니다. 또한이 부모의 버전 선언은 Maven Release Plugin에 의해 자동으로 충돌하므로 실제로 릴리스 또는 범핑 버전에 Maven Release Plugin을 사용하는 한 2 곳의 버전이있는 것은 문제가되지 않습니다.

이 동작이 실제로 문제가되지 않고 더 많은 유연성을 제공하는 경우가 있습니다. 때로는 이전 부모 버전을 사용하여 상속하려고하지만 때로는 주류가 아닙니다.


Maven은 그런 식으로 작동하도록 설계되지 않았지만이 목표를 달성하기위한 해결 방법이 있습니다 (부작용이있을 수 있으므로 시도해보십시오). 트릭은 자식 프로젝트에 순수한 maven 좌표 대신 상대 경로 를 통해 부모를 찾도록하고 속성에서 버전 번호를 외부화하는 것입니다.

부모 치어

<groupId>com.dummy.bla</groupId>
<artifactId>parent</artifactId>
<version>${global.version}</version>
<packaging>pom</packaging>

<properties>
   <!-- Unique entry point for version number management --> 
   <global.version>0.1-SNAPSHOT</global.version>
</properties>

어린이 치어

<parent>
   <groupId>com.dummy.bla</groupId>
   <artifactId>parent</artifactId>
   <version>${global.version}</version>
   <relativePath>..</relativePath>    
</parent>

<groupId>com.dummy.bla.sub</groupId>
<artifactId>kid</artifactId>

필자는 빌드 초기에 maven이 많은 경고를 기록한다는 사실을 제외하고는 특별한 문제없이 내 프로젝트 중 하나에 대해 잠시 동안 그 트릭을 사용했는데, 이는 매우 우아하지 않습니다.

편집하다

maven 3.0.4는 더 이상 그러한 구성을 허용하지 않는 것 같습니다.


버전 IMO를 업데이트하는 가장 쉬운 방법 :

$ mvn versions:set -DgenerateBackupPoms=false

(root / parent pom 폴더에서 수행하십시오).

POM이 구문 분석되고 설정할 버전을 묻는 메시지가 표시됩니다.


Since Maven 3.5.0 you can use the ${revision} placeholder for that. The use is documented here: Maven CI Friendly Versions.

In short the parent pom looks like this (quoted from the Apache documentation):

<project>
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.apache</groupId>
    <artifactId>apache</artifactId>
    <version>18</version>
  </parent>
  <groupId>org.apache.maven.ci</groupId>
  <artifactId>ci-parent</artifactId>
  <name>First CI Friendly</name>
  <version>${revision}</version>
  ...
  <properties>
    <revision>1.0.0-SNAPSHOT</revision>
  </properties>
  <modules>
    <module>child1</module>
    ..
  </modules>
</project>

and the child pom like this

<project>
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.apache.maven.ci</groupId>
    <artifactId>ci-parent</artifactId>
    <version>${revision}</version>
  </parent>
  <groupId>org.apache.maven.ci</groupId>
  <artifactId>ci-child</artifactId>
   ...
</project>

You also have to use the Flatten Maven Plugin to generate pom documents with the dedicated version number included for deployment. The HowTo is documented in the linked documentation.

Also @khmarbaise wrote a nice blob post about this feature: Maven: POM Files Without a Version in It?


As Yanflea mentioned, there is a way to go around this.

In Maven 3.5.0 you can use the following way of transferring the version down from the parent project:

Parent POM.xml

<project ...>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mydomain</groupId>
    <artifactId>myprojectparent</artifactId>
    <packaging>pom</packaging>
    <version>${myversion}</version>
    <name>MyProjectParent</name>

    <properties>
        <myversion>0.1-SNAPSHOT</myversion>
    </properties>

    <modules>
        <module>modulefolder</module>
    </modules>
    ...
</project>

Module POM.xml

<project ...>
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.mydomain</groupId>
        <artifactId>myprojectmodule</artifactId>
        <version>${myversion}</version> <!-- This still needs to be set, but you can use properties from parent -->
    </parent>

    <groupId>se.car_o_liner</groupId>
    <artifactId>vinno</artifactId>
    <packaging>war</packaging>
    <name>Vinno</name>
    <!-- Note that there's no version specified; it's inherited from parent -->
    ...
</project>

You are free to change myversion to whatever you want that isn't a reserved property.


You could also use:

$ mvn release:update-versions -DdevelopmentVersion={version}

to update the version numbers in your POMs.


eFox's answer worked for a single project, but not when I was referencing a module from another one (the pom.xml were still stored in my .m2 with the property instead of the version).

However, it works if you combine it with the flatten-maven-plugin, since it generates the poms with the correct version, not the property.

The only option I changed in the plug-in definition is the outputDirectory, it's empty by default, but I prefer to have it in target, which is set in my .gitignore configuration:

<plugin>
   <groupId>org.codehaus.mojo</groupId>
   <artifactId>flatten-maven-plugin</artifactId>
   <version>1.0.1</version>
   <configuration>
      <updatePomFile>true</updatePomFile>
      <outputDirectory>target</outputDirectory>
   </configuration>
   <executions>
      <execution>
         <id>flatten</id>
         <phase>process-resources</phase>
         <goals>
            <goal>flatten</goal>
         </goals>
      </execution>
   </executions>
</plugin>

The plug-in configuration goes in the parent pom.xml


Use mvn -N versions:update-child-modules to update child pom`s version

https://www.mojohaus.org/versions-maven-plugin/examples/update-child-modules.html

참고URL : https://stackoverflow.com/questions/10582054/maven-project-version-inheritance-do-i-have-to-specify-the-parent-version

반응형