Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Maven assembly plugin with multi module maven project

I am new to maven and spent ~3 days in generating the zip file with assembly plugin refering to http://www.petrikainulainen.net/programming/tips-and-tricks/creating-a-runnable-binary-distribution-with-maven-assembly-plugin/ My project is multi module, so I also referred to Managing multi-module dependencies with Maven assembly plugin

Still I have several inefficiencies. Below is assembly.xml (which I inherited from 1st link)

 <assembly>
<id>bin</id>
<!-- Generates a zip package containing the needed files -->
<formats>
    <format>zip</format>
</formats>

<!-- Adds dependencies to zip package under lib directory -->
<dependencySets>
    <dependencySet>
        <!-- Project artifact is not copied under library directory since it is 
            added to the root directory of the zip package. -->
        <useProjectArtifact>false</useProjectArtifact>
        <outputDirectory>lib</outputDirectory>
        <unpack>false</unpack>

    </dependencySet>
</dependencySets>
<moduleSets>
    <moduleSet>

        <!-- Enable access to all projects in the current multimodule build! <useAllReactorProjects>true</useAllReactorProjects> -->

        <!-- Now, select which projects to include in this module-set. -->
        <includes>
            <include>com.XX:YY-main</include>
        </includes>

        <!--<binaries> <outputDirectory>YY-main/target</outputDirectory> <unpack>false</unpack> 
            </binaries> -->
    </moduleSet>
</moduleSets>
<fileSets>
    <!-- Adds startup scripts to the root directory of zip package. The startup 
        scripts are located to src/main/scripts directory as stated by Maven conventions. -->
    <fileSet>
        <directory>${project.build.scriptSourceDirectory}</directory>
        <outputDirectory>conf</outputDirectory>
        <includes>
            <include>*</include>
        </includes>
    </fileSet>
    <fileSet>
        <directory>${project.build.directory}</directory>
        <outputDirectory></outputDirectory>
        <includes>
            <include>log4j.properties</include>
        </includes>
    </fileSet>
    <!-- adds jar package to the root directory of zip package -->
    <fileSet>
        <directory>${project.build.directory}</directory>
        <outputDirectory></outputDirectory>
        <includes>
            <include>*with-dependencies.jar</include>
        </includes>
    </fileSet>
</fileSets>

Below is the pom.xml (primary or main)

 <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4</version>
             <configuration>                    <descriptors>
              <descriptor>YY-main/src/main/assembly/assembly.xml</descriptor>   
         <!-- <descriptor>DummyAssembly.xml</descriptor> -->

                </descriptors>
            </configuration>
        </plugin>   

Questions:

1)Since assembly.xml is referred in primary pom, all submodules also get zipped. How can I specify only certain submodules to be zipped, while all others get ignored. I tried to move YY-main/src/main/assembly/assembly.xml from main pom to child/submodule pom and have dummyassembly.xml in main which does nothing. But that is not working (possibly because dummyassembly.xml is missing some required lines). tried few things, but nothing seem to work

2)Also in assembly.xml (dependencyset), it is copying all the libraries to "lib" folder. How can I avoid this. again I tried few things (exclusion..) based on http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html#class_dependencySet but nothing worked.

Can some one provide me with specific statements that I should change in my pom or assembly files to address 1 and 2-thanks

like image 321
kashili kashili Avatar asked Jun 06 '14 00:06

kashili kashili


People also ask

How can I add an existing Maven project to another Maven project as a module?

Just do a regular "Import existing maven project into workspace" to get this done. Show activity on this post. If you use M2e with Eclipse you do not need to do that, because Eclipse resolves dependencies across the workspace. You just need to have the two projects open and your dependencies declared correctly.

What is the use of multi-module project in Maven?

A multi-module project is built from an aggregator POM that manages a group of submodules. In most cases, the aggregator is located in the project's root directory and must have packaging of type pom. The submodules are regular Maven projects, and they can be built separately or through the aggregator POM.

Does Maven support multi project build?

As seen in the introduction to the POM, Maven supports project aggregation in addition to project inheritance. This section outlines how Maven processes projects with multiple modules, and how you can work with them more effectively.


2 Answers

The basic thing you should change is to create a separate module where you do the packaging which will look like this.

   +-- root (pom.xml)
        +--- mod-1 (pom.xml)
        +--- mod-2 (pom.xml)
        +--- mod-assembly (pom.xml)

The pom in the mod-assembly will look like this:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>org.test.parent</groupId>
    <artifactId>root</artifactId>
    <version>1.0.0-SNAPSHOT</version>
  </parent>

  <artifactId>dist</artifactId>
  <packaging>pom</packaging>

  <name>Packaging Test : Distribution</name>

  <dependencies>
    <dependency>
      <groupId>${project.groupId}</groupId>
      <artifactId>module-one</artifactId>
      <version>${project.version}</version>
    </dependency>
    <dependency>
      <groupId>${project.groupId}</groupId>
      <artifactId>module-two</artifactId>
      <version>${project.version}</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
          <execution>
            <id>make-bundles</id>
            <goals>
              <goal>single</goal>
            </goals>
            <phase>package</phase>
            <configuration>
              <descriptors>
                <descriptor>proj1-assembly.xml</descriptor>
              </descriptors>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

</project>

That will solve the problem with running maven-assembly-plugin in every child and the problem with the dummy descriptor. Here you can find a real example of such structure.

Furthermore having the modules in one folder and the dependencies in an other folder you can use a assembly descriptor like this:

  <id>bin</id>
  <formats>
    <format>zip</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <dependencySets>
    <dependencySet>
          <useProjectArtifact>false</useProjectArtifact>
          <useTransitiveDependencies>true</useTransitiveDependencies>
          <outputDirectory>lib</outputDirectory>
          <unpack>false</unpack>
          <excludes>
            <exclude>${project.groupId}:*:*</exclude>
          </excludes>
      </dependencySet>
  </dependencySets>
  <moduleSets>
    <moduleSet>
      <useAllReactorProjects>true</useAllReactorProjects>
      <binaries>
        <outputDirectory>modules</outputDirectory>
        <unpack>false</unpack>
      </binaries>
    </moduleSet>
  </moduleSets>
like image 92
khmarbaise Avatar answered Sep 26 '22 17:09

khmarbaise


I think the post How to Assemble Multi-Module Project with Maven might answer your question with an example.

My answers to your questions:

1) You should add the Assembly plugin in the parent pom within the build and pluginManagement tags to reuse it in the modules; the post mentioned above is a good example on how to do it. You can also take a look at 8.6 Best Practise chapter, topic 8.6.2, of the online book 'Maven: The Complete Reference'.

2) The exclusion tag might be tricky. Again, take a look at 8.5 Controlling the Contents of an Assembly chapter of the online book 'Maven: The Complete Reference'. For example, the subtopic 8.5.4, about dependencySets mentions how to fine tune dependency includes and excludes for dependencySets; again, the subtopic 8.5.5, now about moduleSets, shows how to use it in that context.

like image 31
Alexandre Avatar answered Sep 25 '22 17:09

Alexandre