Let the compiled jar package contain all dependencies so that it can be run independently.
In the section called "Running the Simple Weather Program", we executed the Simple Weather application using the Maven Exec plugin. while the Maven Exec plugin executed the program and produced some output, you shouldn't look to Maven as an execution container for your applications. if you are distributing this command-line application to others, you will probably want to distribute a JAR or an archive as a ZIP or TAR 'd GZIP file. the following section outlines a process for using a predefined assembly descriptor in the Maven Assembly plugin to produce a distributable JAR file which contains the project's bytecode and all of the dependencies.
The Maven Assembly plugin is a plugin you can use to create arbitrary distributions for your applications. you can use the Maven Assembly plugin to assemble the output of your project in any format you desire by defining a custom assembly descriptor. in a later chapter we will show you how to create a custom assembly descriptor which produces a more complex archive for the Simple Weather application. in this chapter, we're re going to use the predefined jar-with-dependencies format. to configure the Maven Assembly Plugin, we need to add the following plugin configuration to our existing build configuration in the pom. xml.
Refreshing the Maven Assembly Descriptor
Refreshing the Maven Assembly Descriptor
<project>
[...]
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
[...]
</project>
Once you 've added this configuration, you can build the assembly by runningMvn assembly: assembly.
$ cd target$ java -cp simple-weather-1.0-jar-with-dependencies.jar com.sonatype.maven.weather.Main 100020 INFO YahooRetriever - Retrieving Weather Data221 INFO YahooParser - Creating XML Reader399 INFO YahooParser - Parsing XML Response474 INFO WeatherFormatter - Formatting Weather Data********************************* Current Weather Conditions for: New York, NY, US Temperature: 44 Condition: Fair Humidity: 40 Wind Chill: 40*********************************
Thejar-with-dependencies
Format creates a single JARFile which except des all of the bytecode fromsimple-weather
Project and the unpacked bytecode from all of the dependencies. This somewhat unconventional format produces a 9 MiB JARFile containing approximately 5290 classes, but it does provide for an easy distribution format for applications you 've developed with Maven. later in this book, we'll show you how to create a custom assembly descriptor to produce a more standard distribution.