【问题标题】:Maven build for Spring boot Application for windows and linux platforms适用于 windows 和 linux 平台的 Spring boot 应用程序的 Maven 构建
【发布时间】:2018-01-22 01:53:43
【问题描述】:

我需要使用 Maven 构建为 windows 和 linux 平台生成我的 Spring Boot 应用程序二进制文件。

我计划为 windows 和 linux 设置两个不同的 application.properties,即

  • 应用程序-windows.properties
  • application-linux.properties

在 pom.xml 中我会有这个

<profile>
    <id>dev</id>
    <properties>
        <activatedProperties>windows</activatedProperties>
    </properties>
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>
</profile>
<profile>
    <id>release</id>
    <properties>
        <activatedProperties>linux</activatedProperties>
    </properties>
</profile>

Maven 构建将使用 Jenkins 触发。但是这样我一次只能激活一个配置文件,因此它需要两个用于 windows 和 linux 的 jenkins 项目。 有没有更好的方法呢?

【问题讨论】:

  • 为什么在构建时需要不同的属性?
  • 我需要基于平台具有不同的属性值,例如linux 中的日志目录将在 /var/log/.. 但在 Windows 的情况下会有所不同..
  • 在我看来,此类属性文件不应成为将要交付的工件的一部分。此类属性应由目标系统提供。
  • 但我不想使用任何外部属性文件。我的属性文件将成为构建 jar 的一部分。
  • 只需在您的应用程序中提供两个属性文件(您不应该为不同的环境构建不同的工件)然后使用 --spring.profiles.active=&lt;your-environment&gt; 启动您的应用程序或创建一个 ApplicationInitializer 来设置。添加基于活动的配置文件在检测到的环境中。

标签: java maven jenkins spring-boot


【解决方案1】:

应该通过在启动 spring boot 应用程序时提供 build 参数来选择配置文件。

-Dspring.profles.active=$profile

如果您使用jenkins启动应用程序,只需在作业执行期间添加一个选择配置文件的选项,该选项可以向应用程序添加参数。

https://docs.spring.io/spring-boot/docs/1.1.6.RELEASE/reference/html/boot-features-profiles.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多