【问题标题】:Building non-webapp maven2 based project with extra non-essential files and batch files使用额外的非必要文件和批处理文件构建基于非 webapp maven2 的项目
【发布时间】:2010-11-15 06:46:54
【问题描述】:

在移植我的一个简单项目时,我才刚刚开始掌握 maven2 的设置。我已经浏览了package command line example on the Sonatype web site,但我对如何扩展和更改这个项目的包装有点困惑。我已尝试查找有关此主题的更多信息,但我认为我要么是在考虑问题,要么是在搜索错误的措辞。

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

  • 示例项目.zip:
    • easy.bat
    • props.ini
    • 库
      • dependent1.jar
      • dependent2.jar
      • main.jar
    • 子目录
      • someextrafile.txt

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

【问题讨论】:

    标签: java maven-2 build-process build-automation


    【解决方案1】:

    appassembler plugin 完全按照您的描述进行:创建 shell 脚本,并打包依赖项。

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

    【讨论】:

    • 啊,谢谢你的帮助。我想我只是不明白我应该使用什么插件。这些信息 + Rich's 正是我想要的。谢谢。
    【解决方案2】:

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

    appassembler-maven-plugin会生成batch/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>
    

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

    【讨论】:

    • 这正是我想要的。我对它与集成测试阶段的绑定有点困惑,但我想通了: mvn integration-test -P archive 谢谢!
    • 如果你运行 mvn install,也会执行集成测试阶段。它只需要在打包之后,以确保在打包应用程序之前构建您的 jar。所以 mvn install -Parchive 也可以工作。
    • 为什么顺便排除了组装阶段?如果激活,它不会构建 bat 文件 - 只是复制库(忽略文件夹名称的覆盖但将其平放,这很奇怪)。我将其切换为 assembly 以进行正确的设置...
    • 这是一个复制粘贴错误。在我的应用程序中,我有自己的批处理文件,而不是生成的批处理文件。我会更新帖子,谢谢。
    猜你喜欢
    • 1970-01-01
    • 2012-02-11
    • 2021-09-18
    • 1970-01-01
    • 1970-01-01
    • 2013-02-13
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    相关资源
    最近更新 更多