Maven-assembly-plugin: custom jar filenames

23,702

Solution 1

The standard maven plugins are meant for general, repetitive work. They gather all information from the POM and they are smart (in the sense that you don't need to configure much).

If you need special tasks, then I suggest to use the ant plugin which allows you to embed a piece of Ant code in the POM. This allows you to run the jar task.

Solution 2

You can move the configuration element below the execution element of the plugin declaration. This means the configuration will only be applied to that execution. You can then add additional executions of the assembly plugin for your other assemblies.

Here is an example of the modified configuration with two executions, each referencing a different assembly:

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <executions>
    <execution>
      <id>make-business-assembly</id>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
      <configuration>
        <appendAssemblyId>false</appendAssemblyId>
        <finalName>app_business</finalName>
        <descriptors>
          <descriptor>src/main/assembly/business-assembly.xml</descriptor>
        </descriptors>
        <attach>true</attach>
      </configuration>
    </execution>
    <execution>
      <id>make-gui-assembly</id>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
      <configuration>
        <appendAssemblyId>false</appendAssemblyId>
        <finalName>app_gui</finalName>
        <descriptors>
          <descriptor>src/main/assembly/gui-assembly.xml</descriptor>
        </descriptors>
        <attach>true</attach>
      </configuration>
    </execution>
  </executions>
</plugin>

With this configuration, two additional jars (app _business.jar and app _gui.jar) will be created in the target directory, though be aware if you install the project, only the last artifact assembled will be installed (this could of course be a problem).

To avoid this you would need to change the appendAssemblyId properties to true. The closest you can get in this case is to change the finalNames to "app" and the IDs to "gui" and "business", resulting in app-gui.jar and app-business.jar being packaged and all artifacts being installed.

Share:
23,702
Synox
Author by

Synox

Updated on January 26, 2020

Comments

  • Synox
    Synox over 4 years

    I use the assembly plugin to create several jars with some classes in it. I need custom names for the resulting jars: app_business.jar app_gui.jar core.jar etc.

    Currently I have to following configuration:

        <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
            <appendAssemblyId>false</appendAssemblyId>
        <finalName>app_business</finalName>
            <descriptors>
                <descriptor>assembly.xml</descriptor>
            </descriptors>
            <attach>true</attach>
        </configuration>
        <executions>
            <execution>
                <id>make-assembly</id>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    

    an the assembly.xml file:

        <assembly>
          <id>app_business</id>
          <formats>
            <format>jar</format>
          </formats>
          <baseDirectory>target</baseDirectory>
          <includeBaseDirectory>false</includeBaseDirectory>
    
          <fileSets>
            <fileSet>
              <directory>${project.build.outputDirectory}</directory>
              <outputDirectory></outputDirectory> 
              <includes>
                <include>org/xyz/**</include>
              </includes>
            </fileSet>    
          </fileSets>
        </assembly>
    

    this creates a file app_business.jar which is perfect. But i have no idea how to create my other files. The option appendAssemblyId doesn't help me, as it creates filenames in the format AppName-app_business.jar. I really need the exact filesname app_business.jar.

    Any Idea? Thank you very much!

  • Synox
    Synox over 14 years
    Hey thanks for the really fast reply! it works. as you said there are warnings on the console, and the last artifact becomes the main artifact. I can live with that, but i done't like the warnings on the console. Is there a way to hide them (or configure assembly to do so)?
  • Synox
    Synox over 14 years
    I don't like to mix ant and maven tasks. Anyway, using the ant tasks needs much less xml-configuration. (about 20 lines for my job)
  • Rich Seller
    Rich Seller over 14 years
    what are the warnings? The last section of my answer addresses the main artifact issue, if you can stand to change from underscores to hyphens
  • Synox
    Synox over 14 years
    The warnings are: [WARNING] Configuration options: 'appendAssemblyId' is set to false, and 'classifier' is missing. Instead of attaching the assembly file: ...\app_business.jar, it will become the file for main project artifact. NOTE: If multiple descriptors or descriptor-formats are provided for this project, the value of this file will be non-deterministic! [WARNING] Replacing pre-existing project main-artifact file: ...\app_gui.jar with assembly file: ....\app_gui.jar I would like to avoid having to change from underscore to hypens. But if i can't avoid... this solution is quite nice.
  • bastianneu
    bastianneu over 14 years
    you can't avoid this at the moment. +1 for great Answer.
  • Rich Seller
    Rich Seller over 14 years
    Based on your assembly having the basedDirectory property set you're using a 2.2 version of the assembly-plugin, this should use the id property as the classifier, but it seems not to be. I'll have a look at this when I get achance and update the answer accordingly.
  • Rich Seller
    Rich Seller over 14 years
    @Synox, you shouldn't avoid the antrun plugin, it is very useful to fill in the gaps in Maven without rolling your own plugin. I just tend to find that people are a touch too keen to use it when there are more Mavenic (? whatever, the Maven equivalent of Pythonic) ways to do it. In this case I think the assembly plugin should be a better fit, but as you can see from my answer, there seems to still be a few issues with it that stop it working exactly as the OP wants.
  • Mykola Golubyev
    Mykola Golubyev almost 14 years
    It is easy to roll maven plugin using ant.
  • Rade_303
    Rade_303 over 9 years
    not really useful answer, does not really relates to the question asked.
  • Aaron Digulla
    Aaron Digulla over 9 years
    @RockyMM: I agree that Rich Seller's answer is much more useful.