Maven:如果在属性标签中的 pom.xml 中的语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18052589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 14:07:55  来源:igfitidea点击:

Maven: If sentences in pom.xml in the property tag

xmlmavenif-statementpom.xmlsentence

提问by Elyahu

I'd like to set a property if an environment variable is set. I googled a lot on it and all I found is something similar to the code below, but I keep getting the error:

如果设置了环境变量,我想设置一个属性。我在谷歌上搜索了很多,我发现的所有内容都类似于下面的代码,但我不断收到错误消息:

[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 s een ...roperties"\r\n
classpathref="maven.plugin.classpath" />... @29:55) @ line 29, column 55

[致命] 不可解析的 POM Y:\Maven\parent-pom\pom.xml:TEXT 后面必须紧跟 END_TAG 而不是 START_TAG(位置:START_TAG seen ...roperties"\r\n
classpathref="maven。 plugin.classpath" />... @29:55) @ 第 29 行,第 55 列

That's the code I'm trying, its inside a pom.xml and I ran the command -

这就是我正在尝试的代码,它在 pom.xml 中,我运行了命令 -

mvn --errors deploy

mvn --errors 部署

Of course, I'll be happy to get any other solution, if you have other suggestion on how to set a property in pom.xml depending on an environment variable content.

当然,如果您对如何根据环境变量内容在 pom.xml 中设置属性有其他建议,我很乐意获得任何其他解决方案。

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

采纳答案by Elyahu

Apparently there is no way to do a proper IF-THEN-ELSE sentence in Maven. At least none that I found.

显然没有办法在 Maven 中做一个正确的 IF-THEN-ELSE 语句。至少我没有发现。

The closest thing I found was using the "activation" tag. which will activate the profile if the condition in it is met.

我发现最接近的是使用“激活”标签。如果满足其中的条件,它将激活配置文件。

There is some bug on multiple conditions in Maven 2.0.X, not sure what's the status of it now -

Maven 2.0.X 中的多个条件存在一些错误,不确定它现在的状态 -

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

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

So, a code example is answered on the question here -

所以,这里的问题回答了一个代码示例 -

Maven 2: Run maven-replacer-plugin with conditional replacement value?

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

But if you want to do something like << IF ... Then ... Else >> You're going to hit you head on a wall doing it. There are other ways, of course and I assume it is done in purpose, so you'll have to follow Maven way.

但是如果你想做一些类似<< IF ... Then ... Else >> 你会在做这件事时撞到墙上。当然,还有其他方法,我认为它是有目的的,因此您必须遵循 Maven 的方式。

Thanks to all that tried to help Elyahu

感谢所有试图帮助以利亚胡的人

回答by Mark O'Connor

A much better approach would be to use profile activations.

更好的方法是使用配置文件激活

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

Update:

更新:

My preferred way to use profiles is to have a default set of properties in my POM and then override these on demand using profiles in my settings file.

我使用配置文件的首选方法是在我的 POM 中有一组默认属性,然后使用我的设置文件中的配置文件按需覆盖这些属性。

This approach is easy to do explicitly by using the "-s" and "-P" commandline parameters:

通过使用“-s”和“-P”命令行参数,这种方法很容易显式实现:

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

This approach is easy to maintain in Jenkins using Config File Provider pluginwhich enables a GUI for editing the various settings files I use for each project.

这种方法很容易在 Jenkins 中使用Config File Provider 插件进行维护,该插件启用了一个 GUI 来编辑我为每个项目使用的各种设置文件。

Update 2:

更新 2:

Here's an example of how I setup my builds. The POM contains a section with the default property values. And I setup one or more pfiles to over-ride these values:

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

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

So in this example the build defaults to English. To run the build with the settings in Spanish

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

mvn -P build_in_spanish ...

Note:

笔记:

  • Profiles can be in the POM or in a separated settings file. The latter approach gives the most flexibility.
  • Profiles can be explicitly enabled (using the "-P" command line option or by attempting to discover the environment it sits within. This is a design choice)
  • 配置文件可以在 POM 中或在单独的设置文件中。后一种方法提供了最大的灵活性。
  • 可以显式启用配置文件(使用“-P”命令行选项或尝试发现它所在的环境。这是一个设计选择)

回答by Julia Sokolova

try to replace \>with />in lines

尝试更换 \>/>中的行

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

回答by mebada

usage of tasks inside properties tag is wired , use maven-antrun plugin instead. you can set maven properties with

使用属性标签内的任务是有线的,请改用 maven-antrun 插件。你可以设置 maven 属性

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

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>