上一篇写了关于创建 maven archetype的文章 ,其中关于模块名称的修改写得比较详细,但是如果每次修改发布都这么修改,就比较麻烦。为此我写了一个批处理文件来处理内容,并自动打包,发布。
注意:我的子模块是maven-开头的,你如果不一样,请把文件中的内容改成跟你的一样。
1、新建一个maven.bat
@echo off
setlocal enabledelayedexpansion
::一键执行脚本
:: 先执行 mvn archetype:create-from-project
::如果有问题,请执行进行查看详细内容 mvn archetype:create-from-project -X
::修改模块文件夹名
::再到 target/generated-sources/archetype 目录下执行 mvn install
::发布到私服 mvn deploy
::记录根路径
set root=%cd%
echo ==========================清空原文件==========================
rd /q /s %root%\target
echo ==========================生成新文件==========================
call mvn archetype:create-from-project
echo ==========================修改文件名==========================
set mypath=%cd%\target\generated-sources\archetype\src\main\resources\archetype-resources
cd %mypath%
echo %mypath%
for /d %%i in (maven-*) do (
set n=%%i
set str=!n:maven-=__rootArtifactId__-!
echo ren %%i !str!
ren %%i !str!
)
echo 替换完成1
set mypath=%root%\target\generated-sources\archetype\target\classes\archetype-resources
cd %mypath%
echo %mypath%
for /d %%i in (maven-*) do (
set n=%%i
set str=!n:maven-=__rootArtifactId__-!
echo ren %%i !str!
ren %%i !str!
)
echo 替换完成2
cd %root%\target
echo ==========================修改xml内容==========================
powershell -Command "&{Set-ExecutionPolicy UnRestricted}"
for /r %%f in (archetype-metadata.xml*) do (
powershell ..\file.ps1 %%f
echo 替换完成%%f
)
for /r %%f in (pom.xml*) do (
powershell ..\pom.ps1 %%f
echo 替换完成%%f
)
echo 替换完成
cd %root%\target\generated-sources\archetype
echo ==========================安装到本地仓库==========================
call mvn install
echo ==========================安装到私服==========================
call mvn deploy
pause
2、新建 file.ps1 文件,用于处理文件夹名
$file = $args
$content = Get-Content $file -encoding utf8
$content = $content -Replace 'id="maven-', 'id="${rootArtifactId}-'
$content = $content -Replace 'name="maven-', 'name="${rootArtifactId}-'
$content -Replace 'dir="maven-', 'dir="__rootArtifactId__-' | Out-File $args[0] -Encoding utf8
exit
3、新建 pom.ps1 文件,用于处理 pom.xml中的项目引用
$file = $args
$content = Get-Content $file -encoding utf8
$content = $content -Replace '<artifactId>maven-app</artifactId>', '<artifactId>${rootArtifactId}-app</artifactId>'
$content = $content -Replace '<artifactId>maven-domain</artifactId>', '<artifactId>${rootArtifactId}-domain</artifactId>'
$content = $content -Replace '<artifactId>maven-grpc</artifactId>', '<artifactId>${rootArtifactId}-grpc</artifactId>'
$content = $content -Replace '<artifactId>maven-http</artifactId>', '<artifactId>${rootArtifactId}-http</artifactId>'
$content = $content -Replace '<artifactId>maven-service</artifactId>', '<artifactId>${rootArtifactId}-service</artifactId>'
$content = $content -Replace '<artifactId>maven-test</artifactId>', '<artifactId>${rootArtifactId}-test</artifactId>' | Out-File $args[0] -Encoding utf8
exit
最后骨架代码写完,在工程根目录 双击 maven.bat 就可以了。