Java Spring Boot 控制目标 JAR 文件名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37698473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:34:32  来源:igfitidea点击:

Spring Boot control target JAR file name

javamavenspring-boot

提问by Ido Barash

My Spring Boot project has build description:

我的 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>

I want my JAR file name to be app-1.0-SNAPSHOT.jarin one branch and 1.0-RELEASE.jarin another, controlled by Jenkins (using some kind of mvn settings or JVM argument such as -D..

我希望我的 JAR 文件名app-1.0-SNAPSHOT.jar在一个分支和1.0-RELEASE.jar另一个分支中,由 Jenkins 控制(使用某种 mvn 设置或 JVM 参数,例如 -D..

Can I do this?

我可以这样做吗?

采纳答案by sendon1982

You can specify with the maven boot plugin:

您可以使用 maven 启动插件指定:

In this case, it will be NewJarName.jar

在这种情况下,它将是 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>

回答by Do Nhu Vy

So simple, In one branch, you have pom.xmlwith

就这么简单,在一个分支,你必须pom.xml

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


In other branch, you have pom.xmlwith


在其他的分支,你必须pom.xml

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

回答by avi.elkharrat

You can propagate the version of the project to your build name like this:

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

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

or the version of your parent project if you have one:

或者你的父项目的版本,如果你有的话:

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

Then you would keep track of you project version rather than the build name.

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

However, note that managing the build verson in SCM using branches is a pain in the neck and error prone. It is rather recommanded that your code repository woud be agnostic of your build version.

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

A possible alternative would be to use some release management tool, like maven release plugin, or even more simple maven version.

一种可能的替代方法是使用一些发布管理工具,例如maven release plugin,甚至更简单的maven version

Example:

例子:

Here I'll give and example using maven verion.

在这里,我将给出使用maven verion.

Say you're using SCM tool (it could be git) and a build factory (like Jenkinsor any other tool). Say you have a job to build and deploy snapshotsand another one for releases.

假设您正在使用 SCM 工具(可能是git)和构建工厂(如Jenkins或任何其他工具)。假设您有一份工作来构建和部署快照,另一份工作是发布

In the snapshotjob, you can set-up a pre-build task with the following maven target:

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

versions:set -DnewVersion=app-1.0-SNAPSHOT

and the following in the releasejob:

以及发布作业中的以下内容:

versions:set -DnewVersion=app-1.0-RELEASE

Now doing this is OK, because you are only doing it locally and never have to manage the build version in your code.

现在这样做是可以的,因为您只是在本地进行,而不必在代码中管理构建版本。

Now, you can tag your (release) version after having applied maven versionand build successfuly (hopefuly including unit, integration and functional tests). This way you may keep track exactly of the code that has been deployed on each release.

现在,您可以在maven version成功应用和构建(希望包括单元、集成和功能测试)后标记您的(发布)版本。通过这种方式,您可以准确跟踪每个版本上部署的代码。

Tip!!Space is money! Do yourself a favour: clean your snapshot repository regularly. Creating a job that does so every once in a while shouldn't be to difficult.

提示!!空间就是金钱!帮自己一个忙:定期清理您的快照存储库。创造一份每隔一段时间这样做的工作应该不难。