How to (implicitly) pass environment variables to Maven build in gitlab-ci.yml for a Spring Boot application?

huangapple go评论59阅读模式
英文:

How to (implicitly) pass environment variables to Maven build in gitlab-ci.yml for a Spring Boot application?

问题

在一个gitlab-ci.yml文件中,我正在使用Maven构建一个Spring Boot应用程序。要替换application.properties中的占位符@MY_PROP@(使用Maven资源插件),需要将MY_PROP作为-D参数传递给mvn命令。由于我的构建中有许多参数,是否有一种方法可以在不显式通过-D参数传递每个单独的环境参数的情况下将环境变量传递给Maven?

application.properties
    my.prop=@MY_PROP@

gitlab环境变量
    MY_PROP=4711

gitlab-ci.yml
Build:
    image: maven:3-jdk-11
    stage: build
    script:
        - mvn clean package deploy -s ci_settings.xml --batch-mode -DMY_PROP=$MY_PROP
英文:

In a gitlab-ci.yml I'm building a Spring Boot app using Maven. To replace placeholder @MY_PROP@ in application.properties (using the Maven resources plugin) MY_PROP is passed as -D parameter to the mvn command.
Since there are a lot of parameters in my build, is there a way to pass environment varaibles to Maven without explicitly passing each individual env parameter via -D?

application.properties 
	my.prop=@MY_PROP@

gitlab env variable 
	MY_PROP=4711

gitlab-ci.yml
	Build:
		image: maven:3-jdk-11
		stage: build
		script:
			- mvn clean package deploy -s ci_settings.xml --batch-mode -DMY_PROP=$MY_PROP

答案1

得分: 1

From the Maven reference: https://books.sonatype.com/mvnref-book/reference/resource-filtering-sect-properties.html

You can do:

> env.*
>
> 环境变量,如 PATHM2_HOME,可以使用 env.* 前缀引用。

So in your pom.xml you can reference them as:

${env.MY_PROP}

In your case, to replace with resource plugin you can write:

my.prop=@env.MY_PROP@
英文:

From the Maven reference: https://books.sonatype.com/mvnref-book/reference/resource-filtering-sect-properties.html

You can do:

> env.*
>
> Environment variables like PATH and M2_HOME can be referenced using the env.* prefix.

So in your pom.xml you can reference them as:

${env.MY_PROP}

In your case, to replace with resource plugin you can write:

my.prop=@env.MY_PROP@

huangapple
  • 本文由 发表于 2023年2月24日 15:41:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75553783.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定