0

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>

Environment is the parameter i'm going to pass via

mvn clean test -Denv=dev

in Test class

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'm in need to pass the environment variable from command line to pom and to Java, but every time I'm getting null.

halfer
  • 19,824
  • 17
  • 99
  • 186
Arun AV
  • 86
  • 4

1 Answers1

0

You can inject properties into your tests by configuring the surefire plugin, as in:

...
  <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>
Daniele
  • 2,672
  • 1
  • 14
  • 20