The title is a little pompous. This article will introduce the use of BAT instructions and tools such as Git to automate the remote branch, while using Gradle to automatically compile packaging to send mail to the designated mailbox process, to automate the compilation and notification function, because then the demand only automatic packaging and notification, So there is no exception handling mechanism, log records are not perfect, but the overall idea is consistent, the rest is the experience of strengthening, interested friends can try their own, you can use the implementation of the function of the bat file to build a more approachable web interface for each project.
First, preface
Some basic things here will not be described, such as git, Gralde, bat instructions, and the process of the various tools involved in the environment, these can be self-Baidu, very much information.
Second, the configuration file for the convenience of use, it is best to provide users to configure specific project specific parameters of the file, after all, not everyone knows bat, the following provides a sample of the GIT resources required for the compilation tool configuration:
Git mailbox |git password |git Address |git Branch | The branch specific commit record (emptying default up-to-date) |git project name
Sample email address configuration After packaging is complete:
Sender mailbox Sender Mailbox Password This is the message header this is the specific message content recipient 1 Mailbox recipient 2 Mailbox
Third, automatically compile the bat file content because Git content is more, so the specific explanation I directly comment in the code, BAT's comments with REM, some of the important commands will be a little comment, if there are no comments and unclear can Baidu understand.
@echo off & setlocal enabledelayedexpansion COLOR 0a:clonebeginrem * initialization parameters. *set gituser= "" Set gitpwd= "" Set giturl= "" s ET gitbranch= "set gitcommit=" "Set Gitprojectname=" "Set clonestr=" "Set/a index=0set clonerootpath=" "Set Clonebranchpath= "" Set Clonecommitpath= "" Set Currentgitname= "" Set/a status=0set/a Mailindex=0rem * Reads the configuration information row by line from the Git-config file and moves the project and its dependencies to local. *for/f "Tokens=1-6 delims==|"%%a in (Git-config.txt) do (REM * initialization parameters. *set " Gituser=%%a "Set" Gitpwd=%%b "Set" giturl=%%c "Set" gitbranch=%%d "Set" gitcommit=%%e "Rem * escape @ character. *set gituser=!gituser:@ =%%40!set Giturl=!giturl:https://=!set Giturl=!giturl:http://=!rem * Generates a git clone directive. *set clonestr=http://!gituser!:[ email protected]!giturl! -B!gitbranch!set "currentgitname=%%f" Echo!clonestr!if!index! EQU 0 (set "gitprojectname=%%f" Echo ***************!currentgitname! start Clone...***************rem * Pull the main project to the current directory under the Item list. *git clone!clonestr!./!gitprojectname!) Else (Echo ***************!currentgitname! start clone...***************cD!gitprojectname!rem * pull away dependent projects into the main project directory under the list of dependent libraries. *git clone!clonestr!./!currentgitname!rem *dp0 represents the current directory of the bat file. *CD%~dp0 REM * Sets the Build.gradle path for the current project and its branch name and commit record path. *if!index! EQU 0 (set "Clonerootpath=%~dp0!gitprojectname!\build.gradle" Set "Clonebranchpath=%~dp0!gitprojectname!\.git\head" Set "clonecommitpath=%~dp0!gitprojectname!\.git\refs\heads\!gitbranch!") Else (set "clonerootpath=%~dp0!gitprojectname!\%%f" Set "Clonebranchpath=%~dp0!gitprojectname!\%%f\.git\head" set " clonecommitpath=%~dp0!gitprojectname!\%%f\.git\refs\heads\!gitbranch! ") REM * Determines whether the current item has been successfully pulled, and whether its branch and current commit record is correct *if exist!clonerootpath! (Echo *******!currentgitname! clone complete...******* find/i "!gitbranch!"!clonebranchpath! && (Echo *******! currentgitname! Clone Branch Correctly...*******if "!gitcommit!" = = "" (Echo *******!currentgitname! Clone latest commit correctly...*******) | | (find/i "!gitcommit!"!clonecommitpath! && (echo *******!currentgitname! clone seleteced Commit correctly...*** ****)|| (If!index! EQU 0 (CD%~dp0!gitprojectname!) Else (CD%~dp0!gitprojectname!\!currentgitname!) rem * Change Branch *git reset--hard!gitcommit!cd%~dp0find/i "!gitcommit!"!clonecommitpath! && (echo *******!currentgitname! clone seleteced commit correctly...*******) | | (Echo *******!currentgitname! clone seleteced commit failed...*******rem rd/s/q%~dp0!gitprojectname!goto cloneError))) )|| (Echo *******!currentgitname! Clone branch Failed...*******rem rd/s/q%~dp0!gitprojectname!goto cloneerror)) Else (Echo *******!gitprojectname! clone failed, please try Again...*******rem rd/s/q%~dp0!gitprojectname!goto cloneer ROR) set/a index+=1) REM * Compile project. *if exist!clonerootpath! (CD!gitprojectname!:buildapkgradle assemblereleaserem *after Call other batch such as Gradle, turn echo off to make comma nd disappear.* @echo off & setlocal enabledelayedexpansion REM * post-compilation of the completed processing bat file. *call build-after.batpause) Else (: Cloneerrorrem * error handling, print the current information to determine whether the configuration error, modify the wrong configuration information, press ENTER to start the compilation again. *echo *******!gitprojectname! Clone failed, there is your configs in!currentgitname! As Follow:*******echo *******gituser=!gituser!*******echo *******gitpwd=!gitpwd!*******echo *******gi Turl=!giturl!*******echo *******gitbranch=!gitbranch!*******echo *******gitcommit=!gitcommit!*******echo Repositoryname=!currentgitname!*******echo *******please Check for errors and correct them, press any key to re Clone after All*******pausegoto:clonebegin) PAUSE
Four, after the compilation of BAT file content This file is automatically called at the end of the previous BAT file, it can be run directly after compiling, determine whether the compilation is successful, if the compilation is successful, call the Blat tool to send the message to the specified recipient.
@echo off & setlocal enabledelayedexpansion set Status=0set senduser= "" Set sendpwd= "" Set mailtitle= "" Set mailcontent = "" set/a mailindex=0cd%~dp0rem * Reads the main project name from the Git-config file. *for/f "Tokens=1-6 delims==|"%%a in (git-config.txt) do (if!stat Us! EQU 0 (Set Status=1rem * Determines whether the master project name was compiled successfully. *if exist%~dp0%%f\app\build\outputs\apk\app-release.apk (echo *******%%f Build Success*******rem * generates the current time. *set Time=%date:~0,4%%date:~5,2%%date:~8,2%%time:~0,2%%time:~3,2%rem * Rename the generated apk file, and move it to the specified directory. *ren%~dp0%%f\app\build\outputs\apk\app-release.apk%%f_1.0.0_!time!. Apkmove%~dp0%%f\app\build\outputs\apk\%%f_1.0.0_!time!. APK%~dp0rd/s/q%~dp0%%frem * Reads the contents of the mail profile line by row, and finally calls Blat to send a message to each recipient. *for/f ""%%a in (mail-config.txt) do (if!mailindex! EQU 0 (set "senduser=%%a") if!mailindex! EQU 1 (set "Sendpwd=%%a") if!mailindex! EQU 2 (set "Mailtitle=%%a") if!mailindex! EQU 3 (set "Mailcontent=%%a") if!mailindex! GTR 3 (blat-body "!mailcontent!"-to%%a-s "!mailtitle!"-u!senduser!-pw!sendpwd!-charset UTF-8) set/aMailindex+=1) Else (echo *******%%f build Failed*******echo *******please Check for errors and correct them, press any KE Y to rebuild after All*******pause)) PAUSE
Five, the end of writing this automated compilation tool is not aware of the bat language, Gradle, Blat These tools are also to do while learning, but the main difficulty is still in the preparation of bat, sometimes a statement will be pondering a half-day, and because the time is limited, writing tools can only be used, To talk about a good experience that must be very far away, interested friends can take to optimize their own optimization and then share it, you can also exchange discussions, if you have to see a friend to be reproduced please indicate this blog source, thank you.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Automated compilation of Android using Bat