
Maven遵循【约定大于配置】规范其强大之处其一就是有着丰富的插件生态系统。本文是对知识的一个汇总多参考于网络资源。versions-maven-plugin一行命令同时修改maven项目中多个module的版本号。一般略具规模的工程都是多module的项目父模块包含多个子模块。但是这样的项目在版本升级的时候就会比较麻烦因为要遍历的修改所有pom中的版本号。比如要把0.0.1升级到0.0.2那么就需要把所有的pom中的version都改掉。在最外层的pom文件中增加以下插件配置/pluginsplugingroupIdorg.codehaus.mojo/groupIdartifactIdversions-maven-plugin/artifactIdversion2.7/versionconfigurationgenerateBackupPomsfalse/generateBackupPoms/configuration/plugin/pluginsgenerateBackupPoms用于配置是否生成备份Pom用于版本回滚。配置好插件后执行命令mvn versions:set -DnewVersion0.0.2即可将以上例子中的所有版本号修改成0.0.2。为了方便使用还可以在linux上设置别名alias mvsmvs() { mvn versions:set -DnewVersion$1 }; mvs即可使用mvs命令一键修改版本号。license-maven-plugin功能检查三方库依赖的许可。最近Dubbo的依赖的某三方库发现使用了LGPL的依赖由于Dubbo在Apache基金会里面进行孵化需要确保所有的三方依赖都和Apache许可证进行兼容。也就是说任何LGPLGPL许可的三方依赖理论上都不能被纳入Dubbo的依赖无论是直接依赖还是传递依赖。解决方案为了解决这个问题maven的license插件提供许可证查询机制命令pom文件无需引入该GAVmvn license:add-third-party -Dlicense.useMissingFile上述命令会自动扫描所有模块当中的依赖包括直接和传递依赖输出到每个模块的target/generated-sources/license/THIRD-PARTY.txt文件下内容如下Lists of 114 third-party dependencies. (ASF 2.0) Code Generation Library (cglib:cglib-nodep:2.2 - http://cglib.sourceforge.net/)可看到有一些依赖是暴露双协议或者多协议这种情况下可选择兼容度最高的一种协议。例如logback作为一个典型的例子就是暴露的是EPLLGPL双协议而EPL属于在Apache基金金下的Category B即允许二进制形式的依赖所以logback是允许作为三方依赖出现的。为了排除这些双协议造成的干扰采用如下命令进行过滤后只剩下2条记录find.-nameTHIRD-PARTY.txt|xargsgrepGPL|grep-vApache|grep-vMIT|grep-vCDDL ./dubbo-registry/dubbo-registry-nacos/target/generated-sources/license/THIRD-PARTY.txt:(GNU Lesser General Public License(LGPL), Version2.1)Jackson(org.codehaus.jackson:jackson-core-lgpl:1.9.6 -http://jackson.codehaus.org)./dubbo-registry/dubbo-registry-nacos/target/generated-sources/license/THIRD-PARTY.txt:(GNU Lesser General Public License(LGPL), Version2.1)DataMapperforJackson(org.codehaus.jackson:jackson-mapper-lgpl:1.9.6-http://jackson.codehaus.org)上述的依赖需要进行清理nacos-client-1.0.0-RC3开始修复这个问题。但是三方依赖的optional依赖没法通过上述命令检测出来例如nacos-client的如下依赖也是LGPL用mvn license插件无法检查出来。dependencygroupIdcom.github.spotbugs/groupIdartifactIdspotbugs-annotations/artifactIdoptionaltrue/optional/dependency事实上maven license插件提供一个选项但是实际上对于三方依赖的optional依赖却不生效原因待确定。个人推测如果在nacos-client下运行上述命令应该是可以检查出来的。总结后续Dubbo每新增一个三方依赖都需要检查License的兼容情况尤其是三方依赖的optional依赖需要仔细检查目前没有特别好的办法。wagon-maven-plugin参考wagon-maven-plugin插件实现自动化构建部署到服务器。maven-replacer-plugin问题背景本地的一个有前后端的demo有用没有严格的前后端分离静态资源放在webapp下面。更新静态资源文件之后没有生效。问题原因浏览器缓存。解决方案使用 maven 的 com.google.code.maven-replacer-plugin 插件在项目打包 package 时自动为静态文件追加 xxx.js?vtime 的后缀从而解决浏览器修改后浏览器缓存问题此插件只会在生成 war 包源码时生效不需要修改任何代码。原始文件和最终生成效果原始文件script src${resource!}/js/xxx/xxx.js/script打包后script src${resource!}/js/xxx/xxx.js?v20180316082543/script使用步骤pom.xml 中插件添加properties!-- maven.build.timestamp 默认时间戳格式 --maven.build.timestamp.formatyyyyMMddHHmmss/maven.build.timestamp.format/propertiespluginsplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-war-plugin/artifactIdconfigurationuseCachetrue/useCache/configurationexecutions!-- 在打包之前执行打包后包含已经执行后的文件 --executionidprepare-war/idphaseprepare-package/phasegoalsgoalexploded/goal/goals/execution/executions/pluginplugingroupIdcom.google.code.maven-replacer-plugin/groupIdartifactIdreplacer/artifactIdversion1.5.3/versionexecutions!-- 打包前进行替换 --executionphaseprepare-package/phasegoalsgoalreplace/goal/goals/execution/executionsconfiguration!-- 自动识别到项目target文件夹 --basedir${build.directory}/basedir!-- 替换的文件所在目录规则 --includesinclude${build.finalName}/WEB-INF/views/*.html/includeinclude${build.finalName}/WEB-INF/views/**/*.html/include/includesreplacements!-- 更改规则在css/js文件末尾追加?v时间戳反斜杠表示字符转义 --replacementtoken\.css\/tokenvalue.css?v${maven.build.timestamp}\/value/replacementreplacementtoken\.css\/tokenvalue.css?v${maven.build.timestamp}\/value/replacementreplacementtoken\.js\/tokenvalue.js?v${maven.build.timestamp}\/value/replacementreplacementtoken\.js\/tokenvalue.js?v${maven.build.timestamp}\/value/replacement/replacements/configuration/plugin/plugins4.html中 css/js 文件引用规则文件引用结尾处必须是pom.xml文件中添加的规则script src${resource!}/js/xxx/xxx.jstypetext/javascript/scriptlink href${resource!}/css/xxx/xxx.cssrelstylesheettypetext/cssmaven-assembly-plugin将项目的依赖文件静态资源配置信息打包打包格式如zip、rar、tar.gz。官网文档pom.xml配置pluginartifactIdmaven-assembly-plugin/artifactIdconfigurationdescriptorsrc/main/assembly/assembly.xml/descriptor/configurationexecutionsexecutionidmake-assembly/idphasepackage/phasegoalsgoalsingle/goal/goals/execution/executions/plugin另外需配置一个assembly.xml文件maven-release-pluginmaven release plugin用于发布的插件功能如下把版本号SNAPSHOT去掉向maven私服发布一个稳定版本检查项目中所有外部依赖包是否包含SNAPSHOT在git上面建立tag作为release的milestone自动对当前版本号加1为下一个迭代SNAPSHOT版本做好准备pom.xmldistributionManagementrepositoryidreleases/idnamexxxx internal releases repository/nameurlhttp://xxx.com/nexus/content/repositories/releases/url/repositorysnapshotRepositoryidsnapshots/idnamexxxx internal snapshots repository/nameurlhttp://xxx.com/nexus/content/repositories/snapshots/url/snapshotRepository/distributionManagementscmurlhttp://xxx.com/project/urlconnectionscm:git:gitxxx.com/project.git/connectiondeveloperConnectionscm:git:gitxxx.com/project.git/developerConnectiontagHEAD/tag/scmtomcat8-maven-plugin最基本的功能本地无需安装Tomcat容器IDEA也无需配置Tomcat使用tomcat8-maven-plugin即可启动一个内嵌式的Tomcat容器。这属于常规用法。下面稍微进阶一下。犹记第一份工作本地开发环境是Windows系统应用却部署在Linux服务器里面。是故本地测试时需要打包应用生成war包用SSH工具上传到Linux服务器指定目录让Tomcat自动解包缩完成部署。怎么将这一过程自动化呢借助于tomcat8-maven-plugin插件利用Tomcat的manager模块来实现war包部署须确保Tomcat安装Manager模块一般默认安装或者看webapp下有没有Manager目录。对本地的Tomcat的manager模块进行一些配置打开本地Tomcat的tomcat-users.xml文件增加配置user usernamejohnny passwordawesome rolesmanager-script/角色设定为manager-script表明可使用Tomcat的manager模块的后台脚本管理如果角色为manager-gui则表示此用户可使用manager模块的网页管理功能。对测试服务器也是类似的配置。propertieswarPackageNamedemeApp/warPackageNametomcat.deploy.serverlocalDevServer/tomcat.deploy.servertomcat.deploy.serverUrlhttp://localhost/manager/text/tomcat.deploy.serverUrl/propertiesprofilesprofileiddeploy2test/idpropertiestomcat.deploy.servertestServer/tomcat.deploy.servertomcat.deploy.serverUrlhttp://host:8080/manager/text/tomcat.deploy.serverUrl/properties/profile/profilesbuildfinalName${warPackageName}/finalNamepluginsplugingroupIdorg.apache.tomcat.maven/groupIdartifactIdtomcat8-maven-plugin/artifactIdversion2.2/versionconfigurationserver${tomcat.deploy.server}/serverurl${tomcat.deploy.serverUrl}/urlpath/${warPackageName}/path/configuration/plugin/plugins/buildconfiguration.server使用变量tomcat.deploy.server实现将War包部署到不同服务器去本地开发时tomcat.deploy.server可被赋值为localDevServerlocalDevServer配置在~/.m2/settings.xml中servers!-- 可配置多个server表示不同的环境 --serveridlocalDevServer/idusernamejohnny/usernamepasswordawesome/password/server/serversconfiguration.url默认值是http://localhost/manager/text这个地址便是Tomcat的Manager模块的后台脚本入口。configuration.path指定War包部署位置如果path为/也就是部署为ROOT.WAR。部署成功之后可以通过http://localhost/path来访问。部署mvn tomcat8:deploy执行完命令后应用就被打包部署到配置的Tomcat服务器上。通过-p参数来指定配置的多个profile其一mvn tomcat8:deploy -Pdeploy2test移除程序使用undeploy即可。maven-surefire-plugin插件 maven-surefire-plugin 可用于执行 JUnit 或 TestNG 的测试用例。在默认情况下这个插件的test目标会自动执行测试源码路径下所有符合一组命名模式的测试类**/Test*.java任何子目录下所有命名以Test开关的Java类**/*Test.java任何子目录下所有命名以Test结尾的Java类**/*TestCase.java任何子目录下所有命名以TestCase结尾的Java类。pom.xml配置文件示例plugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-surefire-plugin/artifactIdversion2.20.1/versionconfigurationsuiteXmlFilessuiteXmlFile./src/test/resources/testng.xml/suiteXmlFile/suiteXmlFiles!--跳过测试运行等价于mvn package -DskipTests--skiptrue/skip!--跳过测试代码编译等价于mvn package -Dmaven.test.skiptrue--skipTeststrue/skipTests!--mvn install Dtesttest参数用于在命令行中指定要运行的测试用例。如 mvn test -DtestRandomGeneratorTest使用通配符mvn test -DtestRandom*Test 使用,号指定多个测试类mvn test -DtestRandom*Test,AccountCaptchaServiceTest--!--没有写UT也可以编译不进行强校验等同于mvn install Dtest -DfailIfNoTestsfalse--failIfNoTestsfalse/failIfNoTests!--用于额外的添加和排除--includesinclude/include/includesincludesFile/includesFileexcludesFile/excludesFileexcludedGroups/excludedGroupsexcludesexclude/exclude/excludes/configuration/plugin默认情况下此插件在项目的target/surefire-reports目录下生成两种格式的错误报告。简单文本格式——内容十分简单可以看出哪个测试项出错。与JUnit兼容的XML格式——XML格式已经成为Java单元测试报告的事实标准这个文件可以用其他的工具如IDE来查看。参考一行命令同时修改maven项目中多个module的版本号Maven插件wagon-maven-plugin自动化部署Java项目到Linux远程服务器maven-replacer-plugin 静态资源版本号解决方案用Maven部署war包到远程Tomcat服务器