java - 将一个 jar 打包到一个 dist 目录中,其中包含分离的外部资源和依赖项

标签 java maven-2 jar dependencies

这是我想要实现的目标 - 一个看起来像这样的 dist 目录(或 zip 文件):

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

基本上:

  • 生成一个可执行 jar(在 list 中有适当的类路径)
  • 我想排除src/main/resources自动打包jar,这样application.properties可以修改
  • 我想在lib/目录下有外部依赖

我提出了一个解决方案,该解决方案使用一个配置文件和附加到包阶段的插件,但是使用程序集插件会是更好的解决方案吗?

最佳答案

使用程序集插件的解决方案有几个部分:

  • pom包括配置jar插件(maven-jar-plugin)和配置assembly插件(maven-assembly-plugin)。
  • 在maven打包阶段,调用jar插件构建应用jar。
  • 然后运行程序集插件,并将构建的 jar、资源和依赖项组合成一个由程序集文件 (distribution-zip.xml) 定义的 zip 文件。

在pom中,配置插件:

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

程序集文件 distribution-zip.xml 的内容(感谢 Neeme Praks )结合了创建的 jar、资源和依赖项:

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

关于java - 将一个 jar 打包到一个 dist 目录中,其中包含分离的外部资源和依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4200695/

相关文章:

maven-2 - Windows 7 Maven 2 安装

java - 将 jar 可执行文件上传到 Azure 并运行

java - 文件: URI corresponding to a Windows path name look like?应该如何

java - 为什么我的 jar 文件拒绝分发?

java - 如何从字符串中提取和分离 double 值?

java - onCreate 被调用两次导致应用程序过早退出

java - 如何生成JSON响应的POJO类

java - 无法启动tomcat 7 : Caused by: java. lang.NoClassDefFoundError

java - 从 Maven 运行测试失败并停止构建 jar

java - 使用 maven 构建后未找到 src/main/resources 中的资源