关于bat:bat脚本一键配置java开发环境

39次阅读

共计 4764 个字符,预计需要花费 12 分钟才能阅读完成。

背景

在新电脑配置或者新人入职时须要对 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://yunpan.360.cn/surl_yk…(提取码:8079)

环境变量脚本

环境变量脚本能够通过 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

正文完
 0