【问题标题】:Maven antrun: pass maven properties to antMaven antrun:将 Maven 属性传递给 ant
【发布时间】:2013-08-08 14:30:10
【问题描述】:

我正在尝试将 maven 属性(通过配置文件定义)传递给 antrun 执行:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.7</version>
    <dependencies>
        <!-- ... these are ok -->
    </dependencies>
    <executions>
        <execution>
            <configuration>
                <target>
                    <property name="ant_destDir" value="${destDir}" />
                    <property name="ant_serverDeploy" value="${serverDeploy}" />
                    <property name="ant_deployDir" value="${deployDir}" />
                    <property name="ant_userDeploy" value="${userDeploy}" />
                    <property name="ant_passwordDeploy" value="${passwordDeploy}" />
                    <!-- correct task definitions for sshexec and scp -->
                    <sshexec host="${serverDeploy}" username="${userDeploy}" 
                            password="${passwordDeploy}" trust="yes" 
                            command="some command" />
                    <scp remoteTodir="${userDeploy}@${serverDeploy}:${destDir}" 
                            password="${passwordDeploy}" trust="yes" sftp="true">
                        <fileset dir="${deployDir}" includes="*.jar" />
                    </scp>
                    <sshexec host="${serverDeploy}" username="${userDeploy}" 
                            password="${passwordDeploy}" trust="yes" 
                            command="some command" />
                </target>
            </configuration>
            <phase>install</phase>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>

属性是在配置文件中定义的,以允许在不同的服务器中进行部署(我知道这不是最好的方法,但这里就是这样做的),如下所示:

<profile>
    <id>aprofile</id>
    <properties>
        <property name="serverDeploy" value="somevalue" />
        <property name="userDeploy" value="someuser" />
        <property name="passwordDeploy" value="somepassword" /> 
        <!-- and so on -->
    </properties>
</profile>

我的问题是我无法在 ant 插件中使用 maven 属性;我试图在 ant 中添加一个&lt;echoproperties&gt; 任务,以查看我拥有哪些属性并且没有任何 maven 属性的痕迹。 是否可以使用 Maven 定义的属性或者我应该使用其他方法?欢迎提出任何建议。

编辑:我根据第一个答案修改了脚本,它仍然不起作用

【问题讨论】:

    标签: maven maven-antrun-plugin


    【解决方案1】:

    您可以通过定义新的 Ant 属性来传递属性(在 configuration 中使用 target 中的 property 标记)。比如:

    <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/maven-v4_0_0.xsd">
    
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.test</groupId>
        <artifactId>test-module</artifactId>
        <name>test-module</name>
        <version>SNAPSHOT</version>
    
        <properties>
            <my.custom.property>false</my.custom.property>
        </properties>
    
        <profiles>
            <profile>
                <id>customProfile</id>
                <properties>
                    <my.custom.property>true</my.custom.property>
                </properties>
            </profile>
        </profiles>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>1.7</version>
                    <executions>
                        <execution>
                            <id>compile</id>
                            <phase>compile</phase>
                            <configuration>
                                <target>
                                    <property name="antProperty" value="${my.custom.property}"/>
                                    <echo message="Custom Ant Property is: ${antProperty}"/>
                                    <echoproperties />
                                </target>
                            </configuration>
                            <goals>
                                <goal>run</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </project>
    

    当我在这个 pom 上执行 mvn compile 时,输出是:

    main:
    [echo] Custom Ant Property is: false
    [echoproperties] #Ant properties
    [echoproperties] #Thu Aug 08 17:17:30 CEST 2013
    [echoproperties] ant.project.name=maven-antrun-
    [echoproperties] ant.version=Apache Ant(TM) version 1.8.2 compiled on December 20 2010
    [echoproperties] antProperty=false
    

    当命令为mvn -PcustomProfile compile 时,输出为:

    main:
    [echo] Custom Ant Property is: true
    [echoproperties] #Ant properties
    [echoproperties] #Thu Aug 08 17:18:30 CEST 2013
    [echoproperties] ant.project.name=maven-antrun-
    [echoproperties] ant.version=Apache Ant(TM) version 1.8.2 compiled on December 20 2010
    [echoproperties] antProperty=true
    

    这适用于 maven 3.0.5。

    【讨论】:

    • @RiccardoCossu 我创建了一个仅包含一个 pom 的测试项目(我已经编辑了答案以包含完整的 pom)。正如我执行 maven 时的答案中所述,该属性在 antrun 调用中被正确替换。你用的是什么版本的maven?
    • 我试过你的测试项目,它可以工作;我仍然需要了解为什么它在我的特定情况下不起作用... :-( 我使用的是 3.0.4
    • 知道了!我陷入了 ant 和 maven 之间的混淆循环......我在 maven 部分以“ant”方式声明属性(使用名为 property 的标签,而不是自定义名称)
    • 很高兴听到你把它整理好了。
    【解决方案2】:

    大多数属性都会自动传递给 ant,至少在您运行内联的 ant 脚本时是这样。一些属性被重命名。我建议运行“mvn -X”,antrun 插件将所有变量映射的列表打印到 ant 中(例如 basedir 变成 project.basedir 等)

    【讨论】:

      【解决方案3】:

      在较新版本的 maven 上,您可以使用:

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

      示例:

         <build>
      ....    
             <plugins>
               ....
          <plugin>
               <artifactId>maven-antrun-plugin</artifactId>
               <executions>
                  <execution>
                  <phase>install</phase>
                  <goals>
                      <goal>run</goal>
                  </goals>
                  <configuration>
                  <tasks>
                    <taskdef  resource="net/sf/antcontrib/antcontrib.properties"
                                              classpathref="maven.plugin.classpath" />
      
                    <echo message="Project name from Maven: ${project.name}" />
      
                  </tasks>
              </configuration>
               </execution>
             </executions>
          </plugin>
               ....
            </plugins>
               ....
         </build>
      

      <dependencies>
      <dependency>
          <groupId>ant-contrib</groupId>
          <artifactId>ant-contrib</artifactId>
          <version>1.0b3</version>
          <exclusions>
              <exclusion>
                  <groupId>ant</groupId>
                  <artifactId>ant</artifactId>
              </exclusion>
          </exclusions>
      </dependency>
      <dependency>
          <groupId>ant</groupId>
          <artifactId>ant-nodeps</artifactId>
          <version>1.6.5</version>
      </dependency>
      </dependencies>
      

      【讨论】:

        猜你喜欢
        • 2016-09-02
        • 1970-01-01
        • 2015-03-18
        • 1970-01-01
        • 1970-01-01
        • 2023-04-05
        • 1970-01-01
        • 2019-09-06
        • 2012-10-28
        相关资源
        最近更新 更多