如何根据操作系统在maven中设置环境变量

时间:2013-10-16 20:42:20

标签: linux windows maven

我对maven相当新鲜。我已经设置了一个pom.xml,它定义了运行我的单元测试的配置文件。我正在尝试设置Path环境变量。 env变量名称是Windows的路径和Linux的LD_LIBRARY_PATH。我不想继续改变这些环境。变量名取决于操作系统。我该怎么做到这一点?

<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>

1 个答案:

答案 0 :(得分:5)

Profile activation可能对此有所帮助。从集成测试配置文件中删除<environmentVariables>配置。然后添加下面的配置文件,调整<activation>部分以满足特定要求。您无需在命令行上显式启用这些配置文件; Maven将根据运行构建的系统激活正确的配置文件。

<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>
相关问题