Enumerating Dependencies

Often you will need to access information from the POM in a Jelly script. How about running this weather program without having to list all of the dependencies as pathelements?

How do I do that?

The POM is made available to a Jelly script as a variable—${pom}. Here's a goal which uses the core Jelly tag forEach and an Apache Ant pathelement tag to create the classpath for running the mdn.weather.Weather class:

<goal name="weather:run" prereqs="jar">
  
  <ant:java classname="mdn.weather.Weather" fork="true">
    <ant:arg value="41.30"/>
    <ant:arg value="-87.51"/>
    <ant:arg value="1"/>
    <ant:classpath>
      <ant:pathelement location="${maven.build.dir}/${maven.final.name}.jar"/>
      <j:forEach var="lib" items="${pom.artifacts}">
        <ant:pathelement path="${lib.path}"/>
      </j:forEach>      
    </ant:classpath>
  </ant:java>
  
</goal>

Running this goal has the same effect as running weather:run from the previous section. This method is preferable to the previous one, as you do not need to list the dependencies in two places. Using this approach, a dependency added to project.xml will automatically be added to the classpath.

What just happened?

Your custom Jelly goal iterated through the dependencies using the j:forEach tag. When you referenced ${pom.artifacts}, you were referencing the getArtifacts() method on the POM which is represented by the org.apache.maven.project.Project object. getArtifacts() returns a list of org.apache.maven.project.Artifact objects which contain information from project.xml ...

Get Maven: A Developer's Notebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.