9

I'm working on a project that used ant. I had a target dist that would basically do jar first, and then install the application into a directory.

This means, it would create directories like bin/, lib/ and config/ in the installation directory, and then copy the relevant files into each of these directories.

My question is two-fold:

  1. Is there any maven goal that does this kind of thing?
  2. If not, I want to do maven dist and make this happen. How would you suggest I accomplish this using Maven?
  3. If I can't have my own "target" (like dist), then what would be the best way?

Bottom line: I want to do all this, but don't want to alter the behavior of the default "targets" like compile and package etc.

Thanks, jrh

PS: I'm using maven version 2.2.21

4 Answers 4

8

I don't know what would go in config, but lib and bin is easy.

To copy all dependencies to a folder just do this:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>
                ${project.build.directory}/dist/lib
                </outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

To output your jar to a bin folder do this (reference page):

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <finalName>${project.artifactId}</finalName>
        <outputDirectory>${project.build.directory}/dist/bin</outputDirectory>
    </configuration>
</plugin>

Ah, there are additional requirements:

Bottom line: I want to do all this, but don't want to alter the behavior of the default "targets" like compile and package etc.

In this case I'd use a profile to turn this on:

<profile>
    <id>dist</profile>
    <build>
        <plugins>
            <!-- insert stuff from above here -->
        </plugins>
    </build>
</profile>

Now you would do mvn clean package -Pdist to get your dist directory and if you don't add the profile, you get default behaviour.

Basically, things work differently in maven from the way they do in ant. There are no targets, there are only lifecycle phases and plugin goals.

You can either execute a lifecycle phase, which will call all maven plugin goals that are bound to all phases up to this one (e.g. if you do mvn compile, the following phases will be executed: validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile). But there is no (easy) way to define a lifecycle phase named dist.

Or you can execute a specific plugin goal (you can actually execute multiple phases and / or plugin goals). E.g. you could write your own dist plugin and call it using mvn dist:dist, but I wouldn't recommend that because you are using existing functionality and the profile solution should be a pretty good fit.

3
  • okay, so how would this action kick in? Do I run mvn package? I want to do something like maven dist and make this happen (updated this in the question).. is that possible?
    – jrharshath
    Nov 22, 2010 at 14:17
  • +1 hm, helpful. Question: should I do what I want to do in the package lifecycle, or in the deploy? and why?
    – jrharshath
    Nov 23, 2010 at 4:33
  • @simpatico I guess this should be a comment to the question, not to my answer, right? May 5, 2011 at 12:19
2

You could try writing an assembly descriptor for the assembly plugin (or search google for a suitable one).

Something like this

<assembly 
  xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation=
     "http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 
      http://maven.apache.org/xsd/assembly-1.1.2.xsd">

  <id>dist</id>
  <formats><format>zip</format></formats>

  <fileSets>
    <fileSet>
      <directory>src/main/config</directory>
      <outputDirectory>config</outputDirectory>
      <useDefaultExcludes>true</useDefaultExcludes>
    </fileSet>
  </fileSets>

  <files>
    <file>
      <source>${project.build.directory}/${project.artifactId}.jar</source>
    </file>
  </files>

  <dependencySets>
    <dependencySet>
      <useProjectArtifact>false</useProjectArtifact>
      <outputDirectory>lib</outputDirectory>
    </dependencySet>
  </dependencySets>
</assembly>

will create a ${project.artifactId}-dist.zip inside your target directory. The zip file will be laid out like

yourProjectName/
yourProjectName/config/...
yourProjectName/lib/...
yourProjectName/${project.artifactId}.jar

It looks like the assembly plugin will only create compressed files, it won't just copy them to a dist folder.

1

Seans answer is good and I almost went for it until I found out about the appassembler plugin http://mojo.codehaus.org/appassembler/appassembler-maven-plugin/.

See here for an example https://github.com/khmarbaise/maven-training/tree/master/502-assembly-plugin

It is called as part of the package lifecycle.

 <build>
<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
    <configuration>
      <source>1.6</source>
      <target>1.6</target>
    </configuration>
  </plugin>
  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>appassembler-maven-plugin</artifactId>
    <version>1.1</version>
    <executions>
      <execution>
        <phase>prepare-package</phase>
        <goals>
          <goal>assemble</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <binPrefix>utility</binPrefix>
      <assembleDirectory>${project.build.directory}/appassembler</assembleDirectory>
      <extraJvmArguments>-Xms512m -Xmx1024m</extraJvmArguments>
      <generateRepository>false</generateRepository>
      <repositoryName>lib</repositoryName>
      <repositoryLayout>flat</repositoryLayout>
      <includeConfigurationDirectoryInClasspath>true</includeConfigurationDirectoryInClasspath>
      <platforms>
        <platform>windows</platform>
        <platform>unix</platform>
      </platforms>
      <programs>
        <program>
          <mainClass>com.soebes.tools.cli.UtilityCLI</mainClass>
          <name>utility</name>
        </program>
      </programs>
    </configuration>
  </plugin>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.2</version>
    <configuration>
      <descriptors>
        <descriptor>src/main/assembly/bin.xml</descriptor>
        <descriptor>src/main/assembly/bin-unix.xml</descriptor>
        <descriptor>src/main/assembly/src.xml</descriptor>
      </descriptors>
      <tarLongFileMode>gnu</tarLongFileMode>
    </configuration>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>single</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
</plugins>

The descriptors it references are fairly straigtforward and it also creates a batch and shell script for you to run your application!

0

You cannot have maven dist, with NetBeans (and other IDEs I believe) you can create a custom action dist which executes as mvn install -Pdist (or mvn clean package -Pdist, as suggested by Sean).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.