2.6. Running the Program from a Custom GoalNow that you've generated the client library, use Maven to execute the mdn.weather.Weather class and obtain the weather report for Chicago, Illinois. 2.6.1. How do I do that?Define a custom goal in maven.xml that uses Ant's java task to execute 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"/> <ant:pathelement location="${pom.getDependencyPath('commons-discovery:commons-discovery')}"/> <ant:pathelement location="${pom.getDependencyPath('commons-logging:commons-logging')}"/> <ant:pathelement location="${pom.getDependencyPath('axis:axis')}"/> <ant:pathelement location="${pom.getDependencyPath('axis:axis-jaxrpc')}"/> <ant:pathelement location="${pom.getDependencyPath('axis:axis-saaj')}"/> <ant:pathelement location="${pom.getDependencyPath('axis:axis-wsdl4j')}"/> </ant:classpath> </ant:java> </goal> The Weather class takes three argumentslatitude, longitude, and number of forecast days. In this goal, we've specified the coordinates for Chicago and a single day. When we execute the maven weather:run goal we get the response in the form of an XML document containing our forecast: <?xml version='1.0' ?> <dwml version='1.0' xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> // <!--snip--> <data> <location> <location-key>point1</location-key> <point latitude="41.50" longitude="-87.51"/> </location> <parameters applicable-location="point1"> <temperature type='maximum' units="Fahrenheit" time-layout="k-p24h-n1-1"> <name>Daily Maximum Temperature</name> <value>53</value> </temperature> <temperature type='minimum' units="Fahrenheit" time-layout="k-p24h-n1-1"> <name>Daily Minimum Temperature</name> <value>34</value> </temperature> <probability-of-precipitation type='12 hour' units="percent" time-layout="k-p12h-n2-2"> <name>12 Hourly Probability of Precipitation</name> <value>30</value> <value>8</value> </probability-of-precipitation> </parameters> </data> </dwml> Okay, it's going to be 53 degrees, and there's a 30% chance of rain. Well, that's better than last week! 2.6.2. What just happened?You used Ant's java task to execute Weather, and you supplied three parameters using ant:arg. The classpath was built by calling the geTDependencyPath( ) method on the ${pom} variable. ${pom.getDependencyPath('axis:axis-saaj')} returns the absolute path to the dependency with groupId axis and artifactId axis-saaj. To reference a dependency with this method, it must be present in your project.xml dependencies. weather:run specifies a prerequisite goal (jar) by listing jar in the prereqs attribute of the goal element. If weather:run depends on more than one goal, this attribute would contain a comma-delimited list of goals. Because weather:run depends on the jar goal, every time you run weather:run you will have to wait for the unit tests to complete. To skip unit tests in any build, add -Dmaven.test.skip=true to the command line. The following command line will execute the Weather class without running unit tests: maven weather:run -Dmaven.test.skip=true While using the ant:java task to execute a Java application is an option, there are better ways to run a program than writing a custom goal in a Maven project. In Chapter 6, you'll write a simple plug-in to execute a JAR. |