教你bat腳本一鍵配置java開發環境

背景

在新電腦配置或者新人入職時需要對java開發相關環境進行配置安裝,但時常會因為安裝配置不到位或者操作錯誤導致時間的浪費,所以在空餘時間收集瞭一系列軟件的免安裝版本,並且編寫瞭相關配置腳本,讓環境安裝變得標準化。

概覽

安裝包目錄如下如所示

BaseEnv包含jdk(1.8),maven(3.2.5)

ProgramEnv包含idea(2019.2)

SoftEnv包含mysql(5.7),navicat(15),redis,redismanage(1.5.8)

remark為安裝總說明

setting.bat腳本配置環境變量

mysql.bat腳本安裝mysql服務

redis.bat腳本安裝redis服務

icon腳本在桌面創建軟件快捷方式

上述軟件可以基本滿足日常java開發的環境要求

安裝包百度網盤地址:

鏈接: https://pan.baidu.com/s/19WIgCkSgw7Sn71FDkWZRIw 提取碼: kbf6 

環境變量腳本

環境變量腳本可以通過wmic ENVIRONMENT語句進行刪除重新重新創建,Path變量的變更可以通過get

VariableValue|findstr /i語句進行判斷是否存在,bat腳本可以獲取當前路徑動態的去進行配置,免去瞭手動

輸入的繁瑣步驟,腳本如下

@echo off
color 02

:step1
cd /d %~dp0
set currentPath=%PATH%
set currentDir=%cd%
set javaPath=%currentDir%\BaseEnv\jdk
set mavenPath=%currentDir%\BaseEnv\maven
set mysqlPath=%currentDir%\SoftEnv\mysql
set redisPath=%currentDir%\SoftEnv\redis

:step2
wmic ENVIRONMENT where "name='JAVA_HOME'" delete
wmic ENVIRONMENT create name="JAVA_HOME",username="<system>",VariableValue="%javaPath%"
wmic ENVIRONMENT where "name='MAVEN_HOME'" delete
wmic ENVIRONMENT create name="MAVEN_HOME",username="<system>",VariableValue="%mavenPath%"
wmic ENVIRONMENT where "name='CLASSPATH'" delete
wmic ENVIRONMENT create name="CLASSPATH",username="<system>",VariableValue=".;%%JAVA_HOME%%\lib\toos.jar;%%JAVA_HOME%%\lib\dt.jar"
wmic ENVIRONMENT where "name='MYSQL_HOME'" delete
wmic ENVIRONMENT create name="MYSQL_HOME",username="<system>",VariableValue="%mysqlPath%"
wmic ENVIRONMENT where "name='REDIS_HOME'" delete
wmic ENVIRONMENT create name="REDIS_HOME",username="<system>",VariableValue="%redisPath%"

echo.
goto step3

:step3
wmic ENVIRONMENT where "name='Path'" get VariableValue|findstr /i /c:"%%JAVA_HOME%%\bin">nul&&(goto step5)  
echo PATH環境變量中未添加: %%JAVA_HOME%%\bin 
wmic ENVIRONMENT where "name='Path' and username='<system>'" set VariableValue="%currentPATH%;%%JAVA_HOME%%\bin"
set currentPATH=%currentPATH%;%%JAVA_HOME%%\bin
echo.

:step5
echo JAVA_HOME PATH中已添加
wmic ENVIRONMENT where "name='Path'" get VariableValue|findstr /i /c:"%%MAVEN_HOME%%\bin">nul&&(goto step6)  
echo PATH環境變量中未添加: %%MAVEN_HOME%%\bin 
wmic ENVIRONMENT where "name='Path' and username='<system>'" set VariableValue="%currentPATH%;%%MAVEN_HOME%%\bin"
set currentPATH=%currentPATH%;%%MAVEN_HOME%%\bin
echo.

:step6
echo MYSQL_HOME PATH中已添加
wmic ENVIRONMENT where "name='Path'" get VariableValue|findstr /i /c:"%%MYSQL_HOME%%\bin">nul&&(goto step7)  
echo PATH環境變量中未添加: %%MYSQL_HOME%%\bin 
wmic ENVIRONMENT where "name='Path' and username='<system>'" set VariableValue="%currentPATH%;%%MYSQL_HOME%%\bin"
set currentPATH=%currentPATH%;%%MYSQL_HOME%%\bin
echo.

