3
votes

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:java task 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?

(from 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

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 '

4

4 Answers

5
votes

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 ant task.

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

        <!-- 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

<!-- 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>
3
votes

in maven:

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

in Ant using pathelement instead of path refid

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

then it work

0
votes

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.

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.

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.

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.

In 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.

0
votes

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

<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.

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