Maven 2 从Ant代码导出Maven属性
我在POM中嵌入了以下代码:Maven 2 从Ant代码导出Maven属性,maven-2,ant,Maven 2,Ant,我在POM中嵌入了以下代码: <plugin name="test"> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <executions> <execution> <phase>validate</ph
<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>
org.apache.maven.plugins
maven antrun插件
验证
跑
然后,我从
runproject
操作中引用${project.build.directory.portable}
,但它返回为null
。在Ant块中执行
将显示正确的值。我做错了什么?我认为您不能从Ant设置一个属性,该属性在Maven中是可见的。您应该从插件文档中编写一个Mojo。:
尝试添加maven
前缀,以便
如果这不起作用,您可能需要自己明确地重新定义属性:
<property name="maven.project.build.dir" value="${project.build.directory}"/>
<path location="${maven.project.build.directory}"/>
为了完整起见,该插件于2010年10月在maven antrun插件中实现
您要查找的配置参数为
使用示例:
<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>
maven antrun插件的1.7版为我提供了一个从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>
maven-antrun插件
1.7
ant-md5
初始化
跑
真的
该属性在以后的java文件中可以通过${blah.md5db}访问 换句话说:即使在Windows下,您如何确保${basedir}也包含unix风格的斜杠?@Gili我不在Windows下,因此无法进行全面测试(抱歉,太懒了,无法启动VM),但我认为您不能。我仍然不明白您为什么需要它,但如果是这样,我会将basedir
属性注入到一个Mojo中,使用unix风格的斜杠重写它,并在另一个属性下公开它。当然,这需要我编写一个新的Mojo。对于现有的插件,难道没有一种优雅的方法可以做到这一点吗?从Ant插件来看,这似乎是不可能的:(从2010年10月开始,这是可能的。请看,这真的有效吗?我遇到了一些问题,因此尝试将exportAntProperties作为全局配置而不是执行配置。我注意到,这不会覆盖POM中已声明的属性,但如果我不声明属性my linter(使用IntelliJ)抱怨未知属性符号。有没有办法告诉它忽略此符号,或者告诉插件覆盖现有属性?
<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>