簡體   English   中英

使用Jenkinsfile,Maven和Java從參數化的Jenkins項目傳遞變量

[英]Passing variables from parameterized Jenkins project using Jenkinsfile, Maven, and Java

我有一個參數化的Pipeline Jenkins項目連接到我從https://github.com/jenkins-docs/simple-java-maven-app分叉的Maven項目。 我試圖通過一個所謂的“平台”我在詹金斯項目設置參數: 這里顯示

在我自己的大型項目上實現此功能之前,我想看看是否有可能通過Maven將參數從Jenkins傳遞到Java應用程序。 我嘗試了下面的代碼中看到的一些解決方案。

但是,無論我嘗試什么,在運行System.getProperty("platform")時仍然會得到null。 我不確定我可能做錯了什么。 我是否缺少某些內容,或者我只是無法識別某些語法錯誤?

下面的代碼段:

Jenkinsfile

pipeline {
    agent {
        docker {
            image 'maven:3-alpine' 
            args '-v /root/.m2:/root/.m2' 
        }
    }
    stages {
        stage('Build') { 
            steps {
                sh "mvn -Dplatform=${params.Platform} -B clean package" 
            }
        }
        stage('Deliver') {
            steps {
                sh './jenkins/scripts/deliver.sh'
            }
        }
    }
}

delivery.sh我添加了echo“ $ {env.platform}”來查看返回的內容,但出現錯誤-./jenkins/scripts/deliver.sh:第2行:$ {env.platform}:替代錯誤

#!/usr/bin/env bash
echo "${env.platform}"
set -x
mvn jar:jar install:install help:evaluate -Dexpression=project.name
set +x

echo 'The following complex command extracts the value of the <name/> element'
echo 'within <project/> of your Java/Maven project''s "pom.xml" file.'
set -x
NAME=`mvn help:evaluate -Dexpression=project.name | grep "^[^\[]"`
set +x

echo 'The following complex command behaves similarly to the previous one but'
echo 'extracts the value of the <version/> element within <project/> instead.'
set -x
VERSION=`mvn help:evaluate -Dexpression=project.version | grep "^[^\[]"`
set +x

echo 'The following command runs and outputs the execution of your Java'
echo 'application (which Jenkins built using Maven) to the Jenkins UI.'
set -x
java -jar target/${NAME}-${VERSION}.jar

Java主

public static void main(String[] args) {
        String test = System.getProperty("platform");
        System.out.println(test);
}

的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>my-app</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <!-- Build an executable JAR -->
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.0.2</version>
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <classpathPrefix>lib/</classpathPrefix>
              <mainClass>com.mycompany.app.App</mainClass>
            </manifest>
          </archive>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

更新-找到的解決方案:遵循ИлиянМихайлов的解決方案,它起作用了! 同樣,在Java類中,我不得不使用System.getenv("Platform")而不是使用System.getProperty("platform") System.getenv("Platform")

您嘗試在錯誤的位置設置參數(在構建步驟中)。 在Maven中,每次運行都是獨立的,並且不存儲有關參數的任何信息。 mvn jar:jar install:install help:evaluate -Dexpression = project.name -Dplatform =“ $ 1”->此參數必須在Jenkins作業sh'中作為參數發送。 /jenkins/scripts/deliver.sh $ {params.Platform}'}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM