类路径问题:使用 Maven Antrun 插件运行 ant java 任务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11953377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 07:04:03  来源:igfitidea点击:

Classpath problems:running ant java task with Maven Antrun plugin

javamavenantclasspath

提问by user1571449

When running ant task from maven ant run plugin I can set maven classpath as an ant property. However when I try to run <ant:javatask setting this exact classpath I get the error that the reference can not be find. As if the whole classpath is interpreted as one jar. Is there a way to somehow set this classpath to ant java task?

从 maven ant run 插件运行 ant 任务时,我可以将 maven 类路径设置为 ant 属性。但是,当我尝试运行<ant:java设置此确切类路径的任务时,我收到无法找到引用的错误。好像整个类路径都被解释为一个 jar。有没有办法以某种方式将此类路径设置为 ant java 任务?

(from maven)

(来自 Maven)

<plugin>
   <artifactId>maven-antrun-plugin</artifactId> 
     ....
   <property name="compile_classpath" refid="maven.compile.classpath"/>
   ....

(from ant) ...

(来自蚂蚁)...

<path id="classpath">
   <path refid="${compile_classpath}"/>
</path>
...
<java   classname="..." classpathref="classpath">
...
</java>

The version of maven ant run plugin is 1.7

maven ant run 插件版本为1.7

If this can not be done is there some way in ant to iterate this classpath string (location of jar files with ';' separator) and set the values of jar location as '

如果这不能完成,ant 中是否有某种方法来迭代这个类路径字符串(带有 ';' 分隔符的 jar 文件的位置)并将 jar 位置的值设置为 '

回答by Adrian

I think I've hit on the solution to this one after being frustrated for some time : inspired by this thread

我想我在沮丧一段时间后找到了解决方案:受此线程启发

The antrun plugin is correctly constructing classpath references, but not passing them through to the external build file when you invoke the anttask.

antrun 插件正确构建类路径引用,但在调用ant任务时不会将它们传递到外部构建文件。

So the solution is to explicitly pass in any classpath references you want to access using the <reference>element.

因此,解决方案是显式传入您要使用该<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>

And consume them as normal in your ant build task

并在您的 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>

回答by kuanba

in maven:

在 Maven 中:

<plugin>
   <artifactId>maven-antrun-plugin</artifactId> 
     ....
   <property name="compile_classpath" refid="maven.compile.classpath"/>
....

in Ant using pathelement instead of path refid

在 Ant 中使用 pathelement 而不是 path refid

<path id="classpath">
    <pathelement path="${compile_classpath}"/>
</path>

then it work

然后它工作

回答by Richard Holt

The problem you're having here is that compile_classpath is an Ant property. The expression ${compile_classpath} resolves to the value of the property.

您在这里遇到的问题是 compile_classpath 是一个 Ant 属性。表达式 ${compile_classpath} 解析为属性的值。

Whereas the refid attribute on the path element requires the reference to a path. Basically you're getting a runtime type error where a path ref is expected but you're providing a string.

而路径元素上的 refid 属性需要对路径的引用。基本上你会得到一个运行时类型错误,其中需要一个路径引用,但你提供了一个字符串。

What you really want to do is pass the maven.compile.classpath directly into your Ant path element. Since both are dealing with path objects. But this doesn't work.

您真正想要做的是将 maven.compile.classpath 直接传递到您的 Ant 路径元素中。因为两者都在处理路径对象。但这不起作用。

So the workaround I came up with was to pass the path to the individual jars as properties from Maven to the Ant build file.

所以我想出的解决方法是将单个 jar 的路径作为属性从 Maven 传递到 Ant 构建文件。

In Maven:

在 Maven 中:

<plugin>
<artifactId>maven-antrun-plugin</artifactId>
...
    <property name="example.jar" 
        value="${org.example.example:example-artifact:jar}"/> 
    ...

In Ant:

在蚂蚁中:

<path id="classpath">
    <path location="${example.jar}"/>
</path>

This works, but is obviously terrible if you have more than one dependency in the Maven classpath or want to pass the transitive dependencies. I think Ant Ivy is probably the way I'm going to take my build file.

这有效,但如果您在 Maven 类路径中有多个依赖项或想要传递传递依赖项,则显然很糟糕。我认为 Ant Ivy 可能是我获取构建文件的方式。

回答by Powerlord

Sorry to rebump an old thread, but after running into this myself today, I noticed that the Maven documentation has a bug in it. As you've discovered

很抱歉回复一个旧线程,但在今天自己遇到这个问题后,我注意到 Maven 文档中有一个错误。正如你所发现的

<property name="compile_classpath" refid="maven.compile.classpath"/>

doesn't work. However,

不起作用。然而,

<property name="compile_classpath" value="${maven.compile.classpath}"/>

should work. Of course, you can also use ${maven.compile.classpath}directly.

应该管用。当然也可以${maven.compile.classpath}直接使用。

The Maven bugtracker claims that this documentation bug was fixed 4 years ago, but as of today's date, it still existsin the last documentation pushed (in late 2011).

Maven bugtracker 声称这个文档错误是在4 年前修复的,但截至今天,它仍然存在于最后推送的文档中(2011 年底)。