【问题标题】:Exporting Maven properties from Ant code从 Ant 代码中导出 Maven 属性
【发布时间】:2011-03-05 05:18:13
【问题描述】:

我在我的 POM 中嵌入了以下代码:

<plugin name="test">
  <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
      <executions>
        <execution>
          <phase>validate</phase>
            <configuration>
              <tasks>
                <pathconvert targetos="unix" property="project.build.directory.portable">
                  <path location="${project.build.directory}"/>
                </pathconvert>
              </tasks>
            </configuration>
          <goals>
        <goal>run</goal>
      </goals>
    </execution>
  </executions>
</plugin>

然后我从run project 操作中引用${project.build.directory.portable},但它返回为null。在 Ant 块中执行 &lt;echo&gt; 会显示正确的值。我做错了什么?

【问题讨论】:

    标签: maven-2 ant


    【解决方案1】:

    为了完整起见,mentioned feature 于 2010 年 10 月在maven-antrun-plugin 中实施。

    您要查找的配置参数是exportAntProperties。

    使用示例:

    <plugin>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.7-SNAPSHOT</version>
        <executions>
            <execution>
                <phase>process-resources</phase>
                <goals>
                    <goal>run</goal>
                </goals>
                <configuration>
                    <target>
                        <exec outputproperty="svnversion"
                            executable="svnversion">
                            <arg value=".." />
                        </exec>
                    </target>
                    <exportAntProperties>true</exportAntProperties>
                </configuration>
            </execution>
        </executions>
    </plugin>
    

    附带说明,在发布这篇文章时 (2011-10-20),官方插件文档没有记录此选项。获取插件“versionXYZ”的帮助:

    mvn help:describe -Dplugin=org.apache.maven.plugins:maven-antrun-plugin:versionXYZ -Ddetail
    

    【讨论】:

    • 嗯,真的有用吗?根据这个issue我有一些麻烦
    • 尝试将 exportAntProperties 作为全局配置而不是执行配置
    • 我注意到这不会覆盖已经在 POM 中声明的属性,但是如果我不声明该属性,我的 linter(使用 IntelliJ)会抱怨未知的属性符号。有没有办法告诉它忽略这个,或者告诉插件覆盖现有属性?
    【解决方案2】:

    1.7 版的 maven-antrun-plugin 对我有用,可以将属性从 ant 传递到 maven(以及从 mvn 到 ant)。一些示例代码计算文件的 md5 校验和,然后将其存储到 mvn 稍后访问的属性中:

    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.7</version>
    <executions>
        <execution>
            <id>ant-md5</id>
            <phase>initialize</phase>
            <goals>
            <goal>run</goal>
            </goals>
        <configuration>
    
    <target>
        <property name="compile_classpath" refid="maven.compile.classpath"/>
        <property name="outputDir" value="${project.build.outputDirectory}"/>
        <property name="sourceDir" value="${project.build.sourceDirectory}"/>
        <checksum  file="${sourceDir}/com/blah/db/blah.java" property="blah.md5db"/>
    </target>
    <exportAntProperties>true</exportAntProperties>
    </configuration>
    </execution>
    </executions>
    

    稍后可以在 java 文件中使用 ${blah.md5db} 访问该属性。

    【讨论】:

      【解决方案3】:

      我不认为您可以从 Ant 设置一个属性,该属性将在 Maven 中可见。你应该写一个 Mojo。

      【讨论】:

      • 换一种说法:即使在 Windows 下,您如何确保 ${basedir} 包含 unix 样式的斜线?
      • @Gili 我不在 Windows 下,所以我无法进行广泛的测试(抱歉,懒得启动 VM),但我认为你不能。我仍然不明白你为什么需要这个,但如果是这样的话,我会在 Mojo 中注入 basedir 属性,使用 unix 样式的斜杠重写它并将其暴露在另一个属性下。
      • 当然,但这需要我写一个新的魔咒。难道没有一种优雅的方法可以用现有的插件来做到这一点吗?
      • 这在 Ant 插件中似乎是不可能的 :(
      • 自 2010 年 10 月以来,这是可能的。见my answer below
      【解决方案4】:

      来自插件文档here:

      尝试添加maven 前缀,这样你就有了 改为&lt;path location="${maven.project.build.directory}"/&gt;

      如果这不起作用,您可能需要自己明确地重新定义该属性:

      <property name="maven.project.build.dir" value="${project.build.directory}"/>
      <path location="${maven.project.build.directory}"/>
      

      【讨论】:

        猜你喜欢
        • 2011-03-20
        • 2023-04-05
        • 1970-01-01
        • 1970-01-01
        • 2017-09-10
        • 2016-09-08
        • 2015-01-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多