开发者

maven: multi-module project assembly into single jar

I have 开发者_如何学Ca multi-module project and want to create a single jar containing the classes of all my modules. Inside my parent POM, I declared the following plugin:

<plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-assembly-plugin</artifactId>
 <configuration>
  <descriptorRefs>
   <descriptorRef>bin</descriptorRef>
  </descriptorRefs>
 </configuration>
</plugin>

However, when running mvn assembly:assembly, only the source from the parent folder (empty) are included. How do I include the sources from my modules into the archive?


I think you are looking for the Maven Shade Plugin:

http://maven.apache.org/plugins/maven-shade-plugin/index.html

Packages up any number of dependencies into an uber package depenency. This can then be deployed to a repository.


To package classes from all modules to a single jar I did the following:

  1. Created additional module that is used only for packing contents of all other modules to a single jar. This is usually reffered to as a assembly module. Try calling this module same as target jar file.

  2. In pom.xml of this new module i added maven-assemby-plugin. This plugin packages all classes and puts them in single file. It uses additional configuration file (step 4.)

<build>
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.4</version>
        <executions>
          <execution>
            <id>go-framework-assemby</id>
            <phase>package</phase><!-- create assembly in package phase (invoke 'single' goal on assemby plugin)-->
            <goals>
              <goal>single</goal>
            </goals>
            <configuration>
              <descriptors>
                <descriptor>src/main/assemble/framework_bin.xml</descriptor>
              </descriptors>
                  <finalName>framework</finalName>
                  <appendAssemblyId>false</appendAssemblyId>
          </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

3.In pom.xml of this new module I also added dependencies to all other modules including parent pom. Only modules included in dependencies will be packed in target jar file.

<dependencies>
    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>fwk-bam</artifactId>
        <version>${project.version}</version>
    </dependency>...

4.Finally i created assembly descriptor in assembly module (file: src/main/assemble/framework_bin.xml)

<assembly
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
    <id>all-jar</id>
    <formats>
        <format>jar</format> <!-- the result is a jar file -->
    </formats>

    <includeBaseDirectory>false</includeBaseDirectory> <!-- strip the module prefixes -->

    <dependencySets>
        <dependencySet>
            <unpack>true</unpack> <!-- unpack , then repack the jars -->
            <useTransitiveDependencies>false</useTransitiveDependencies> <!-- do not pull in any transitive dependencies -->
        </dependencySet>
    </dependencySets>
</assembly>


The predefined bin won't do the trick here. You'll have to use a custom descriptor similar to the predefined bin descriptor but that declares moduleSet to include your project modules.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