【问题标题】:Maven not passing overriden properties to antMaven 没有将覆盖的属性传递给 ant
【发布时间】:2013-02-22 12:57:36
【问题描述】:

目前在调用 ant 的 Maven 构建中遇到了一个奇怪的问题,在父 pom 中定义了以下属性:-

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

我试图通过在运行 Maven 时传入 -Djboss.home 来覆盖它。

此构建包含一个配置文件,当激活调用 Ant 构建时:-

<ant antfile="build.xml" inheritRefs="true">
    <target name="all"/>
</ant>

Ant 脚本使用以下属性:-

<property name="jboss.dir" value="${jboss.home}"/>

An 随后输出它:-

<echo message="jboss dir is: ${jboss.dir}"/>

问题是它输出的值是${env.JBOSS_HOME}

我可以从命令行覆盖父 pom 中的其他属性。

如果在 Maven 构建中的其他地方使用,即使 jboss.home 属性似乎也会被覆盖。

在尝试了各种命令组合之后,几乎可以看出传递给 Ant 的属性集是从 poms 解析的,然后是命令行的任何覆盖。如果我设置了 JBOSS_HOME 环境变量,那么所有使用这个变量的地方都有正确的值。

我是否缺少能够在命令行上覆盖此变量并在 Ant 脚本中使用覆盖值的东西?

【问题讨论】:

  • 我也看到了同样的问题...您找到解决方案了吗?我正在使用 Maven 3

标签: maven ant build system-properties


【解决方案1】:

今天偶然发现了这个问题,并确实找到了答案。此链接提供了一些背景信息:http://technotes.khitrenovich.com/properties-resolution-maven-implications-antrun-plugin/

如果属性在 内内联引用,Maven 属性将正常工作。例如:

<execution> .. <configuration> <target> <echo message="Command-line override of external.property will work OK like this: ${external.property}"/> </target> </configuration> </execution>

但是,如果您使用这样编写的外部 ant 文件,许多/大多数属性都将起作用,但是 命令行覆盖将不会传递到 ant 文件中。您将获得未覆盖的值。

<execution> .. <configuration> <target> <property name="external.property" value="${external.property}" /> <ant antfile="build.xml" target="all" /> </target> </configuration> </execution>

相反,对于外部 ant 文件,请使用此语法。命令行值将正确传递到外部antfile:

<execution> .. <configuration> <target> <ant antfile="build.xml" target="all" > <property name="external.property" value="${external.property}" /> <ant/> </target> </configuration> </execution>

【讨论】:

    【解决方案2】:

    问题在于${env.JBOSS_HOME} 是一个系统环境变量,而您将系统属性-Djboss.home=... 传递给JVM。这是两个不同的东西。除此之外,Java 世界中的任何变量、参数等都是区分大小写的。

    【讨论】:

      【解决方案3】:

      您似乎确实在做所有正确的事情,它对我有用,我用 2 个 pom.xml 文件做了一个工作示例。父 pom 如下所示:

      <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>com.stackoverflow.test</groupId>
              <artifactId>test</artifactId>
              <version>1.0</version>
          <name>Test</name>
          <packaging>pom</packaging>
          <modules>
              <module>test-ant-properties</module>
          </modules>
          <properties>
              <jboss.home>${env.JBOSS_HOME}</jboss.home>
          </properties>
      </project>
      

      然后我在子文件夹 test-ant-properties 中创建了一个模块 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>
          <parent>
              <groupId>com.stackoverflow.test</groupId>
              <artifactId>test</artifactId>
              <version>1.0</version>
          </parent>
          <artifactId>test-ant-properties</artifactId>
          <name>Test maven ant properties</name>
          <profiles>
              <profile>
                  <id>jboss</id>
                  <build>
                      <plugins>
                          <plugin>
                              <groupId>org.apache.maven.plugins</groupId>
                              <artifactId>maven-antrun-plugin</artifactId>
                              <version>1.6</version>
                              <executions>
                                  <execution>
                                      <id>jboss-ant</id>
                                      <phase>install</phase>
                                      <configuration>
                                          <target>
                                              <property name="jboss.dir" value="${jboss.home}"/>
                                              <echo message="jboss dir is: ${jboss.dir}"/>                                
                                          </target>
                                      </configuration>
                                      <goals>
                                          <goal>run</goal>
                                      </goals>
                                  </execution>
                              </executions>
                          </plugin>
                      </plugins>
                  </build>
              </profile>
          </profiles>
      </project>
      

      我没有安装 JBOSS,因此出于测试目的,我将环境变量设置为 test1234,如下所示: set JBOSS_HOME=test1234

      当我使用 jboss 配置文件执行父 pom 时

      mvn install -Pjboss
      

      我得到以下结果:[echo] jboss dir is: test1234

      当我使用 jboss.home 设置执行相同的命令时

      mvn install -Pjboss -Djboss.home=my_custom_variable
      

      我得到以下结果:[echo] jboss dir is: my_custom_variable

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-09-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-10
        • 1970-01-01
        • 2018-11-26
        相关资源
        最近更新 更多