This project depends on two classpath resources: the Main
class that configures Log4J
with a classpath resource named log4j.properties, and the
WeatherFormatter
that references a Velocity
template from the classpath named output.vm. Both of these resources need to
be in the default package (or the root of the classpath).
To add these resources, weâll need to create a new directory
from the base directory of the project: src/main/resources. Since this directory
was not created by the archetype:create
task, we need to
create it by executing the following commands from the projectâs base
directory:
$ cd src/main $ mkdir resources $ cd resources
Once the resources directory is created, we can add the two resources. First, add the log4j.properties file in the resources directory, as shown in Example 4-9.
Example 4-9. simple-weatherâs Log4J configuration file
# Set root category priority to INFO and its only appender to CONSOLE. log4j.rootCategory=INFO, CONSOLE # CONSOLE is set to be a ConsoleAppender using a PatternLayout. log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.CONSOLE.Threshold=INFO log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern=%-4r %-5p %c{1} %x - %m%n
This log4j.properties file
simply configures Log4J to print all log messages to standard output
using a PatternLayout
. Lastly, we need to
create the output.vm, which is
the Velocity template used to render the output of this command-line
program. Create output.vm in the
resources/ directory. See Example 4-10.
Example 4-10. simple-weatherâs output Velocity template
********************************* Current Weather Conditions for: ${weather.city}, ${weather.region}, ${weather.country} Temperature: ${weather.temp} Condition: ${weather.condition} Humidity: ${weather.humidity} Wind Chill: ${weather.chill} *********************************
This template contains a number of references to a variable
named weather
, which is the
Weather
bean that was passed to the
WeatherFormatter
. The
${weather.temp}
syntax is shorthand for retrieving
and displaying the value of the temp
bean property.
Now that we have all of our projectâs code in the right place, we can
use Maven to run the example.
Get Maven: The Definitive Guide 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.