使用Jenkinsfile,Maven和Java从参数化的Jenkins项目传递变量

时间:2018-09-23 21:53:10

标签: java macos maven jenkins

我有一个参数化的Pipeline Jenkins项目,该项目连接到我从https://github.com/jenkins-docs/simple-java-maven-app分叉的Maven项目。我正在尝试传递在詹金斯项目中设置的名为“平台”的参数: shown here

在我自己的大型项目上实现此功能之前,我想看看是否有可能通过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'
            }
        }
    }
}

deliver.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.getProperty("platform")而不是使用System.getenv("Platform")

1 个答案:

答案 0 :(得分:0)

您尝试在错误的位置设置参数(在构建步骤中)。在Maven中,每次运行都是独立的,并且不存储有关参数的任何信息。 mvn jar:jar install:install help:evaluate -Dexpression = project.name -Dplatform =“ $ 1”->此参数必须在Jenkins作业sh'中作为参数发送。 /jenkins/scripts/deliver.sh $ {params.Platform}'}

相关问题