我正在使用buildnumber-maven-plugin为我的jar生成序列号。我将在CICD流程中使用它。我遵循了在线提供的所有示例。但是,我可以得到${buildNumber}进行打印,但是在打包罐子时却找不到数字,而我得到了$ {buildNumber}文本。我搜索了Maven LifeCycle,发现需要在其他任何插件之前先将其添加到validate中。但我仍然无法解决问题。

  <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>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
                <version>${scala.plugin.version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <args>
                        <!-- work-around for https://issues.scala-lang.org/browse/SI-8358 -->
                        <arg>-nobootcp</arg>
                    </args>
                </configuration>
            </plugin>

            <!-- Maven Assembly for building Jar With Dependencies and Binary Tarball -->
            <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>

            <!-- Maven Deployment for Deploying Tarball to CICD Process -->

            <plugin>
                <artifactId>maven-deploy-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>deploy-file</goal>
                        </goals>
                        <phase>deploy</phase>
                        <configuration>
                            <repositoryId>${repo.id}</repositoryId>
                            <url>${repo.url}</url>
                            <file>target/${tar.name}.tar.gz</file>
                            <artifactId>scala_cicd</artifactId>
                            <groupId>com.spark.cicd</groupId>
                            <version>${final.version}</version>
                            <packaging>tar.gz</packaging>
                            <generatePom>false</generatePom>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <properties>
        <argLine>-Dfile.encoding=UTF-8 -Dlog4j.skipJansi=false -DmodelLogLevel=${modelLogLevel}</argLine>
        <modelLogLevel>DEBUG</modelLogLevel>

        <tar.name>${artifact.id}_source-${model.version}</tar.name>
        <final.version>${artifact.id}_source-${model.version}</final.version>
        <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>


        <!-- Dependency Versions -->
        <scala.version>2.11.8</scala.version>
        <scala.binary.version>2.11</scala.binary.version>
        <scala.xml.version>1.0.6</scala.xml.version>
        <scala.parser.version>1.0.4</scala.parser.version>
        <scala.swing.version>2.0.0-M2</scala.swing.version>

        <spark.version>2.3.0</spark.version>

        <daedalus.test.version>0.1.0</daedalus.test.version>
        <spark.test.base.version>0.9.0</spark.test.base.version>

        <!-- 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>

        <scala.plugin.version>3.2.1</scala.plugin.version>
        <scalatest.plugin.version>1.0</scalatest.plugin.version>
        <scalariform.plugin.version>0.1.2</scalariform.plugin.version>
        <scoverage.plugin.version>1.3.0</scoverage.plugin.version>
        <findbugs.plugin.version>3.0.4</findbugs.plugin.version>

        <!-- Build Settings -->
        <coverage.minimum>75</coverage.minimum>
        <coverage.fail.on.minimum>true</coverage.fail.on.minimum>
        <findbugs.effort>Max</findbugs.effort>
        <findbugs.fail.threshold>High</findbugs.fail.threshold>

        <group.id>com.test.model_name</group.id>
        <artifact.id>model_abc</artifact.id>
        <!-- Do not change build.type manually. It's injected automatically during the Jenkins build -->
        <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>


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

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


我也检查了类似的问题hereenter 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>


出于同样的原因。

您可以尝试使用this answer中所述的build-helper-maven-plugin。但是,由于model.version用作project.version的值,因此这可能仍然不起作用,因为它是GAV坐标的一部分,因此很早就可以解决。值得一试。

07-27 13:30