7

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

[致命] 不可解析的 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 列

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

mvn --errors 部署

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

谢谢, 伊莱

    <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

4 回答 4

12

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

<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 中使用Config File Provider 插件进行维护,该插件使 GUI 能够编辑我用于每个项目的各种设置文件。

更新 2:

这是我如何设置构建的示例。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>

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

mvn -P build_in_spanish ...

笔记:

  • 配置文件可以在 POM 或单独的设置文件中。后一种方法提供了最大的灵活性。
  • 可以显式启用配置文件(使用“-P”命令行选项或尝试发现它所在的环境。这是一种设计选择)
于 2013-08-05T08:18:38.823 回答
3

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

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

Maven 2.0.X 中的多个条件存在一些错误,不知道它现在的状态是什么 -

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

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

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

但是,如果您想做类似 << IF ... Then ... Else >> 之类的事情,那么您将在这样做时将头撞到墙上。当然还有其他方法,我认为它是有目的的,所以你必须遵循 Maven 方式。

感谢所有试图帮助 Elyahu 的人

于 2013-08-19T17:05:47.223 回答
3

尝试在行 中用 / >替换\>

<equals arg1="${env.WAS60_HOME}" arg2=""\>
于 2015-02-13T14:21:51.997 回答
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>
于 2013-08-05T07:49:28.890 回答