简体   繁体   English

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

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

I have a parameterized Pipeline Jenkins project connected to a Maven project that I forked from https://github.com/jenkins-docs/simple-java-maven-app . 我有一个参数化的Pipeline Jenkins项目连接到我从https://github.com/jenkins-docs/simple-java-maven-app分叉的Maven项目。 I am trying to pass a parameter called "Platform" I have set in the Jenkins Project: shown here 我试图通过一个所谓的“平台”我在詹金斯项目设置参数: 这里显示

Before implementing this on my own, larger project, I wanted to see if it was possible to pass a parameter from Jenkins to the Java application via Maven. 在我自己的大型项目上实现此功能之前,我想看看是否有可能通过Maven将参数从Jenkins传递到Java应用程序。 I've tried some solutions seen in below code. 我尝试了下面的代码中看到的一些解决方案。

However, no matter what I try, I still get null when running System.getProperty("platform") . 但是,无论我尝试什么,在运行System.getProperty("platform")时仍然会得到null。 I'm not sure what I could be doing incorrectly. 我不确定我可能做错了什么。 Am I missing something or is there some incorrect syntax I'm just not identifying? 我是否缺少某些内容,或者我只是无法识别某些语法错误?

Code snippets below: 下面的代码段:

Jenkinsfile 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 I added echo "${env.platform}" to see what it returned and I get an error - ./jenkins/scripts/deliver.sh: line 2: ${env.platform}: bad substitution 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 main Java主

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

pom.xml 的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>

UPDATE - solution found: Followed Илиян Михайлов's solution and it worked! 更新-找到的解决方案:遵循ИлиянМихайлов的解决方案,它起作用了! Also, in the Java class, instead of using System.getProperty("platform") I had to use System.getenv("Platform") . 同样,在Java类中,我不得不使用System.getenv("Platform")而不是使用System.getProperty("platform") System.getenv("Platform")

You try to set a parameter in the wrong place (in the build step). 您尝试在错误的位置设置参数(在构建步骤中)。 In maven, each run is independently and not store any information about the parameters. 在Maven中,每次运行都是独立的,并且不存储有关参数的任何信息。 The parameter must be sent when is needed may be on this line mvn jar:jar install:install help:evaluate -Dexpression=project.name -Dplatform="$1" -> this should be sent as parameter from the Jenkins job sh './jenkins/scripts/deliver.sh ${params.Platform}'} 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