使用额外的非必要文件和批处理文件构建基于非 webapp maven2 的项目

发布于 2024-07-30 11:20:49 字数 822 浏览 6 评论 0原文

我刚刚开始掌握 maven2 的设置,同时移植我的一个简单项目。 我已经浏览了 包命令行示例在 Sonatype 网站上,但我对如何扩展和更改该项目的打包有点困惑。 我试图找到有关此主题的更多信息,但我认为我要么对问题的思考错误,要么对搜索的措辞错误。

本质上,我想构建一个项目,该项目将是所有依赖项 jar、主 jar 本身、为方便起见启动它的批处理脚本以及应用程序的其他各种文件(如属性文件或其他文件)的 zip。 但是,我不希望将这些全部捆绑到罐子中。 我只想有一个项目的压缩存档版本,其中包含所有 jar 的 lib 目录,根目录包含属性文件和批处理脚本,也许还有子文件夹来保存额外的非必要文件。 有点像:

  • sample-proj.zip:
    • easy.bat
    • props.ini
      • dependent1.jar
      • dependent2.jar
      • main.jar
    • 子目录
      • 一些额外文件.txt

使用 maven2 构建这样的项目的正确方法是什么? 我是否需要创建一个构建 zip 并将子项目作为模块包含的父项目? 这只是一个简单的项目,不需要多模块......

I'm just beginning to grasp the setup of maven2 while porting over a simple project of mine. I've ran through the package command line example on the Sonatype web site but I'm a bit confused of how I could expand and change the packaging of this project. I've tried to find more information on this subject but I think I'm either thinking about the problem wrong or phrasing the searches wrong.

Essentially I want to build a project that would be a zip of all the dependency jars, the main jar itself, batch scripts to start it for convenience and maybe other various files for the application (like properties files or something). However, I don't want these all bundled into the jar. I'd like to just have a compressed archive build of the project with a lib directory of all the jars, the root containing properties files and batch scripts and maybe sub folders to hold extra non-essential files. Kind of like:

  • sample-proj.zip:
    • easy.bat
    • props.ini
    • lib
      • dependent1.jar
      • dependent2.jar
      • main.jar
    • sub_dir
      • someextrafile.txt

What's the correct way of building a project like this using maven2? Do I need to make a parent project that builds the zip and includes the sub project as a module? This would be just a simple project that wouldn't need to be multi-module...

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

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

发布评论

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

评论(2

完美的未来在梦里 2024-08-06 11:20:49

以 Matt B 的答案为基础。 以下是如何使用引用的插件的一些示例。 请注意,执行绑定到集成测试阶段,以确保在尝试打包之前已创建 jar。

appassembler-maven-plugin 将生成批处理/shell 文件,下载依赖项(在本例中为 lib 目录),并将所有内容放在 target/appassembler 中

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>appassembler-maven-plugin</artifactId>
    <version>1.0</version>
    <executions>
      <execution>
        <id>assemble-standalone</id>
        <phase>integration-test</phase>
        <goals>
          <goal>assemble</goal>
          <!--if you only want to create the repo and skip script generation, 
             use this goal instead-->
          <!--goal>create-repository</goal-->
        </goals>
        <configuration>
          <programs>
            <program>
              <mainClass>name.seller.rich.Foo</mainClass>
              <name>foo</name>
            </program>
          </programs>
          <platforms>
            <platform>windows</platform>
            <platform>unix</platform>
          </platforms>
          <repositoryLayout>flat</repositoryLayout>
          <repositoryName>lib</repositoryName>
        </configuration>
      </execution>
    </executions>
  </plugin>

程序集插件可用于将 appassembler 输出打包到 zip 中:

<profiles>
  <profile>
    <id>archive</id>
    <build>
      <plugins>
        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>2.2-beta-2</version>
          <executions>
            <execution>
              <phase>integration-test</phase>
              <goals>
                <goal>single</goal>
              </goals>
              <configuration>
                <descriptors>
                  <descriptor>src/main/assembly/archive.xml</descriptor>
                </descriptors>
              </configuration>
            </execution>
          </executions>
        </plugin>    
      </plugins>
    </build>
  </profile>
</profiles>

程序集描述符如下所示:

<assembly>
  <id>archive</id>
  <formats>
    <format>zip</format>
  </formats>
  <fileSets>
    <fileSet>
     <directory>${project.build.directory}/appassembler</directory>
     <outputDirectory>/</outputDirectory>
    </fileSet>
  </fileSets>
  <!-- add any other filesets to include docs, readmes, licences etc here -->
</assembly>

Building on matt b's answer. Here's some examples of how to use the referenced plugins. Note the executions are bound to the integration-test phase to ensure the jar has been created before attempting to package it.

The appassembler-maven-plugin will generate batch/shell files, download the dependencies (in this example to the lib directory), and place all the contents in target/appassembler

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>appassembler-maven-plugin</artifactId>
    <version>1.0</version>
    <executions>
      <execution>
        <id>assemble-standalone</id>
        <phase>integration-test</phase>
        <goals>
          <goal>assemble</goal>
          <!--if you only want to create the repo and skip script generation, 
             use this goal instead-->
          <!--goal>create-repository</goal-->
        </goals>
        <configuration>
          <programs>
            <program>
              <mainClass>name.seller.rich.Foo</mainClass>
              <name>foo</name>
            </program>
          </programs>
          <platforms>
            <platform>windows</platform>
            <platform>unix</platform>
          </platforms>
          <repositoryLayout>flat</repositoryLayout>
          <repositoryName>lib</repositoryName>
        </configuration>
      </execution>
    </executions>
  </plugin>

The assembly plugin can be used to package the appassembler output into a zip:

<profiles>
  <profile>
    <id>archive</id>
    <build>
      <plugins>
        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>2.2-beta-2</version>
          <executions>
            <execution>
              <phase>integration-test</phase>
              <goals>
                <goal>single</goal>
              </goals>
              <configuration>
                <descriptors>
                  <descriptor>src/main/assembly/archive.xml</descriptor>
                </descriptors>
              </configuration>
            </execution>
          </executions>
        </plugin>    
      </plugins>
    </build>
  </profile>
</profiles>

And the assembly descriptor looks something like this:

<assembly>
  <id>archive</id>
  <formats>
    <format>zip</format>
  </formats>
  <fileSets>
    <fileSet>
     <directory>${project.build.directory}/appassembler</directory>
     <outputDirectory>/</outputDirectory>
    </fileSet>
  </fileSets>
  <!-- add any other filesets to include docs, readmes, licences etc here -->
</assembly>
墨落画卷 2024-08-06 11:20:49

appassembler 插件 完全按照您的描述进行操作:创建 shell 脚本,然后打包增加依赖关系。

但是我不相信它将它们打包到 zip/tar.gz/etc 中(appassembler 只是将它们放入 target/appassembler 下的文件夹结构中。但是,您可以使用 maven-assemble 插件 与 appassembler 配合以打包该输出 - maven- assembly在生成 zip/jar/tar.gz/bzip2/whatever 方面做得非常出色,并且您可以使用 XML 描述符完全控制文件中的内容。

The appassembler plugin does exactly what you describe: create shell scripts, and package up the dependencies.

However I don't believe that it packages them up into a zip/tar.gz/etc (appassembler just puts them into a folder structure under target/appassembler. But, you can use the maven-assembly plugin in concert with appassembler to just package up that output - maven-assembly does an excellent job of producing a zip/jar/tar.gz/bzip2/whatever, and you can control what goes into the file completely with an XML descriptor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文