[英]How to inject Jenkins environment variable into maven build?
我需要獲取一些Jenkins環境變量,如BUILD_NUMBER和BUILD_URL,並將它們作為變量注入我的Maven Java構建中。
我已將此添加到pom.xml中
<properties>
<jenkins.buildUrl>${env.BUILD_URL}</jenkins.buildUrl>
</properties>
在使用mvn install進行構建時,我正在嘗試獲取變量
private static final String JENKINS_BUILD_URL = System.getProperty("jenkins.buildUrl");
但不幸的是結果是空的......
我做錯了什么?
猜猜你試圖將這個值讀入你的單元測試? 然后你必須配置surefire插件的環境變量:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<environmentVariables>
<jenkins.buildUrl>${env.BUILD_URL}</jenkins.buildUrl>
</environmentVariables>
</configuration>
</plugin>
如本文檔中所述: http : //maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#environmentVariables
請注意,可以在其他插件中執行相同操作,例如Maven Tomcat插件。
這是我在我的應用程序中實現的方法
在pom.xml中添加屬性
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jenkins.buildNumber>${env.BUILD_NUMBER}</jenkins.buildNumber>
</properties>
在.properties文件中創建一個新屬性
jenkins.build.number = ${jenkins.buildNumber}
通過添加啟用maven中的過濾
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
然后使用以下代碼從java中讀取它
properties.load(MyClass.class.getResourceAsStream("/project.properties"));
version = properties.getProperty("jenkins.build.number");
將maven目標更改為clean package -Denv.BUILD_NUMBER = $ {BUILD_NUMBER}
我在我們的應用程序中顯示了這個頁面。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.