【问题标题】:maven: multi-module project assembly into single jarmaven:多模块项目组装成单个jar
【发布时间】:2011-02-11 11:09:51
【问题描述】:
我有一个多模块项目,想创建一个包含我所有模块的类的单个 jar。在我的父 POM 中,我声明了以下插件:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>bin</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
但是,在运行 mvn assembly:assembly 时,仅包含来自父文件夹(空)的源代码。如何将模块中的源代码包含到存档中?
【问题讨论】:
标签:
maven
maven-2
archive
maven-assembly-plugin
multi-module
【解决方案1】:
要将所有模块中的类打包到一个 jar 中,我做了以下操作:
创建了仅用于将所有其他模块的内容打包到单个 jar 中的附加模块。这通常被称为装配模块。尝试调用与目标 jar 文件相同的此模块。
在这个新模块的 pom.xml 中,我添加了 maven-assemby-plugin。该插件将所有类打包并将它们放在单个文件中。它使用额外的配置文件(第 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.在这个新模块的 pom.xml 中,我还添加了对所有其他模块的依赖项,包括父 pom。只有依赖中包含的模块才会被打包到目标 jar 文件中。
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>fwk-bam</artifactId>
<version>${project.version}</version>
</dependency>...
4.最后我在程序集模块中创建了程序集描述符(文件: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>
【解决方案3】:
预定义的bin 在这里不起作用。您必须使用与预定义的 bin 描述符类似的自定义描述符,但声明 moduleSet 以包含您的项目模块。