2013-02-11 73 views
2

Maven属性有时用于在插件配置中进行插值,有时不存在。为什么是这样?未插入Maven属性

例如,在以下片段中,antrun插件可以访问依赖项属性并按预期方式返回jar的本地路径。执行插件失败,因为${...}评估为空。

<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 

回答

4

的依赖般的属性只提供给maven-antrun-plugin。我不知道,如果你已经使用Maven依赖从实际Ant脚本内,但如果你问心无愧发挥它,你就会知道,蚂蚁有:

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

这基本上是什么让我们你使用Maven依赖关系的路径。

就你而言,我相信这是隐藏在你的视野之外,你已经获得了作为属性的依赖路径。我想说的是,插件正在为您处理这个问题。因此,其他插件不会看到这些属性,因为maven-antrun-plugin不会导出实际的Maven属性,而是 - 在Ant本身范围内的属性。

我希望这样做更有意义。

+1

谢谢,卡尔!必然的问题是你如何访问这些位置。我发现这样的答案确实有效:http://stackoverflow.com/a/8206943/175469 – 2013-02-12 13:28:12

+0

有趣!这很好理解! :) – carlspring 2013-02-12 13:55:19