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

时间:2009-07-30 00:10:48

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

我刚刚开始掌握maven2的设置,同时移植我的一个简单项目。我已经浏览了package command line example on the Sonatype web site,但我对如何扩展和更改此项目的包装感到困惑。我试图找到关于这个主题的更多信息,但我认为我要么认为问题不对,要么错误地判断错误。

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

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

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

2 个答案:

答案 0 :(得分:6)

建立在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>

答案 1 :(得分:2)

appassembler plugin完全符合您的描述:创建shell脚本,并打包依赖项。

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