spring boot 发布自动生成svn版本号

通过Jenkins构建发布spring boot项目时,常常有需求,需要把Svn的版本号更新到项目的版本上,通过有两种解决方案:

1. 通过shell命令对配置文件中的指定字符进行替换,

如:

配置文件

app-config:
  isDev: 1
  version: 10.0.dev.312

shell命令

sed -i "s/dev/$BUILD_NUMBER/g" src/main/resources/application.properties

2.使用spring boot插件在构建compile时,对版本号的指定字符进行自动替换:

application.yml

app-config:
  version: 10.0.${prefix.revision}.312

POM文件添加plugin:

<!--svn版本号-->
            <plugin>
                <groupId>com.google.code.maven-svn-revision-number-plugin</groupId>
                <artifactId>maven-svn-revision-number-plugin</artifactId>
                <version>1.7</version>
                <configuration>
                    <verbose>true</verbose>
                    <entries>
                        <entry>
                            <prefix>prefix</prefix>
                            <depth>empty</depth>
                        </entry>
                    </entries>
                </configuration>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>revision</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.tmatesoft.svnkit</groupId>
                        <artifactId>svnkit</artifactId>
                        <version>1.8.5</version>
                    </dependency>
                </dependencies>
            </plugin>
            <!--替换配置文件变量-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>default-resources</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>target/classes</outputDirectory>
                            <useDefaultDelimiters>true</useDefaultDelimiters>
                            <resources>
                                <resource>
                                    <directory>src/main/resources</directory>
                                    <filtering>true</filtering>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

点击compile:

注:通常开发时,是不需要进行compile的,所以,为避免debug出错,可以添加application-dev.yml文件

app-config:
  version: 10.0.dev.312

在target文件夹生成的application.yml可以看到版本号已经改为svn号了。

猜你喜欢

转载自www.cnblogs.com/hankuikui/p/12055975.html