maven项目手动打包jar上传到中央仓库bat脚本,maven手动打包上传中央仓库bat脚本

一、问题描述

我这里有个项目,模块特多,而且拆散在各个工程里,在IDEA里打包,需要每个工程每个工程打开然后打包或者在cmd下,麻烦不说,还费时间,这种方式,打包所有的项目,到上传到中央仓库,起码半个小时,由于我这有些模块需要单独手动进行打包,不能直接就上传,还需要处理下,每次发布时特麻烦,所有就有了想写个工具来处理下,刚开始想整个maven插件,这种方式也麻烦,每个项目里还有在引入一下,索性就直接用bat批处理算了,简单还不需要什么环境,到后面发现bat真的是难用。网上的资源也不多,尤其是变量,分什么延迟变量,真的是醉了,正则提取这一块findstr也不支持,后面用grep.exe实现了,所以就多了很多一些重复性代码,将第一个版本分享出来,这个版本比较基础,可以自行扩展,最新版本功能有点多,不好分享,功能都差不多。

二、解决方法

完整代码如下:

@echo off
chcp 65001
cls
title          sonatype手动处理程序
echo           欢迎使用sonatype处理程序
echo ------------------请选择----------------
echo ------------------1.清除文件----------
echo ------------------2.复制jar----------
echo ------------------3.修改pom----------
echo ------------------4.打包文件----------
echo ------------------5.上传上传----------
echo ------------------6.打包上传----------
echo ------------------0.退出----------------
set /p a=请输入你的选择(0-6):
echo 你输入的数据是:%a%
setlocal enabledelayedexpansion
set filename=""
set targetJar=""
set currentPath=%~dp0
if %a%==0 (
 goto et
)
if %a%==1 (
 goto clean_file
)
if %a%==2 (
 goto copy_jar
)
if %a%==3 (
 goto edit_pom
)
if %a%==4 (
 goto build_file
)
if %a%==5 (
 goto upload_file
)

if %a%==6 (
 goto build_upload
)



:copy_jar
    ::set targetPath=%currentPath%Plugins
    ::set targetPath=..\target\
	set "regexp_jar=-[0-9].[0-9].[0-9].jar"
	::删除当前目录下的旧jar
	del *.asc*
	del *-javadoc.jar*
	del *-sources.jar*
	for /R %targetPath% %%f in (*.jar) do ( 
		set "line=%%f"
		setlocal enabledelayedexpansion	
		echo "%%f"|findstr /r /C:"%regexp_jar%" >nul 2>&1
		if ERRORLEVEL 1 (
			rem echo "%%f"
		) else (
			del /q "%%f"
			echo 删除旧版本%%f
		)
		endlocal
	)
	for %%I in ("%CD%\..") do set targetPath=%%~fI
	set targetPath=%targetPath%\target\
	echo %targetPath%
	for /R %targetPath% %%f in (*.jar) do ( 
		set "line=%%f"
		::setlocal enabledelayedexpansion	
		echo "%%f"|findstr /r /C:"%regexp_jar%" >nul 2>&1
		if ERRORLEVEL 1 (
			rem echo "%%f"
		) else (
			set targetJar=%%f
		)
		endlocal
	)
	echo 复制新版本%targetJar%
	copy /y %targetJar% %currentPath%
	pause
	goto end
:edit_pom
    set "regexp_jar=-[0-9].[0-9].[0-9].jar"
	set "regexp_version=[0-9].[0-9].[0-9]"
	for %%I in ("%CD%\..") do set targetPath=%%~fI
	set targetPath=%targetPath%\target\
	echo %targetPath%
	for /R %targetPath% %%f in (*.jar) do ( 
		set "line=%%f"
		echo "%%f"|findstr /r /C:"%regexp_jar%" >nul 2>&1
		if ERRORLEVEL 1 (
			rem echo "%%f"
		) else (
			set targetJar=%%f
		)
	)
	for /R %currentPath% %%f in (*.jar) do ( 
		set "line=%%f"
		echo "%%f"|findstr /r /C:"%regexp_jar%" >nul 2>&1
		if ERRORLEVEL 1 (
			rem echo "%%f"
		) else (
			set currentJar=%%f
		)
	)	
	if not exist grep.exe (
        rem 下载grep到当前路径
		mshta http://bathome.net/s/hta/index.html Tools.get('grep')
    )
	echo %targetJar%|grep -o -w "%regexp_version%" >temp.txt
	set /p newJarVersion= < temp.txt
	echo 最新jar版本 %newJarVersion%
	del temp.txt
	
	echo %currentJar%|grep -o -w "%regexp_version%" >temp.txt
	set /p oldJarVersion= < temp.txt
	echo 当前jar版本 %oldJarVersion%
	del temp.txt
	
	(for /f "delims=" %%i in ('dir /b /s /a-d *.pom') do (
	    set pomname=%%~ni
		set pomfile=%%~ni%%~xi
	))
	echo 替换pom文件%pomfile%版本号
	for %%i in (test.txt) do set file=%%~fi
	for /f "delims=" %%i in ('type %pomfile%') do (
		set str=%%i
		set "str=!str:%oldJarVersion%=%newJarVersion%!"
		echo !str!>>$
	)
	set "stre=%pomfile%"
	set "stre=!stre:%oldJarVersion%=%newJarVersion%!"
	echo 替换文件%stre%
	move %pomfile% %pomfile%.bak
	move $ %stre%
	pause
