从外部 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.JdbcOdbc...