如何在 JAR 文件名中包含内部版本号?
How can I include a build number in the JAR file name?
我有一个项目,它是使用 Maven 程序集插件构建的。
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
我有一个文件 BUILD.txt
,其中包含当前版本号。
当我调用 mvn assembly:single
时,我希望 maven-assembly-plugin
生成一个名为 myproduct-1.0-SNAPSHOT.BUILD.jar
的 JAR 文件(具有所有依赖项),其中 BUILD 是来自 BUILD.txt
的文本(即如果 BUILD.txt
包含 172,则结果 JAR 应称为 myproduct-1.0-SNAPSHOT.172.jar
).
如何从 BUILD.txt
中读取文本以便在程序集插件的 finalName
设置中使用它?
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<finalName>${project.build.finalName}.${build}.jar</finalName
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
方法一
- 使用Maven Properties Plugin
- 将您的
BUILD.txt
更改为包含 buildNumber=NNN
- 对
<finalName>
使用属性${buildNumber}
方法二
- 假设 您使用像 SVN 或 Git
这样的 SCM
- 不再需要使用
BUILD.txt
- 使用Maven Build Number Plugin
- 对
<finalName>
使用属性${buildNumber}
试试这个
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<finalName>${project.build.finalName}.${build}.jar</finalName
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<appendAssemblyId>false</appendAssemblyId>
</plugin>
使用 appendAssemblyId
您将避免使用 jar-with-dependencies
后缀。您应该生成 BUILD.txt
作为属性文件,然后在 pom.xml
中将其作为普通属性文件访问
你可以这样做:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
...
将其放入您的 pom(构建部分),然后配置它以访问您的 scm。
然后定义您的 bundleFileName,例如像这样
<bundleFileName>${project.artifactId}-${project.version}.jar</bundleFileName>
如果您无法控制此构建文件的创建并且无法更改其内容,一种方法是使用带有 maven-antrun-plugin
的 Ant 任务读取文件,存储内容在 属性 中,然后使用 属性 作为最终名称。
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>prepare-package</phase>
<configuration>
<target>
<!-- this task will read the content of the file and store it inside the "build" property -->
<loadfile property="build" srcFile="BUILD.txt" />
</target>
<exportAntProperties>true</exportAntProperties> <!-- Ant properties are not exported as Maven properties by default so we need to add it -->
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<finalName>${project.build.finalName}.${build}.jar</finalName
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<phase>package</phase> <!-- bind the maven-assembly-plugin to the package phase instead -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
运行 Maven mvn clean install
将为程序集生成正确的名称。
但是请注意,您可以使用已经自动处理内部版本号的 buildnumber-maven-plugin
而不是这样做,从而使您免于维护 BUILD.txt
文件的负担。
我有一个项目,它是使用 Maven 程序集插件构建的。
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
我有一个文件 BUILD.txt
,其中包含当前版本号。
当我调用 mvn assembly:single
时,我希望 maven-assembly-plugin
生成一个名为 myproduct-1.0-SNAPSHOT.BUILD.jar
的 JAR 文件(具有所有依赖项),其中 BUILD 是来自 BUILD.txt
的文本(即如果 BUILD.txt
包含 172,则结果 JAR 应称为 myproduct-1.0-SNAPSHOT.172.jar
).
如何从 BUILD.txt
中读取文本以便在程序集插件的 finalName
设置中使用它?
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<finalName>${project.build.finalName}.${build}.jar</finalName
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
方法一
- 使用Maven Properties Plugin
- 将您的
BUILD.txt
更改为包含buildNumber=NNN
- 对
<finalName>
使用属性
${buildNumber}
方法二
- 假设 您使用像 SVN 或 Git 这样的 SCM
- 不再需要使用
BUILD.txt
- 使用Maven Build Number Plugin
- 对
<finalName>
使用属性
${buildNumber}
试试这个
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<finalName>${project.build.finalName}.${build}.jar</finalName
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<appendAssemblyId>false</appendAssemblyId>
</plugin>
使用 appendAssemblyId
您将避免使用 jar-with-dependencies
后缀。您应该生成 BUILD.txt
作为属性文件,然后在 pom.xml
你可以这样做:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
...
将其放入您的 pom(构建部分),然后配置它以访问您的 scm。 然后定义您的 bundleFileName,例如像这样
<bundleFileName>${project.artifactId}-${project.version}.jar</bundleFileName>
如果您无法控制此构建文件的创建并且无法更改其内容,一种方法是使用带有 maven-antrun-plugin
的 Ant 任务读取文件,存储内容在 属性 中,然后使用 属性 作为最终名称。
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>prepare-package</phase>
<configuration>
<target>
<!-- this task will read the content of the file and store it inside the "build" property -->
<loadfile property="build" srcFile="BUILD.txt" />
</target>
<exportAntProperties>true</exportAntProperties> <!-- Ant properties are not exported as Maven properties by default so we need to add it -->
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<finalName>${project.build.finalName}.${build}.jar</finalName
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<phase>package</phase> <!-- bind the maven-assembly-plugin to the package phase instead -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
运行 Maven mvn clean install
将为程序集生成正确的名称。
但是请注意,您可以使用已经自动处理内部版本号的 buildnumber-maven-plugin
而不是这样做,从而使您免于维护 BUILD.txt
文件的负担。