Как динамически передать параметр из класса pom. xml в java при выполнении в jenkins - PullRequest
0 голосов
/ 26 февраля 2020

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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>Imageproceesing</groupId>
  <artifactId>beginner</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>beginner</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <environment>${env}</environment>
  </properties>

  <dependencies>
  <!-- https://mvnrepository.com/artifact/net.sourceforge.tess4j/tess4j -->
<dependency>
    <groupId>net.sourceforge.tess4j</groupId>
    <artifactId>tess4j</artifactId>
    <version>4.4.1</version>
</dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Среда - это параметр, который я собираюсь передать через

mvn clean test -Denv=dev

в классе тестирования

public class AppTest 
{
    @Test
    public void demo()
    {
        String environment=System.getProperty("environment");
        if(environment.equals("dev"))
           //...rest code here
        System.out.println("check the code");
    }
}

I мне нужно передать переменную окружения из командной строки в pom и Java, но каждый раз, когда я получаю нулевое значение.

1 Ответ

0 голосов
/ 26 февраля 2020

Вы можете добавить свойства в свои тесты, настроив плагин surefire, например:

...
  <groupId>use-surefire</groupId>
  <artifactId>use-surefire</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <properties>
    <environment>${env}</environment>
  </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.22.0</version>
        <configuration>
          <systemPropertyVariables>
            <environment>${environment}</environment>
          </systemPropertyVariables>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...