有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

本质上,我想构建一个项目,它包含所有依赖项jar、主jar本身、为方便而启动它的批处理脚本,以及应用程序的其他各种文件(如属性文件等)。但是,我不想把这些都塞进罐子里。我想要一个项目的压缩归档构建,包含所有jar的lib目录,根目录包含属性文件和批处理脚本,可能还有子文件夹来保存额外的非必要文件。有点像:

  • 样本项目。邮编:
    • 简单。球棒
    • 道具。伊尼
    • 解放党
      • 家属1。罐子
      • 家属2。罐子
      • 梅因。罐子
    • 副处长
      • 一些额外的文件。文本

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


共 (2) 个答案

  1. # 1 楼答案

    appassembler plugin与您描述的完全一样:创建shell脚本,并打包依赖项

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

  2. # 2 楼答案

    基于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>
    

    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>