Maven实现version.txt版本号自增并打包myfolder为tgz的配置方案
Hey there! Since you're new to Maven, let's walk through this task step by step—we'll cover the pom.xml setup, the assembly descriptor file, and exactly which commands to run.
1. 基础pom.xml配置
First, create a pom.xml in your project root directory (the parent folder of src/). This file will define the plugins we need to update the version number and create the TGZ package.
Here's the full configuration with explanations:
<?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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <!-- 基础项目信息,可根据你的需求修改 --> <groupId>com.yourcompany</groupId> <artifactId>your-project</artifactId> <version>1.0.0</version> <packaging>pom</packaging> <build> <plugins> <!-- 插件1:读取并自增version.txt中的rev值 --> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>properties-maven-plugin</artifactId> <version>1.0.0</version> <executions> <!-- 第一步:读取version.txt中的属性 --> <execution> <id>read-version-props</id> <phase>initialize</phase> <goals> <goal>read-project-properties</goal> </goals> <configuration> <files> <file>src/myfolder/version.txt</file> </files> </configuration> </execution> <!-- 第二步:自增rev的值 --> <execution> <id>increment-rev</id> <phase>initialize</phase> <goals> <goal>set-property</goal> </goals> <configuration> <name>rev</name> <value>${rev}</value> <type>int</type> <operation>increment</operation> </configuration> </execution> <!-- 第三步:将更新后的属性写回version.txt --> <execution> <id>write-version-props</id> <phase>initialize</phase> <goals> <goal>write-project-properties</goal> </goals> <configuration> <outputFile>src/myfolder/version.txt</outputFile> <properties> <property>maj:${maj}</property> <property>min:${min}</property> <property>incr:${incr}</property> <property>rev:${rev}</property> </properties> </configuration> </execution> </executions> </plugin> <!-- 插件2:将myfolder打包为TGZ --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.6.0</version> <executions> <execution> <id>create-tgz-package</id> <phase>package</phase> <goals> <goal>single</goal> </goals> <configuration> <!-- 指定assembly描述符文件的位置 --> <descriptors> <descriptor>src/main/assembly/assembly.xml</descriptor> </descriptors> <!-- 设置打包后的文件名,可自定义 --> <finalName>${project.artifactId}-${project.version}</finalName> <!-- 启用压缩 --> <tarLongFileMode>gnu</tarLongFileMode> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
关键说明:
- The
properties-maven-pluginhandles three tasks: reading the existing version values fromversion.txt, incrementing therevnumber, then writing the updated values back to the file. - The
maven-assembly-pluginuses a descriptor file to define what to package and the output format (TGZ).
2. Assembly描述符文件(assembly.xml)
Next, create the assembly descriptor file at src/main/assembly/assembly.xml (you'll need to create the assembly folder first). This file tells Maven exactly which files/directories to include in the TGZ.
Here's the content:
<?xml version="1.0" encoding="UTF-8"?> <assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.0 http://maven.apache.org/xsd/assembly-2.1.0.xsd"> <id>dist</id> <formats> <!-- 指定打包格式为TGZ --> <format>tar.gz</format> </formats> <fileSets> <!-- 包含src/myfolder下的所有文件和子目录 --> <fileSet> <directory>src/myfolder</directory> <!-- 打包后在TGZ中的根目录就是myfolder --> <outputDirectory>/myfolder</outputDirectory> <includes> <include>**/*</include> </includes> </fileSet> </fileSets> </assembly>
3. 执行命令
Now that everything is set up, run these commands in your project root directory (where the pom.xml is located):
第一步:更新version.txt中的rev值
Run this command to trigger the property reading, incrementing, and writing:
mvn initialize
After running this, check src/myfolder/version.txt—you'll see rev:124 instead of rev:123.
第二步:打包为TGZ
Once the version is updated, run this command to create the TGZ package:
mvn package
The TGZ file will be generated in the target/ folder, named something like your-project-1.0.0-dist.tar.gz (based on the finalName in the pom).
或者一步到位:
You can run both steps in one command:
mvn clean package
The clean goal will delete the old target/ folder before creating the new package.
小提示
- Make sure you have Maven installed on your system (run
mvn -vto check). - If you get any plugin version errors, you can update the plugin versions in the
pom.xmlto the latest stable ones. - Double-check the file paths in both the pom and assembly descriptor—Maven is strict about correct paths!
内容的提问来源于stack exchange,提问作者codinglevel1

