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

本文介绍如何使用Maven Assembly插件将项目及其所有依赖项打包成单一的Jar文件,以便于应用程序的分发和独立运行。

 

让编译出来的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

 

ContractedBlock.gifExpandedBlockStart.gifConfiguring 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.

 

转载于:https://www.cnblogs.com/forward/archive/2010/02/03/1662772.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值