gpt4 book ai didi

java - 打包 jar 时 Maven 无法检索 buildNumber

转载 作者:行者123 更新时间:2023-11-30 05:29:44 24 4
gpt4 key购买 nike

我正在使用 buildnumber-maven-plugin 为我的 jar 生成序列号。我将在 CICD 过程中使用它。

我遵循了网上提供的所有示例。但是,我可以获取 ${buildNumber} 来打印它,但是在打包 jar 时,我无法获取该编号,并且我得到了 ${buildNumber} 文本。我搜索了 Maven LifeCycle,发现我需要在任何其他插件之前将其添加到验证中。但我仍然无法解决这个问题。

  <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>${group.id}</groupId>
<artifactId>${artifact.id}</artifactId>
<version>${model.version}</version>
<profiles>
<profile>
<id>scala-2.11</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<dependencies>

......
</dependencies>
</profile>
</profiles>
<scm>
<connection>scm:svn:http://127.0.0.1/dummy</connection>
<developerConnection>scm:svn:https://127.0.0.1/dummy</developerConnection>
<tag>HEAD</tag>
<url>http://127.0.0.1/dummy</url>
</scm>

<build>
<sourceDirectory>src/main/scala</sourceDirectory>
<finalName>${artifact.id}.${model.version}</finalName>
<plugins>
<!-- buildnumber-maven-plugin for automatic increment the version number -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>1.4</version>
<configuration>
<buildNumberPropertiesFileLocation>${build.number.dir}</buildNumberPropertiesFileLocation>
<revisionOnScmFailure>no.scm.config.in.pom</revisionOnScmFailure>
<doCheck>true</doCheck>
<doUpdate>true</doUpdate>
<format>{0,number}</format>
<items>
<item>buildNumber</item>
</items>
</configuration>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>create</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>${build.number.dir}</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<echo>current build number is "${buildNumber}"</echo>
</target>
</configuration>
</execution>
</executions>
</plugin>
<!-- ================== ;-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin.version}</version>
<configuration>
<source>${java.source.version}</source>
<target>${java.source.version}</target>
<skipMain>true</skipMain> <!-- skip compile -->
<skip>true</skip> <!-- skip testCompile -->
</configuration>
</plugin>


<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>${maven.assembly.plugin.version}</version>
<executions>
<execution>
<id>jar-with-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>
src/main/assembly/assembly-jar.xml
</descriptor>
</descriptors>
<finalName>${artifact.id}</finalName>
</configuration>
</execution>
<execution>
<id>bin</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>
src/main/assembly/assembly-bin.xml
</descriptor>
</descriptors>
<finalName>${tar.name}</finalName>
</configuration>
</execution>
</executions>
</plugin>



<properties>
<argLine>-Dfile.encoding=UTF-8 -Dlog4j.skipJansi=false -DmodelLogLevel=${modelLogLevel}</argLine>
<modelLogLevel>DEBUG</modelLogLevel>
<build.number.dir>${project.basedir}/buildNumber.properties</build.number.dir>
<java.source.version>1.8</java.source.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>


<!-- Plugin Versions -->
<maven.compiler.plugin.version>3.6.2</maven.compiler.plugin.version>
<maven.reports.plugin.version>2.9</maven.reports.plugin.version>
<maven.assembly.plugin.version>3.1.0</maven.assembly.plugin.version>
<surefire.plugin.version>2.7</surefire.plugin.version>

<group.id>com.test.model_name</group.id>
<artifact.id>proj_abc</artifact.id>
<build.type>snapshot</build.type>
<major.minor.version>0.1</major.minor.version>
<!-- here I am trying to retrieve the actual number-->
<build.number>${buildNumber}</build.number>
<model.version>${major.minor.version}.${build.number}.${build.type}</model.version>
</properties>

</project>

注意:如果我运行验证,我可以找到下面的回显消息,这意味着我可以从属性文件中正确获取数字。如果我运行 package 或 run,我会得到 ${artifact.id}_source-.0.1.${buildNumber}.snapshot ,但没有编号。

main:
[echo] current build number is "90"

我也检查了这个类似的问题here & enter link description here和其他链接,但无法找出问题所在。

最佳答案

我认为这里发生的是 Maven 在构建过程开始时解析属性。

<build.number>${buildNumber}</build.number>
<model.version>${major.minor.version}.${build.number}.${build.type}</model.version>

当它解析 build.number 时,${buildNumber} 没有值。因此,变量名称保持不变。我怀疑如果您将 model.version 修改为:

,您也会遇到同样的问题:
<model.version>${major.minor.version}.${buildNumber}.${build.type}</model.version>

出于同样的原因。

您可以尝试使用 build-helper-maven-plugin,如 this answer 中所述。 。然而,这仍然可能不起作用,因为 model.version 被用作 project.version 的值,它很早就被解析了,因为它是 GAV 坐标的一部分。不过还是值得一试。

关于java - 打包 jar 时 Maven 无法检索 buildNumber,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57807090/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com