现在的位置: 首页 > 综合 > 正文

maven 1: install assembly:assembly, 打包所有依赖

2013年03月28日 ⁄ 综合 ⁄ 共 2347字 ⁄ 字号 评论关闭

 

让编译出来的jar包,包含所有的依赖项,这样就可以独立运行了

 

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 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.

Configuring the Maven Assembly Descriptor

 

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 mvn 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*********************************

The jar-with-dependencies format creates a single JAR file which includes all of the bytecode from the simple-weather project and the unpacked bytecode from all of the dependencies. This somewhat unconventional format produces a 9 MiB JAR file 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.

 

抱歉!评论已关闭.