Maven 属性未插值

标签 maven

Maven 属性有时会在插件配置中用于插值,有时则不会。为什么是这样?

例如,在下面的片段中,antrun插件可以访问dependency属性并按预期返回jar的本地路径。 exec 插件失败,因为 ${...} 的计算结果为 null。

<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>
<groupId>com.example</groupId>
<artifactId>eg</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.0</version>
    </dependency>
</dependencies>
<build>
    <plugins>

        <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
                <execution>
                    <phase>process-resources</phase>
                    <configuration>
                        <tasks>
                            <echo>Path in antrun: </echo>
                            <echo>${maven.dependency.commons-io.commons-io.jar.path}</echo>
                        </tasks>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2</version>
            <executions>
                <execution>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                    <phase>process-resources</phase>
                    <configuration>
                        <executable>echo</executable>
                        <arguments>
                            <argument>Path in mvn exec</argument>
                            <argument>${maven.dependency.commons-io.commons-io.jar.path}</argument>
                        </arguments>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

输出:

$ mvn process-resources
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building eg 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ eg ---
[debug] execute contextualize
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-antrun-plugin:1.3:run (default) @ eg ---
[INFO] Executing tasks
     [echo] Path in antrun:
     [echo] C:\Users\Jim\.m2\repository\commons-io\commons-io\2.0\commons-io-2.0.jar
[INFO] Executed tasks
[INFO]
[INFO] --- exec-maven-plugin:1.2:exec (default) @ eg ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.288s
[INFO] Finished at: Mon Feb 11 14:24:03 GMT 2013
[INFO] Final Memory: 4M/121M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2:exec (default) on project eg: Misconfigured argument, value is null. Set the argument to an empty value if this is the required behaviour. -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

最佳答案

类似依赖项的属性仅适用于 maven-antrun-plugin。我不知道您是否必须在实际的 Ant 脚本中使用 Maven 依赖项,但如果您使用过它,您就会知道 Ant 具有:

<artifact:dependencies ...>
    <pom file="mypom.xml"/>
</artifact:dependencies>

这基本上就是让您使用 Maven 依赖项的路径。

就您而言,我相信这对您来说是隐藏的,并且您已经将依赖项的路径作为属性。我想说的是,该插件正在为您自己处理这个问题。因此,其他插件看不到这些属性,因为 maven-antrun-plugin 并未导出实际的 Maven 属性,而是导出 Ant 本身范围内的属性。

我希望这更有意义。

关于Maven 属性未插值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14814037/

相关文章:

java - 在 Java-Scala maven 项目中看不到 Main.java 中的 scala 类

java - 具有依赖关系的 Maven jar?

jsf - Tomcat 7 链接错误 : loader constraint violation

java - 如何指定maven的distributionManagement组织范围?

Java - Maven - 在表单中设置图标图像

java - Maven 使用 Maven 连接/合并两个依赖项的 META-INF/服务/文件?

java - 查找 Maven 插件绑定(bind)到的默认阶段

java - Spring调度程序servlet找不到index.html。在 DispatcherServlet 中未找到带有 URI [] 的 HTTP 请求的映射

java - ClassNotFoundException oracle.jdbc.driver.OracleDriver

maven - 依赖管理和范围