将 jar 打包到具有分离的外部资源和依赖项的 dist 目录中

发布于 2024-10-03 01:21:12 字数 474 浏览 0 评论 0原文

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

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

基本上:

  • 生成了一个可执行 jar(在清单中具有适当的类路径)
  • 我想排除 < code>src/main/resources 自动与 jar 一起打包,以便可以修改 application.properties
  • 我希望在 lib/ 中有外部依赖项> 目录

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

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

Basically:

  • 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凡间太子 2024-10-10 01:21:12

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

  • pom 包括配置 jar 插件(maven-jar-plugin)和配置程序集插件(maven-assemble-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

The solution using the assembly plugin has a few parts:

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

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

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>

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

吝吻 2024-10-10 01:21:12

您需要使用两个插件来完成此操作:ma​​ven-jar-pluginma​​ven-assemble-plugin

有用的 pom.xml 示例:

(我建议您将用户可编辑的属性文件放置在单独的目录中,但这只是个人喜好的问题。)

示例程序集配置,帮助您入门:

<assembly>
  <id>dist</id>
  <formats>
    <format>zip</format>
  </formats>
  <includeBaseDirectory>true</includeBaseDirectory>
  <baseDirectory>dist</baseDirectory>
  <dependencySets>
    <dependencySet>
      <outputDirectory>lib</outputDirectory>
      <scope>runtime</scope>
    </dependencySet>
  </dependencySets>
  <fileSets>
    <fileSet>
      <directory>src/conf</directory>
      <outputDirectory>conf</outputDirectory>
    </fileSet>
    <fileSet>
      <directory>src/run</directory>
      <outputDirectory></outputDirectory>
      <excludes>
        <exclude>*.sh</exclude>
      </excludes>
    </fileSet>
  </fileSets>
  <files>
    <file>
      <source>src/run/run.sh</source>
      <outputDirectory></outputDirectory>
      <fileMode>0755</fileMode>
    </file>
  </files>
</assembly>

You need to use two plugins to accomplish this: maven-jar-plugin and maven-assembly-plugin.

Useful pom.xml samples:

(I would recommend you to separate the user-editable properties files to separate directory, but it is a matter of taste.)

Sample assembly configuration, to get you started:

<assembly>
  <id>dist</id>
  <formats>
    <format>zip</format>
  </formats>
  <includeBaseDirectory>true</includeBaseDirectory>
  <baseDirectory>dist</baseDirectory>
  <dependencySets>
    <dependencySet>
      <outputDirectory>lib</outputDirectory>
      <scope>runtime</scope>
    </dependencySet>
  </dependencySets>
  <fileSets>
    <fileSet>
      <directory>src/conf</directory>
      <outputDirectory>conf</outputDirectory>
    </fileSet>
    <fileSet>
      <directory>src/run</directory>
      <outputDirectory></outputDirectory>
      <excludes>
        <exclude>*.sh</exclude>
      </excludes>
    </fileSet>
  </fileSets>
  <files>
    <file>
      <source>src/run/run.sh</source>
      <outputDirectory></outputDirectory>
      <fileMode>0755</fileMode>
    </file>
  </files>
</assembly>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文