Maven:如果在pom.xml中的property标签中有if语句

9
如果某个环境变量已经设置了,我想要设置一个属性。我在Google上搜索了很多相关信息,但是我找到的全部都类似下面的代码,而且我一直收到以下错误消息: [FATAL] Non-parseable POM Y:\Maven\parent-pom\pom.xml: TEXT must be immediately followed by END_TAG and not START_TAG (position: START_TAG seen ...roperties"\r\n
classpathref="maven.plugin.classpath" />... @29:55) @ line 29, column 55 这就是我正在尝试的代码,它在pom.xml文件中,并运行了以下命令 - mvn --errors deploy 当然,如果你有其他建议,我会很高兴得到任何其他解决方案,以便根据环境变量内容在pom.xml中设置属性。 谢谢, Eli
    <distributionManagement>
       .....
    </distributionManagement>

    <properties>
          <tasks>
        <taskdef resource="net/sf/antcontrib/antcontrib.properties"
          classpathref="maven.plugin.classpath" />

        <if>
           <condition>
             <equals arg1="${env.WAS60_HOME}" arg2=""\>
           </condition>
           <then>
             <was60.home>${env.WAS60_HOME}</was60.home>
             <javac>${was60.home}/java/bin/javac</javac>
           </then>
        </if>

         <if>
           <condition>
             <equals arg1="${env.WAS85_HOME}" arg2=""\>
           </condition>
           <then>
             <was85.home>${env.WAS85_HOME}</was60.home>
             <javac>${was85.home}/java/bin/javac</javac>
           </then>
        </if>
      </tasks>
</properties>
    <profiles>
       <profile>
    <id>was.base.v60</id>
            <dependencies>
               <dependency>
                 ....
                  <systemPath>${was60.home}/java/jre/lib/xml.jar</systemPath>
               </dependency>
               .....
            </dependencies>
        </profile>
        <profile>
    <id>was.base.v85</id>
            <dependencies>
               <dependency>
                 ....
                  <systemPath>${was85.home}/java/jre/lib/xml.jar</systemPath>
               </dependency>
               .....
            </dependencies>
        </profile>
    </profiles>
4个回答

16

一个更好的方法是使用配置激活

<profiles>
  <profile>
    <id>was.base.v60</id>
    <activation>
      <property>
        <name>env.WAS60_HOME</name>
      </property>
    </activation>
    <dependencies>
      <dependency>
        ....     
        <systemPath>${env.WAS60_HOME}/java/jre/lib/xml.jar</systemPath>
      </dependency>
      .....    
    </dependencies>
  </profile>
  <profile>
    <id>was.base.v85</id>
    <activation>
      <property>
        <name>env.WAS85_HOME</name>
      </property>
    </activation>
    <dependencies>
      <dependency>
        ....     
        <systemPath>${env.WAS85_HOME}/java/jre/lib/xml.jar</systemPath>
      </dependency>
      .....    
    </dependencies>
  </profile>
</profiles>

更新:

我使用配置文件的首选方式是在我的POM中设置默认属性,然后使用设置文件中的配置文件根据需要进行覆盖。

可以通过使用“-s”和“-P”命令行参数来明确地执行此操作:

mvn -s $PROJECT_SETTINGS -P myProfile ....

通过使用配置文件提供程序插件,这种方法在Jenkins中很容易维护,该插件可以启用一个GUI来编辑我为每个项目使用的各种设置文件。

更新2:

这是我设置构建的示例。POM包含具有默认属性值的部分。我设置了一个或多个pfiles来覆盖这些值:

<project>
  <properties>
     <my.property1>hello</my.property1>
     <my.property2>world</my.property2>
     ..
  </properties>
  ..

  <build>
    <profiles>
      <profile>
        <id>build_in_spanish</id>
        <properties>
          <my.property1>hola</my.property1>
          <my.property2>mundo</my.property2>
          ..
        </properties>
      </profile>
      <profile>
        <id>build_in_irish</id>
        <properties>
          <my.property1>dia dhuit</my.property1>
          <my.property2>an domhain</my.property2>
          ..
        </properties>
      </profile>
    <profiles>
  </build>
