多模块Maven项目使用maven-assembly-plugin插件的问题

项目结构:
Parent
|-child1
|-pom.xml
|-child2
|-pom.xml
|-pom.xml

父项目插件配置:

<plugin>
	<artifactId>maven-assembly-plugin</artifactId>
	<version>2.4</version>
	<configuration>
		<descriptors>
			<descriptor>assemble.xml</descriptor>
		</descriptors>
	</configuration>
</plugin>
assemble插件配置:
<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>lib</id>
	<formats>
		<format>zip</format>
	</formats>
	<moduleSets>
		<moduleSet>
			<includes>
				<include>*:*</include>
			</includes>
			<binaries>
				<outputDirectory>/</outputDirectory>
				<unpack>false</unpack>
			</binaries>
		</moduleSet>
	</moduleSets>
</assembly>

以上配置全部可以在Maven官网assemble插件的介绍中找到,但将assembly:assembly命令绑定到package生命周期,并运行mvn package后,会出现错误:
Failed to create assembly: Artifact: xxx:xxx:jar:0.0.1-SNAPSHOT (included by module) does not have an artifact with a file. Please ensure the package phase is run before the assembly is generated.
在百度不出任何结果的情况下,也只能在GOOGLE上查找答案,结果找到了这么一段话:
This is a design flaw with the 2.0.x (and for now, the 2.1.x/trunk) Maven code. In a multimodule build, when child modules specify the parent pom in their <parent/> section, it forces Maven to sort the parent pom's build ahead of those children. Since you're binding the assembly plugin to the parent pom and (I'm assuming) your child poms declare this parent as a <parent />, the parent's build runs first...including the execution of the assembly plugin. At this stage of the build, the child modules' binary artifacts don't exist yet, and any reference to them will cause the assembly plugin execution to fail.
也就是说,Maven会先执行Parent的package,而后再去执行子模块的package,此时由于assembly:assembly的绑定,assembly插件也会随Parent的package执行而执行。由于子模块的package始终在父模块的package之后,所以出现了上述的问题。

文章中同样提出了解决问题的方法:
1、将assembly:assembly命令与package解除绑定,并运行mvn package assembly:assembly,即可成功(分开运行mvn package和mvn assembly:assembly仍会报错,原因暂且不明);
2、在Parent下专门建立一个用于使用assembly插件的子模块,并通过依赖的方式,进行打包的操作。

你可能感兴趣的:(assembly)