Maven Antrun:将Maven属性传递给ant [英] Maven antrun: pass maven properties to ant

查看:89
本文介绍了Maven Antrun:将Maven属性传递给ant的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将Maven属性(通过配置文件定义)传递给antrun执行:

I am trying to pass maven properties (defined through profiles) to a antrun execution:

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

属性是在配置文件中定义的,以允许在不同的服务器中进行部署(我知道这不是最好的方法,但这是在此处完成操作的方式),如下所示:

The properties are defined in profiles to allow for deployment in different servers (I know it's not the best possible approach, but this is the way things are done here), like this:

<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中添加<echoproperties>任务,以查看我具有哪些属性,并且没有任何maven属性的痕迹. 是否可以使用Maven定义的属性,还是应该使用其他方法?欢迎任何建议.

My problem is that I can't get maven properties to work in ant plugin; I tried to add a <echoproperties> task in ant to see which properties I have and there is no trace of maven properties. Is it possible to use maven defined properties or should I use another approach? Any suggestion is welcome.

我按照第一个答案修改了脚本,但仍然无法正常工作

I modified the script as per first answer, it still doesn't work

推荐答案

您可以通过定义新的Ant属性(使用configurationtarget中的property标记)来传递属性.例如:

You can pass the properties by defining new Ant properties (using the property tag in your target within the configuration). So for example:

<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时,输出为:

When I execute mvn compile on this pom the output is:

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时,输出为:

and when the command is mvn -PcustomProfile compile then the output is:

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中有效.

This works using maven 3.0.5.

这篇关于Maven Antrun:将Maven属性传递给ant的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