Linux – How to set environment variables in maven depending on the operating system

How to set environment variables in maven depending on the operating system… here is a solution to the problem.

How to set environment variables in maven depending on the operating system

I’m still new to Maven. I’ve set up a pom .xml that defines a configuration file to run my unit tests. I’m trying to set the Path environment variable. Environment variable names are Windows Path and Linux LD_LIBRARY_PATH. I don’t want to continue to change these environments. Variable names that depend on the operating system. How do I do this?

<profile>
        <id>integration-tests</id>
        <build>
         <plugins>
            <plugin>
                <groupId>org.eclipse.tycho</groupId>
                <artifactId>tycho-surefire-plugin</artifactId>
                <version>${tychoVersion}</version>
                <configuration combine.self="override">
                    <argLine>${tycho.testArgLine} ${global.test.vmargs} ${bundle.test.vmargs}</argLine>
                    <forkMode>${bundle.test.forkMode}</forkMode>
                    <useUIHarness>${bundle.test.useUIHarness}</useUIHarness>
                    <useUIThread>${bundle.test.useUIThread}</useUIThread>
                    <environmentVariables>
                      <!--For windows change LD_LIBRARY_PATH to PATH-->
                        <LD_LIBRARY_PATH>${dependenciesDir}${path.separator}{env. LD_LIBRARY_PATH}</LD_LIBRARY_PATH>

</environmentVariables>
                </configuration>
            </plugin>
        </plugins>
        </build>

</profile>

Solution

Profile activation might help here. Delete the configuration <environmentVariables > from the integration test configuration file. Then add the configuration file below, adjusting the <activation> section to meet your specific requirements. You do not need to explicitly enable these profiles on the command line; Maven will activate the correct profile based on the system on which the build is running.

<profile>
  <id>windows-tests</id>
  <activation>
      <os>
        <family>Windows</family>
      </os>
  </activation>
  <build>
     <plugins>
        <plugin>
            <groupId>org.eclipse.tycho</groupId>
            <artifactId>tycho-surefire-plugin</artifactId>
            <version>${tychoVersion}</version>
            <configuration>
                <environmentVariables>
                    <PATH>${dependenciesDir}${path.separator}{env. PATH}</PATH>
                </environmentVariables>
            </configuration>
        </plugin>
    </plugins>
    </build>
</profile>
<profile>
  <id>linux-tests</id>
  <activation>
      <os>
        <family>Linux</family>
      </os>
  </activation>
  <build>
     <plugins>
        <plugin>
            <groupId>org.eclipse.tycho</groupId>
            <artifactId>tycho-surefire-plugin</artifactId>
            <version>${tychoVersion}</version>
            <configuration>
                <environmentVariables>
                    <LD_LIBRARY_PATH>${dependenciesDir}${path.separator}{env. LD_LIBRARY_PATH}</LD_LIBRARY_PATH>

</environmentVariables>
            </configuration>
        </plugin>
    </plugins>
    </build>
</profile>

Related Problems and Solutions