当从maven ant run插件运行ant任务时,我可以将maven classpath设置为ant属性。但是,当我尝试运行<ant:java
任务设置这个确切的类路径时,我得到了无法找到引用的错误。好像整个类路径被解释为一个jar。有没有办法以某种方式将此类路径设置为ant java task?
(来自maven)
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
....
<property name="compile_classpath" refid="maven.compile.classpath"/>
....
(来自蚂蚁) ...
<path id="classpath">
<path refid="${compile_classpath}"/>
</path>
...
<java classname="..." classpathref="classpath">
...
</java>
maven ant run插件的版本是1.7
如果无法做到这一点,在ant中有一些方法来迭代这个类路径字符串(jar文件的位置带有';'分隔符)并将jar位置的值设置为'
答案 0 :(得分:5)
我认为在经历了一段时间的挫折之后,我已经找到了解决这个问题的解决方案:受到this thread的启发
antrun插件正在构建类路径引用,但在调用ant
任务时不会将它们传递给外部构建文件。
因此,解决方案是使用<reference>
元素显式传入您要访问的任何类路径引用。
<!-- antrun plugin execution -->
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>build</id>
<phase>compile</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<ant antfile="${basedir}/build.xml">
<!-- This is the important bit -->
<reference torefid="maven.compile.classpath" refid="maven.compile.classpath"/>
</ant>
</target>
</configuration>
</execution>
</executions>
</plugin>
并在ant构建任务中正常使用它们
<!-- External ant build referencing classpath -->
<java classname="net.nhs.cfh.ebook.Main" fork="true" failonerror="true">
<arg value="-b"/>
<arg value="${dist.dir}"/>
<arg value="-o"/>
<arg value="${xml.dir}/treeindex"/>
<arg value="tree.xml"/>
<jvmarg value="-Dstrategy=treeParser"/>
<!-- reference to the passed-in classpath reference -->
<classpath refid="maven.compile.classpath"/>
</java>
答案 1 :(得分:3)
在maven中:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
....
<property name="compile_classpath" refid="maven.compile.classpath"/>
....
在Ant中使用pathelement而不是path refid
<path id="classpath">
<pathelement path="${compile_classpath}"/>
</path>
然后它工作
答案 2 :(得分:0)
您在这里遇到的问题是compile_classpath是一个Ant属性。表达式$ {compile_classpath}解析为属性的值。
而path元素的refid属性需要引用路径。基本上你得到一个运行时类型错误,其中路径引用是预期的,但你提供了一个字符串。
您真正想要做的是将maven.compile.classpath直接传递到您的Ant路径元素中。因为两者都在处理路径对象。但这不起作用。
因此,我提出的解决方法是将路径作为属性从Maven传递到各个jar文件到Ant构建文件。
在Maven:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
...
<property name="example.jar"
value="${org.example.example:example-artifact:jar}"/>
...
在Ant:
<path id="classpath">
<path location="${example.jar}"/>
</path>
这很有效,但如果你在Maven类路径中有多个依赖项或想要传递传递依赖项,那么显然很糟糕。我认为Ant Ivy可能就是我要采用构建文件的方式。
答案 3 :(得分:0)
很抱歉重新抽取一个旧线程,但在今天遇到这个问题后,我注意到Maven文档中有一个错误。正如你发现的那样
<property name="compile_classpath" refid="maven.compile.classpath"/>
不起作用。但是,
<property name="compile_classpath" value="${maven.compile.classpath}"/>
应该有效。当然,您也可以直接使用${maven.compile.classpath}
。
Maven bugtracker声称此文档错误为fixed 4 years ago,但截至今天的日期,最后一个文档中的still exists已推迟(2011年底)。