2.9. Enumerating DependenciesOften 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? 2.9.1. 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. 2.9.2. 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 and information about each dependency's artifact. You then assigned each artifact to a variable named lib in a j:forEach and referenced the absolute path of each dependency using ${lib.path}. Some information, such as the path to an artifact exposed through ${pom}, is derived at runtime. For more information about the Artifact object, see the JavaDoc at http://maven.apache.org/apidocs/org/apache/maven/repository/Artifact.html. Note: This example used j:forEachone of the two loop control structures in Jelly. j:forEach or j:while can be used to iterate over Collection, Map, List, or arrays.. |