:clean_file
	del *.asc*
	del *-javadoc.jar*
	del *-sources.jar*
	goto end
:build_file
    echo 开始打包jar
	(for /f "delims=" %%i in ('dir /b /s /a-d *.jar') do (
		set filename=%%~ni
	))
	echo %filename%
	echo README文件判断
	if not exist README (
        rem 创建README
        echo=>README
		echo README 文件不存在,已创建该文件!
    ) else (
        echo README 文件已存在,无需创建!
    )
	jar -cvf %filename%-javadoc.jar README
	jar -cvf %filename%-sources.jar README
	gpg -ab %filename%-javadoc.jar
	gpg -ab %filename%-sources.jar
	gpg -ab %filename%.pom
	gpg -ab %filename%.jar
	::jar -cvf bundle.jar %filename%*
	pause
	goto end
:upload_file
    echo 开始上传jar包
	(for /f "delims=" %%i in ('dir /b /s /a-d *.jar') do (
		set filename=%%~ni
	))	
	echo 上传%filename%中
	call mvn org.apache.maven.plugins:maven-gpg-plugin:1.5:sign-and-deploy-file -Durl=https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/ -DrepositoryId=ossrh -DpomFile=%filename%.pom -Dfile=%filename%.jar
	call mvn org.apache.maven.plugins:maven-gpg-plugin:1.5:sign-and-deploy-file -Durl=https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/ -DrepositoryId=ossrh -DpomFile=%filename%.pom -Dfile=%filename%-sources.jar -Dclassifier=sources
	call mvn org.apache.maven.plugins:maven-gpg-plugin:1.5:sign-and-deploy-file -Durl=https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/ -DrepositoryId=ossrh -DpomFile=%filename%.pom -Dfile=%filename%-javadoc.jar -Dclassifier=javadoc	
	pause
	goto end
:et
 exit
:build_upload
	(for /f "delims=" %%i in ('dir /b /s /a-d *.jar') do (
		set filename=%%~ni
	))
	echo %filename%
	echo README文件判断
	if not exist README (
        rem 创建README
        echo=>README
		echo README 文件不存在,已创建该文件!
    ) else (
        echo README 文件已存在,无需创建!
    )
	jar -cvf %filename%-javadoc.jar README
	jar -cvf %filename%-sources.jar README
	gpg -ab %filename%-javadoc.jar
	gpg -ab %filename%-sources.jar
	gpg -ab %filename%.pom
	gpg -ab %filename%.jar
	::jar -cvf bundle.jar %filename%*
	call mvn org.apache.maven.plugins:maven-gpg-plugin:1.5:sign-and-deploy-file -Durl=https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/ -DrepositoryId=ossrh -DpomFile=%filename%.pom -Dfile=%filename%.jar
	call mvn org.apache.maven.plugins:maven-gpg-plugin:1.5:sign-and-deploy-file -Durl=https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/ -DrepositoryId=ossrh -DpomFile=%filename%.pom -Dfile=%filename%-sources.jar -Dclassifier=sources
	call mvn org.apache.maven.plugins:maven-gpg-plugin:1.5:sign-and-deploy-file -Durl=https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/ -DrepositoryId=ossrh -DpomFile=%filename%.pom -Dfile=%filename%-javadoc.jar -Dclassifier=javadoc	
	pause
	goto end
:end 
    call "sonatype.bat"

你可能感兴趣的:(其他,jar,maven,java)