将jar打包到具有独立外部资源和依赖关系的dist目录中 [英] Packaging a jar into a dist dir with separated external resources and dependencies

查看:102
本文介绍了将jar打包到具有独立外部资源和依赖关系的dist目录中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我想要实现的目标 - 一个 dist 目录(或一个zip文件),如下所示:

Here's what I'm trying to achieve - a dist directory (or a zip file) that looks like this:

dist/
|-- application-1.0.jar
|-- conf/
    |-- application.properties
    |-- log4j.properties
|-- lib/
    |-- *.jar

基本上:


  • 生成一个可执行jar(清单中有适当的类路径)

  • 我想排除 src / main / resources 自动与jar一起打包,以便 application.properties 可修改

  • 我想在 lib / 目录中拥有外部依赖项

  • An executable jar is produced (with appropriate classpath in the manifest)
  • I want to exclude src/main/resources from being automatically packaged with the jar, so that application.properties can be modified
  • I want to have external dependencies in the lib/ directory

我想出了一个解决方案,使用附带插件的配置文件的配置文件,但使用汇编插件是一个更好的解决方案吗?

I came up with a solution using a profile with plugins attached to the package phase, but would using the assembly plugin be a better solution?

推荐答案

使用汇编插件的解决方案有几个部分:

The solution using the assembly plugin has a few parts:


  • pom包括配置jar插件( maven-jar-plugin ),以及配置程序集插件( maven-assembly-plugin )。

  • 在maven的打包阶段,调用jar插件构建应用程序jar。

  • 然后程序集插件运行,并将构造的jar,资源和依赖项组合到一个zip文件中,该文件由程序集文件( distribution-zip.xml )定义。

  • The pom includes configuring the jar plugin (maven-jar-plugin), and configuring the assembly plugin (maven-assembly-plugin).
  • During maven's packaging phase, the jar plugin is called to construct the application jar.
  • Then the assembly plugin is run, and combines the constructed jar, resources and dependencies into a zip file as defined by the assembly file (distribution-zip.xml).

在pom中,配置插件:

In the pom, configure the plugins:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <archive>
                    <!-- Make an executable jar, adjust classpath entries-->
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>./lib/</classpathPrefix>
                        <mainClass>com.acme.KillerApp</mainClass>
                    </manifest>
                    <!--Resources will be placed under conf/-->
                    <manifestEntries>
                        <Class-Path>./conf/</Class-Path>
                    </manifestEntries>
                </archive>
                <!--exclude the properties file from the archive-->
                <excludes>
                    <exclude>*.properties</exclude>
                </excludes>
            </configuration>
        </plugin>

        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.2-beta-5</version>
            <configuration>
                <descriptors>
                    <descriptor>${basedir}/assembly/distribution-zip.xml</descriptor>
                </descriptors>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
...

汇编文件的内容分发 - zip.xml (感谢 Neeme Praks )结合了创建的jar,资源和依赖项:

The contents of the assembly file distribution-zip.xml (with thanks to Neeme Praks) combines the created jar, resources and dependencies:

<assembly>
    <id>dist</id>
    <formats>
        <format>zip</format>
    </formats>

    <includeBaseDirectory>true</includeBaseDirectory>

    <dependencySets>
        <dependencySet>
            <!--Include runtime dependencies-->
            <outputDirectory>lib</outputDirectory>
            <scope>runtime</scope>
        </dependencySet>
    </dependencySets>

    <fileSets>
        <fileSet>
            <!--Get the generated application jar-->
            <directory>${project.build.directory}</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
        <fileSet>
            <!--Get application resources-->
            <directory>src/main/resources</directory>
            <outputDirectory>conf</outputDirectory>
        </fileSet>
        <fileSet>
            <!--Get misc user files-->
            <directory>${project.basedir}</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>README*</include>
                <include>LICENSE*</include>
                <include>NOTICE*</include>
            </includes>
        </fileSet>       
    </fileSets>
</assembly>

生成的可分发zip文件创建类似 target / killer-app-1.0 -dist.zip

The resulting distributable zip file is created like target/killer-app-1.0-dist.zip!

这篇关于将jar打包到具有独立外部资源和依赖关系的dist目录中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