</project>

所以在这个例子中,构建默认为英语。要使用西班牙语设置运行构建

mvn -P build_in_spanish ...

注意:

  • 配置文件可以在POM文件中或者一个独立的设置文件中。后一种方式提供了最大的灵活性。
  • 可以通过显式启用(使用“-P”命令行选项)或尝试发现它所在的环境来启用配置文件。这是一种设计选择。

注意:

  • 配置文件可以在POM文件中或者一个独立的设置文件中。后一种方式提供了最大的灵活性。
  • 可以通过显式启用(使用“-P”命令行选项)或尝试发现它所在的环境来启用配置文件。这是一种设计选择。

另一个问题是,如果ENV变量未设置或为空怎么办? - Elyahu
谢谢马克,但我仍然不明白如何处理变量可能未设置或设置为空的情况? - Elyahu
@Elyahu 完全由你决定。我已经更新了我的答案,并详细介绍了我如何使用配置文件。 - Mark O'Connor
再次感谢你的建议,马克。看起来可以用。但是,我必须保持向后兼容,所以如果该属性被定义为环境变量,它会覆盖pom.xml中的定义吗?那个pom.xml是我们所有开发人员的“父POM”,所以更改它可能会造成很大的影响。 - Elyahu
@Elyahu,为什么不使用环境变量来触发你的配置文件呢?这样可以保留向后兼容性,同时又能利用配置文件来管理你的构建环境。具体请参考配置文件激活文档。 - Mark O'Connor
显示剩余3条评论

3
显然,在Maven中没有办法做一个合适的IF-THEN-ELSE语句。至少我没有找到任何方法。
我找到的最接近的方法是使用“activation”标签。如果其中的条件得到满足,它将激活配置文件。
在Maven 2.0.X中存在一些关于多个条件的错误,现在不确定其状态如何。

http://jira.codehaus.org/browse/MNG-4565

因此,在这个问题上回答了一个代码示例 -

Maven 2:使用条件替换值运行maven-replacer-plugin?

但是,如果您想做类似于<< IF ... Then ... Else >>的事情,您会遇到困难。当然有其他方法,我认为这是有意为之的,所以您必须遵循Maven方式。

感谢所有试图帮助Elyahu的人们


2
这就是Maven的大问题:你必须按照Maven的方式去做,而这种方式总是那么不直观,以至于在试图找到并应用相关文档两个小时后,我放弃了。 - reinierpost

3

尝试在行中将\>替换为/>

<equals arg1="${env.WAS60_HOME}" arg2=""\>

1

在属性标签内使用任务的用法很奇怪,使用maven-antrun插件代替。你可以使用以下方式设置maven属性:

http://maven.apache.org/plugins/maven-antrun-plugin/

<build>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.3</version>
<executions>
  <execution>
    <id>ftp</id>
    <phase>package</phase>
    <goals>
      <goal>run</goal>
    </goals>
    <configuration>
      <tasks>
        <taskdef resource="net/sf/antcontrib/antcontrib.properties"
          classpathref="maven.plugin.classpath" />
        <if>
          <equals arg1="${ftp}" arg2="true" />
          <then>
            <echo message="The value of property ftp is true" />
          </then>
          <else>
            <echo message="The value of property ftp is not true" />
          </else>
        </if>

      </tasks>
     <exportAntProperties>true</exportAntProperties>
    </configuration>
  </execution>
</executions>
<dependencies>
  <dependency>
    <groupId>ant-contrib</groupId>
    <artifactId>ant-contrib</artifactId>
    <version>20020829</version>
  </dependency>
</dependencies>
  </plugin>
</build>

你能给我一个例子吗?我在你发的链接里没找到。 - Elyahu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接