maven plugin assembly

使用 Maven Assembly 插件的预定义装配描述符生成一个可分发的 JAR 文件的过 程,该文件包含了项目的二进制文件和所有的依赖。
Maven Assembly 插件是一个用来创建你应用程序特有分发包的插件。 你可以使
用 Maven Assembly 插件以你希望的任何形式来装配输出,只需定义一个自定义的 装配描述符。 后面的章节我们会说明如何创建一个自定义装配描述符,为 Simple Weather 应用程序生成一个更复杂的存档文件。 本章我们将会使用预定义的 jar- with-dependencies 格式。 要配置 Maven Assembly 插件, 我们需要在 pom.xml 中的 build 配置中添加如下的 plugin 配置。
例 4.19. 配置 Maven 装配描述符
<project> [...]
<build> <plugins>
<plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration>
<descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs> </configuration>
</plugin> </plugins>
</build>
[...] </project>
添加好这些配置以后,你可以通过运行 mvn assembly:assembly 来构建这个装配。
$ mvn install assembly:assembly
...
[INFO] [jar:jar]
[INFO] Building jar: ~/examples/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: \
64
~/examples/simple-weather/target/simple-weather-1.0-jar-with-dependencies.jar
在 target/simple-weather-1.0-jar-with-dependencies.jar 装配好之后, 我们可以在 命令行重新运行 Main 类。在你项目的基础目录下运行以下命令:
$ cd target
$ java -cp simple-weather-1.0-jar-with-dependencies.jar org.sonatype.mavenbook.weath 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
*********************************
jar-with-dependencies 格式创建一个包含所有 simple-weather 项目的二进制代码以 及所有依赖解压出来的二进制代码的 JAR 文件。 这个略微非常规的格式产生了一个 9 MiB 大小的 JAR 文件,包含了大概 5290 个类。 但是它确实给那些使用 Maven 开发 的应用程序提供了一个易于分发的格式。

猜你喜欢

转载自shixin42.iteye.com/blog/1705621