Maven Antrun and Dependencies

16,572

Solution 1

Based on the code that SingleShot linked to, and random poking until it worked, here's how I got this problem "working", (I say in quotes because it feels very tenuous.)

Here's the way to make it properly work:

<property name="log4j_location" 
                value="${maven.dependency.log4j.log4j.jar.path}"/>
<echo message="${log4j_location}"/>

Some important things to note: You cannot use the maven dependency as a refid in setting the ant property. You have to use ${} to get the maven var value.

It appears that the dependency must be in the top-level dependency list, making log4j a dependency of the antrun plugin does not expose it to the plugin in anyway that I can see.

All of the path separators are dots (.), no colons (:) which is why I ultimately checked my own answer as correct.

Soapbox:

I would highly recommend anyone considering Maven use Ant with maven plugins or, even better, use Ant with Ivy instead.

This particular problem is a shining example of the utterly absurd level of difficulty associated with doing anything out of the norm with maven.

I say this having implemented an entire build system based on Maven2, and having also implemented several build systems in Ant. I've used both Maven2 and Ant with complex builds involving Java, Flex/AS3, C# and C++. Maven makes sense for Java projects that have no external dependencies on projects in other languages.

Maven does address some things that aren't addressed implicitly by Ant, but with some up front planning, Ant is the much more flexible, better documented, and the less buggy tool.

If you decide to go the ant route, make sure to define a structure for your projects, figure out your dependency system (Use one).

I think you will ultimately be much happier than with Maven, as you won't spend crunch time trying to fix your build system.

Solution 2

As an addendum to Aaron H.'s answer above, I had to set the plugin's version to 1.3 for that to actually work. I was using it without a specific version and was getting 1.1 (where nothing seems to work).


      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.3</version>
        ...
      </plugin>

Solution 3

Without an example of what you typed into your POM its hard to say, but consider a concrete example. Let's say your POM references log4j (groupId=log4j, artifactId=log4j). I believe this is how you would reference that JAR in your Ant file:

<property name="log4j.jar" refid="maven.dependency.log4j:log4j:jar.path"/>
<echo message="Where is the Log4J JAR?: ${log4j.jar}"/>

Ideally you shouldn't have to reference specific JARs, but rather, reference the entire classpath for the appropriate scope, as the somewhat sparse documentation for the plug-in indicates.

If you still have trouble, please post the <dependency> tag for a Maven POM dependency you are using and I can try to be more specific.

I looked at the plugin's code to confirm.

Solution 4

This works for me.

<copy file="${javax.mail:javax.mail-api:jar}" todir="tomcat/lib" />
<copy file="${org.springframework:spring-instrument-tomcat:jar}" todir="tomcat/lib" />
<copy file="${postgresql:postgresql:jar}" todir="tomcat/lib"/>

http://maven.apache.org/plugins/maven-antrun-plugin/examples/classpaths.html has the explanation of how to reference dependencies form the ant classpath.

Solution 5

There is a bug in the documentation. The path should be of the form:

<property name="mvn.dependency.jar" 
  value="${maven.dependency.my.group.id.my.artifact.id.classifier.jar.path}"/>

So the correct key for your log4j dependency would be:

maven.dependency.log4j.log4j.jar.path

Also note that it should be value= rather than refid=, so the full property would be:

<property name="log4j.jar" 
      value="${maven.dependency.log4j.log4j.jar.path}"/>
<echo message="My Dependency JAR-Path: ${log4j.jar}"/> 
Share:
16,572

Related videos on Youtube

Ofer
Author by

Ofer

I am a developer, my tools are C/C++, Python, and Bash.

Updated on April 19, 2022

Comments

  • Ofer
    Ofer about 2 years

    (See edits below.)

    The reason I can't just use the classpath, is because I need to manage some non-java libraries, and I'm compiling a non-java project.

    I'm trying to use maven dependencies in an antrun call, following the documentation on the maven site:

    http://maven.apache.org/plugins/maven-antrun-plugin/examples/classpaths.html

    At the bottom of the page:

    <property name="mvn.dependency.jar" 
          refid="maven.dependency.my.group.id:my.artifact.id:classifier:jar.path"/>
    <echo message="My Dependency JAR-Path: ${mvn.dependency.jar}"/> 
    

    I can't make this work no matter how I try. I've tried ${} around the refid contents, I've tried colons, periods, etc.. as separators in every way I can think of.

    Can anyone tell me what that refid should really look like for some common dependency?

    EDIT:

    Thanks for your reply.

    Using your example SingleShot, I have the following:

    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <executions>
          <execution>
            <id>create-messages</id>
            <phase>compile</phase>
            <configuration>
              <tasks>
                <property name="build.compiler" value="extJavac"/>
                <property name="compile_classpath" refid="maven.compile.classpath"/>
                <property name="runtime_classpath" refid="maven.runtime.classpath"/>
                <property name="test_classpath" refid="maven.test.classpath"/>
                <property name="plugin_classpath" refid="maven.plugin.classpath"/>
    
                <property name="log4j.jar" refid="log4j:log4j:jar"/>
                <echo message="Where is the Log4J JAR?: ${log4j.jar}"/>
              </tasks>
            </configuration>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
        </executions>
        <dependencies>
          <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.14</version>
          </dependency>
        </dependencies>
      </plugin>
    

    And here's what I get when run mvn compile:

    [INFO] Scanning for projects...
    [INFO] ------------------------------------------------------------------------
    [INFO] Building Chat Component
    [INFO]    task-segment: [compile]
    [INFO] ------------------------------------------------------------------------
    Downloading: http://<redacted>/content/groups/public/log4j/log4j/1.2.14/log4j-1.2.14.pom
    2K downloaded
    Downloading: http://<redacted>/content/groups/public/log4j/log4j/1.2.14/log4j-1.2.14.jar
    358K downloaded
    [INFO] [antrun:run {execution: create-messages}]
    [INFO] Executing tasks
    [INFO] ------------------------------------------------------------------------
    [ERROR] BUILD ERROR
    [INFO] ------------------------------------------------------------------------
    [INFO] Error executing ant tasks
    
    Embedded error: Reference log4j:log4j:jar not found.
    [INFO] ------------------------------------------------------------------------
    [INFO] For more information, run Maven with the -e switch
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 3 seconds
    [INFO] Finished at: Fri Oct 16 14:54:19 PDT 2009
    [INFO] Final Memory: 7M/80M
    [INFO] ------------------------------------------------------------------------
    

    EDIT (2):

    Looking at the sourcecode linked I decided to run "mvn -X compile" and grep for "Storing", which turns up a bunch of log output where things are getting stored.

    Of interest are the facts that the dependency I'm explicitly specifying isn't showing in the list, and, that when I switch to a key based on one of the entries I do see, I still get the error.

  • SingleShot
    SingleShot over 14 years
    Sorry. My original answer missed the "maven.dependency." prefix and ".path" suffix. It is now fixed and should work. Look at line 87 of the plug-in source (linked above) to see how it should look.
  • Ofer
    Ofer over 14 years
    I suspect that the difference is in which repo(s) we're using. This is, imho (Or maybe not so humble), just another example of where "it just works" breaks down with maven...