【问题标题】:How can I set a environment variables in Maven per run?如何在每次运行时在 Maven 中设置环境变量?
【发布时间】:2012-02-29 17:05:52
【问题描述】:

在我的项目中,我们创建了一个 Maven 模块来获取特定的 JBoss AS 并解压缩。
然后所有的测试用例都可以作为嵌入式容器在这个 Jboss AS 下运行。
我们使用 jboss-ejb3-embedded-standalone 来调用嵌入式容器,但是,它只是从环境变量中找到 JBOSS_HOME 并使用它来运行。 因此,我们必须在每次 mvn 安装时更新 JBOSS_HOME。

我尝试在 maven 中通过引入 exec-maven-plugin 来做到这一点,如下所示:

    <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.2.1</version>
    <configuration>
        <executable>env</executable>
        <environmentVariables>
            <JBOSS_HOME>
                C:/Sample/embedded-container/jboss-${version.org.jboss.jbossas}
            </JBOSS_HOME>
        </environmentVariables>
    </configuration>
    <executions>
        <execution>
            <id>resetJbossHome</id>
            <phase>integration-test</phase>
            <goals>
                <goal>exec</goal>
            </goals>
        </execution>
    </executions>
</plugin>

在控制台的输出中,我可以看到

[INFO] --- exec-maven-plugin:1.2.1:exec (resetJbossHome) @ test-embedded ---
....
JBOSS_HOME=C:/Sample/embedded-container/jboss-6.1.0.Final

....

但是在启动JBOSS时,它仍然在运行设置了origin JBOSS_HOME的那个。

此外,我也尝试过使用 maven-antrun-plugin。

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
        <execution>
            <id>copyRelease</id>
            <phase>pre-integration-test</phase>
            <configuration>
                <tasks>
                    <exec executable="env">
       <env key="JBOSS_HOME" value="C:/Sample/embedded-container/jboss-${version.org.jboss.jbossas}"/>
    </exec>
                </tasks>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>

结果是一样的。

是我的配置有问题还是有更好的方法?

【问题讨论】:

    标签: maven jboss exec-maven-plugin


    【解决方案1】:

    看看Maven profiles

    您可以定义一个用于测试的配置文件,一个用于生产的配置文件,具有不同的属性,例如

    <profiles>
      <profile>
        <id>test</id>
        <jboss.home>PATH TO JBOSS TEST INSTANCE</jboss.home>
      </profile>
      <profile>
        <id>prod</id>
        <jboss.home>PATH TO JBOSS PROD INSTANCE</jboss.home>
      </profile>
    </profiles>
    

    在你的 exec 插件中:

    <environmentVariables>
        <JBOSS_HOME>
            ${jboss.home}
        </JBOSS_HOME>
    </environmentVariables>
    

    【讨论】:

    • 其实真正的问题是在设置了environmentVariables之后,启动JBoss AS时不管你设置什么都不会生效。 :(
    • JBoss 是在一个单独的进程中启动的吗?也许这个过程继承自基础环境,而不是你的启动过程中的环境?
    • 在我的项目中,JUnit测试是由maven-failsafe-plugin启动的,在测试用例本身会调用jboss-ejb3-embedded-standalone jar实现的createEJBContainer方法,他会启动jboss作为容器。
    • Env 设置在 Maven 中,我不确定这个设置是否会影响它启动的 JVM...如果配置正确,我们可以说不,所以第一点是验证上面配置的正确性...
    • 只是为了确保我理解:您需要启动一个不同的 JBoss EJB 容器版本进行测试,另一个用于生产。如果是这样,为什么不使用不同的依赖项而不是使用环境变量?
    猜你喜欢
    • 2017-01-01
    • 2012-12-11
    • 2021-07-19
    • 1970-01-01
    • 1970-01-01
    • 2015-08-07
    • 2020-04-06
    • 2021-05-15
    • 2020-01-12
    相关资源
    最近更新 更多