Spring Boot控制目标JAR文件名

问题描述 投票:0回答:4

我的 Spring Boot 项目有构建描述:

<build>

    <plugins>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.18.1</version>
            <dependencies>
                <dependency>
                    <groupId>org.apache.maven.surefire</groupId>
                    <artifactId>surefire-junit47</artifactId>
                    <version>2.18.1</version>
                </dependency>
            </dependencies>
        </plugin>

        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>${spring-boot.version}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <mainClass>com.app.MainClass</mainClass>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>

    </plugins>
</build>

我希望我的 JAR 文件名在一个分支中为

app-1.0-SNAPSHOT.jar
,在另一个分支中为
1.0-RELEASE.jar
,由 Jenkins 控制(使用某种 mvn 设置或 JVM 参数,例如 -D..

我可以这样做吗?

java maven spring-boot
4个回答
53
投票

很简单,在一个分支中,你有

pom.xml

<build>
  <finalName>app-1.0-SNAPSHOT</finalName>
</build>


在其他分支,您有

pom.xml

<build>
  <finalName>1.0-RELEASE</finalName>
</build>

24
投票

您可以使用maven boot插件指定artefact-name:

在这种情况下,它将是

NewJarName.jar

        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>repackage</id>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                    <configuration>
                        <finalName>NewJarName</finalName>
                    </configuration>
                </execution>
            </executions>
        </plugin>

22
投票

您可以将项目的版本传播到您的构建名称,如下所示:

<build>
    <finalName>app-${project.version}</finalName>
</build>

或者您的父项目的版本(如果您有的话):

<build>
    <finalName>app-${parent.version}</finalName>
</build>

然后您将跟踪项目版本而不是构建名称。

但是,请注意,使用分支管理 SCM 中的构建版本是一件令人头疼的事情,并且容易出错。强烈建议您的代码存储库与您的构建版本无关。

一个可能的替代方案是使用一些发布管理工具,例如

maven release plugin
,或更简单的
maven version

示例:

这里我将给出使用

maven verion
的示例。

假设您正在使用 SCM 工具(可能是

git
)和构建工厂(如
Jenkins
或任何其他工具)。假设您有一份工作用于构建和部署 snapshots,另一份工作用于 releases

snapshot 作业中,您可以使用以下 Maven 目标设置预构建任务:

versions:set -DnewVersion=app-1.0-SNAPSHOT

以及release作业中的以下内容:

versions:set -DnewVersion=app-1.0-RELEASE

现在这样做就可以了,因为您只是在本地执行此操作,而不必在代码中管理构建版本。

现在,您可以在应用

maven version
并成功构建后标记您的(发布)版本(希望包括单元、集成和功能测试)。这样您就可以准确跟踪每个版本上部署的代码。

提示!! 空间就是金钱!帮自己一个忙:定期清理快照存储库。创建一个偶尔这样做的工作应该不会太困难。


0
投票

Maven:pom.xml

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    **<finalName>spring-boot-new-jar-file-name</finalName>**
                </configuration>
            </plugin>
        </plugins>
    </build>
    

在文件名中添加版本 spring-boot-new-jar-文件名-${版本}

© www.soinside.com 2019 - 2024. All rights reserved.