:step7
echo MYSQL PATH中已添加
wmic ENVIRONMENT where "name='Path'" get VariableValue|findstr /i /c:"%%REDIS_HOME%%">nul&&(goto step8)  
echo PATH環境變量中未添加: %%REDIS_HOME%%
wmic ENVIRONMENT where "name='Path' and username='<system>'" set VariableValue="%currentPATH%;%%REDIS_HOME%%"
echo.

:step8
echo REDIS PATH中已添加

pause

服務安裝腳本

redis通過–service-install語句進行windows服務的安裝

@echo off
color 02

"%cd%\SoftEnv\redis\redis-server.exe"  --service-install %cd%\SoftEnv\redis\redis.windows.conf 

net start Redis

pause

mysql通過–initialize-insecure語句進行windows服務的安裝

@echo off
color 02

"%cd%\SoftEnv\mysql\bin\mysqld.exe" --install mysql --defaults-file="%cd%\SoftEnv\mysql\my.ini"
"%cd%\SoftEnv\mysql\bin\mysqld.exe" --initialize-insecure --user=mysql

net start mysql

pause

快捷方式創建腳本

通過WshShell腳本的方式進行圖標快捷方式的創建

@echo off
set currentDir=%cd%

set Program=%cd%\ProgramEnv\idea\bin\idea64.exe
set LnkName=idea
set WorkDir=
set Desc=idea
if not defined WorkDir call:GetWorkDir "%Program%"
(echo Set WshShell=CreateObject("WScript.Shell"^)
echo strDesKtop=WshShell.SpecialFolders("DesKtop"^)
echo Set oShellLink=WshShell.CreateShortcut(strDesKtop^&"\%LnkName%.lnk"^)
echo oShellLink.TargetPath="%Program%"
echo oShellLink.WorkingDirectory="%WorkDir%"
echo oShellLink.WindowStyle=1
echo oShellLink.Description="%Desc%"
echo oShellLink.Save)>makelnk.vbs
echo idea icon link set success!
makelnk.vbs

set Program=%cd%\SoftEnv\navicat\navicat.exe
set LnkName=navicat
set WorkDir=
set Desc=navicat
if not defined WorkDir call:GetWorkDir "%Program%"
(echo Set WshShell=CreateObject("WScript.Shell"^)
echo strDesKtop=WshShell.SpecialFolders("DesKtop"^)
echo Set oShellLink=WshShell.CreateShortcut(strDesKtop^&"\%LnkName%.lnk"^)
echo oShellLink.TargetPath="%Program%"
echo oShellLink.WorkingDirectory="%WorkDir%"
echo oShellLink.WindowStyle=1
echo oShellLink.Description="%Desc%"
echo oShellLink.Save)>makelnk.vbs
echo navicat icon link set success!
makelnk.vbs

set Program=%cd%\SoftEnv\redismanage\rdm.exe
set LnkName=rdm
set WorkDir=
set Desc=rdm
if not defined WorkDir call:GetWorkDir "%Program%"
(echo Set WshShell=CreateObject("WScript.Shell"^)
echo strDesKtop=WshShell.SpecialFolders("DesKtop"^)
echo Set oShellLink=WshShell.CreateShortcut(strDesKtop^&"\%LnkName%.lnk"^)
echo oShellLink.TargetPath="%Program%"
echo oShellLink.WorkingDirectory="%WorkDir%"
echo oShellLink.WindowStyle=1
echo oShellLink.Description="%Desc%"
echo oShellLink.Save)>makelnk.vbs
echo rdm icon link set success!
makelnk.vbs

del /f /q makelnk.vbs
exit
goto :eof
:GetWorkDir
set WorkDir=%~dp1
set WorkDir=%WorkDir:~,-1%
goto :eof

到此這篇關於bat腳本一鍵配置java開發環境的文章就介紹到這瞭,更多相關bat腳本一鍵配置java環境內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: