在Maven工程中偶尔会遇到自主平台的项目相互依赖的情况,有时候我们希望在被依赖的项目jar包中使用的某些配置或少量页面等资源一同打包到jar文件里,在依赖项目中在解压到指定目录下来完成资源的转移,而不是手动的将被依赖项目中的配置或其他资源copy到新项目中使用,这样太麻烦且不利于统一管理。
查询了网上许多资料推荐后,决定使用Apache Maven Dependency Plugin来完成此项解压和copy的操作,同时搭配Apache Maven Resources Plugin来进行资源的打包,从而实现关键资源的自动打包与解压操作。
Apache Maven Dependency Plugin中比较常用的功能有:
dependency:copy 用来拷贝某一个或多个特定文件到指定目录;
dependency:copy-dependencies 用来拷贝依赖的文件到指定目录;
dependency:unpack 与 copy 类似,但会解压文件;
dependency:unpack-dependencies 与 copy-dependencies 类似,但会解压文件;
日常开发中,我常通过 dependency:unpack 功能搭配 Apache Maven Resources Plugin 提供的打包功能来进行特定资源的自动发布。此处也以 dependency:unpack 为主,进行主要配置介绍。
dependency:unpack 中参数:
Name | Type | Since | Description |
---|---|---|---|
artifact | String | 1.0 | The artifact to unpack from commandLine. UseartifactItems within the pom-configuration.User property is: artifact. |
artifactItems | List | 1.0 | Collection of ArtifactItems to work on. (ArtifactItem contains groupId, artifactId, version, type, classifier, outputDirectory, destFileName and overWrite.) See Usagefor details. |
excludes | String | 2.0-alpha-5 | A comma separated list of file patterns to exclude when unpacking the artifact. i.e. **\/*.xml,**\/*.properties NOTE: Excludes patterns override the includes. (component code = return isIncluded( name ) AND !isExcluded( name );)User property is: mdep.unpack.excludes. |
ignorePermissions | boolean | 2.7 | ignore to set file permissions when unpacking a dependencyDefault value is: false.User property is: dependency.ignorePermissions. |
includes | String | 2.0-alpha-5 | A comma separated list of file patterns to include when unpacking the artifact. i.e. **\/*.xml,**\/*.properties NOTE: Excludes patterns override the includes. (component code = return isIncluded( name ) AND !isExcluded( name );)User property is: mdep.unpack.includes. |
localRepositoryDirectory | File | 2.2 | Path to override default local repository during plugin's execution. To remove all downloaded artifacts as part of the build, set this value to a location under your project's target directory |
markersDirectory | File | 1.0 | Directory to store flag files after unpackDefault value is: ${project.build.directory}/dependency-maven-plugin-markers. |
outputAbsoluteArtifactFilename | boolean | 2.0 | Output absolute filename for resolved artifactsDefault value is: false.User property is: outputAbsoluteArtifactFilename. |
outputDirectory | File | 1.0 | Default output location used for mojo, unless overridden in ArtifactItem.Default value is: ${project.build.directory}/dependency.User property is: outputDirectory. |
overWriteIfNewer | boolean | 2.0 | Overwrite if newerDefault value is: true.User property is: mdep.overIfNewer. |
overWriteReleases | boolean | 1.0 | Overwrite release artifactsDefault value is: false.User property is: mdep.overWriteReleases. |
overWriteSnapshots | boolean | 1.0 | Overwrite snapshot artifactsDefault value is: false.User property is: mdep.overWriteSnapshots. |
silent | boolean | 2.0 | If the plugin should be silent.Default value is: false.User property is: silent. |
skip | boolean | 2.7 | Skip plugin execution completely.Default value is: false.User property is: mdep.skip. |
useJvmChmod | boolean | 2.5.1 | will use the jvm chmod, this is available for user and all level group level will be ignored since 2.6 is on by defaultDefault value is: true.User property is: dependency.useJvmChmod. |
其中常用参数有:
artifactItems:用来包含声明需要解压的文件元素;
artifactItem:声明需要解压的文件;
outputDirectory:定义解压后输出的文件夹;
includes:定义输出包含的规则;
excludes:定义输出排除的规则。
官方提供了,实际使用时的简易配置如下:
[...] [...] org.apache.maven.plugins maven-dependency-plugin 2.10 unpack package unpack junit junit 3.8.1 **/*.jsp,**/*.html ${project.build.directory}/alternateLocation
需要注意一下的是,在配置 plugin 时,如果将其定义在<pluginManagement></pluginManagement>标签中时,需要在外面重新引用下该 plugin ,否则将不会生效。原因与 <pluginManagement> 标签的功能有关,相关信息可查阅<pluginManagement> 标签的功能。
除此以外,若 <pluginManagement> 中声明的 goal 与 <plugin> 标签引用中的 goal 不相同时,该插件会执行两次分别完成两个目标,执行时会合并两次的配置参数。
dependency:unpack-dependencies 与 dependency:unpack 功能类似,用来解压所有依赖的文件,因此不需要指定 artifactItems。dependency:copy 和 dependency:copy-dependencies 功能参照 unpack 即可,只是少了解压的过程。
参考来源: