views:

551

answers:

2

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

+2  A: 

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.

matt b
Ah, thanks for the help. I guess I just wasn't understanding what plugins I should be using. This information + Rich's was exactly what I was looking for. Thanks.
rcl
+3  A: 

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>
Rich Seller
This is exactly what I was looking for. I was a bit confused by the binding of it to the integration-test phase but I figured it out: mvn integration-test -P archiveThanks!
rcl
If you run mvn install, the integration-test phase will also be executed. It just needs to be after packaging to ensure your jar is built before the application is packaged. So **mvn install -Parchive** will also work.
Rich Seller
Why did you exclude the assembly phase by the way? It wont build the bat files if activated-- just copies the libs (ignoring the override of the folder name but putting it flat, which is odd). I switch it to just <goal>assembly</goal> to get the right set up...
rcl
It's a copy-paste error. In my app I have my own batch file I want to use instead of the generated one. I'll update the post, thanks.
Rich Seller