Maven by Example - 4.13. Building a Packaged Command Line Application |
|
In Section 4.8, “Running the Simple Weather Program” earlier in 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 going to use the predefined Configuring 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
running the $ mvn install assembly:assembly ... [INFO] [jar:jar] [INFO] Building jar: ~/examples/ch-custom/simple-weather/target/simple-weather-1.0.jar [INFO] [assembly:assembly] [INFO] Processing DependencySet (output=) [INFO] Expanding: \ .m2/repository/dom4j/dom4j/1.6.1/dom4j-1.6.1.jar into \ /tmp/archived-file-set.1437961776.tmp [INFO] Expanding: .m2/repository/commons-lang/commons-lang/2.1/\ commons-lang-2.1.jar into /tmp/archived-file-set.305257225.tmp ... (Maven Expands all dependencies into a temporary directory) ... [INFO] Building jar: \ ~/examples/ch-custom/simple-weather/target/\ simple-weather-1.0-jar-with-dependencies.jar Once our assembly is assembled in
$ cd target $ java -cp simple-weather-1.0-jar-with-dependencies.jar \ org.sonatype.mavenbook.weather.Main 10002 0 INFO YahooRetriever - Retrieving Weather Data 221 INFO YahooParser - Creating XML Reader 399 INFO YahooParser - Parsing XML Response 474 INFO WeatherFormatter - Formatting Weather Data ********************************* Current Weather Conditions for: New York, NY, US Temperature: 44 Condition: Fair Humidity: 40 Wind Chill: 40 ********************************* The In Maven 1, a build was customized by stringing together a series of
plugin goals. Each plugin goal had prerequisites and defined a
relationship to other plugin goals. With the release of Maven 2, a
lifecycle was introduced and plugin goals are now associated with a
series of phases in a default Maven build lifecycle. The lifecycle
provides a solid foundation that makes it easier to predict and manage
the plugin goals which will be executed in a given build. In Maven 1,
plugin goals related to one another directly; in Maven 2, plugin goals
relate to a set of common lifecycle stages. While it is certainly
valid to execute a plugin goal directly from the command line as we
just demonstrated, it is more consistent with the design of Maven to
configure the Assembly plugin to execute the The following plugin configuration configures the Maven Assembly
plugin to execute the Configuring Attached Goal Execution During the Package Lifecycle Phase. <project> [...] <build> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> <executions> <execution> <id>simple-command</id> <phase>package</phase> <goals> <goal>attached</goal> </goals> </execution> </executions> </plugin> </plugins> </build> [...] </project>
Once you have this configuration in your POM, all you need to do to
generate the assembly is run |