微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从外部 ant 文件中检索值以用于 Maven 项目

如何解决从外部 ant 文件中检索值以用于 Maven 项目

当使用 apache antrun 插件时 -- 更正式的名称maven-antrun-plugin -- it's possible (since 2010) to expose antrun properties to Maven using the configuration exportAntProperties 设置为 true。这非常适用于“内联”<target> 代码,这正是 most online examples I've found 使用的内容

但是,根据 antrun main project page,鼓励使用外部 build.xml 文件而不是“内联”代码,引用:

“[...] 鼓励将所有 Ant 任务移动到一个 build.xml 文件,并使用 Ant 的 <ant/> 任务从 POM 中调用它。”

...但是,访问通过 build.xml 文件设置的属性似乎要困难得多。

如何从 Maven 项目的 build.xml 文件中访问/检索属性?这适用于内联代码,但事实证明外部文件要困难得多。

代码

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <name>Antrun Tests</name>
    <description>To test inline antrun tasks versus external antrun tasks</description>

    <groupId>org.example</groupId>
    <artifactId>antrun</artifactId>
    <version>1.0.0</version>
    <build>
        <defaultGoal>validate</defaultGoal>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.8</version>
                <executions>
                    <!-- Inline -->
                    <execution>
                        <id>inline-ant</id>
                        <goals><goal>run</goal></goals>
                        <phase>validate</phase>
                        <configuration>
                            <target name="inline-ant">
                                <property name="inline.ant" value="PASS"/>
                                <echo level="info">Ant scope:</echo>
                                <echo level="info">[inline.ant]:   (set to: ${inline.ant})</echo>
                            </target>
                            <exportAntProperties>true</exportAntProperties>
                        </configuration>
                    </execution>

                    <!-- External -->
                    <execution>
                        <id>external-ant</id>
                        <goals><goal>run</goal></goals>
                        <phase>validate</phase>
                        <configuration>
                            <target name="external-ant">
                                <ant antfile="build.xml"/>
                            </target>
                            <exportAntProperties>true</exportAntProperties>
                        </configuration>
                    </execution>

                    <!-- Echo results -->
                    <execution>
                        <id>results</id>
                        <goals><goal>run</goal></goals>
                        <phase>validate</phase>
                        <configuration>
                            <target name="results">
                                <!--
                                     Note: This conditional logic is superfluous!
                                     Ant can NOT set set a variable if it's already
                                     set! However,since Maven's default behavior allows
                                     this,I'm adding a clear,unnecessary guard as to
                                     not confuse causal onlookers. <3
                                -->
                                <!-- Set to Failed if not set -->
                                <condition property="inline.ant" value="Failed">
                                    <not><isset property="inline.ant"/></not>
                                </condition>
                                <condition property="external.ant" value="Failed">
                                    <not><isset property="external.ant"/></not>
                                </condition>

                                <!-- Echo to console -->
                                <echo level="info">Maven scope:</echo>
                                <echo level="info">[inline.ant]:   ${inline.ant}</echo>
                                <echo level="info">[external.ant]: ${external.ant}</echo>
                            </target>
                            <exportAntProperties>true</exportAntProperties>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

... 和相关的 build.xml

<project default="external-ant">
    <target name="external-ant">
        <property name="external.ant" value="PASS"/>
        <echo level="info">Ant scope:</echo>
        <echo level="info">[external.ant]:   (set to: ${external.ant})</echo>
    </target>
</project>

编辑:我认为这实际上可能是 ant 的 <ant antfile="..."> 任务的副作用,而 antrun 插件是对它的附带损害。

例如,如果我有权访问 <import file="..."/>,我相信它会起作用,但是 ant 需要在 <project> 级别进行导入,而 antrun 不允许任何高于 {{1} 级别的内容}} 级(<target> 必须是 <target> 的子级)。

例如,即使我在同一个精确的蚂蚁目标中回显,我仍然看不到属性,这表明这是强迫人们使用 <project> 开始的细微差别,与预期行为相冲突,以及与 antrun 文档中的“鼓励”用法相冲突。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?