java 一鍵部署war包,jar包

JavaAutoDeployClient

java一鍵自動部署war包,jar包工具

github

csdn dowload JavaAutoDeployClient-1.1.jar

用法

直接用批處理

  • 創建 config.xml

<?xml version="1.0" encoding="utf-8" ?>
<config>
    <threadPoolSize default="5">3</threadPoolSize><!--線程池大小,如果上傳服務器多的話可以調大,默認5個線程-->
    <servers>
        <server>
            <host>192.168.0.1</host><!--遠程服務器地址-->
            <userName>root</userName><!--ssh登錄名稱-->
            <password>123456</password><!--ssh登錄密碼-->
            <uploads>
                <upload>
                    <local>C:\test.jar</local><!--本地要上傳至服務器的文件,可以是相對地址-->
                    <remote>/home</remote><!--服務器目錄-->
                </upload>
            </uploads>
            <commands>
                <command>/home/restart.sh</command><!--上傳完完文件後要處理的命令,可以多個-->
                <command>/home/restart.sh2</command>
            </commands>

            <!--服務器應用啓動後的驗證接口,用於驗證最新的代碼是否更新成功,這個接口得自己定義,輪詢驗證直到成功-->
            <verify requestCount="51" timeDelay="20000" timeGap="2000"><!--requestCount:接口訪問次數,默認50,timeDelay:
            服務器命令執行完後多長時間開始啓動驗證接口(單位毫秒,默認10000),timeGap: 輪詢時間間隔(單位毫秒,默認1000) -->
                <httpapi method="get" url="http://xxx/app/info"/><!--method: http 請求方法;url:http 接口 url-->
                
                <httpapi method="post" url="http://xxx/test/testPost">
                    <param key="aaa">000</param><!--post 參數 鍵值-->
                    <param key="bbb">111</param>
                </httpapi>
            </verify>

        </server>

        <server>
            <host>192.168.0.2</host>
            <userName>root</userName>
            <password>123456</password>
            <uploads>
                <upload>
                    <local>C:\test.jar</local>
                    <remote>/home</remote>
                </upload>
                <upload>
                    <local>C:\test2.jar</local>
                    <remote>/home</remote>
                </upload>
            </uploads>
            <commands>
                <command>/home/restart.sh</command>
                <command>/home/restart2.sh</command>
            </commands>

            <verify requestCount="51" timeDelay="20000" timeGap="2000">
                <httpapi method="get" url="http://xxx/app/info"/>

            </verify>

        </server>

    </servers>

</config>

  • 一鍵調用命令

java -jar JavaAutoDeployClient-1.1.jar config.xml


  • 上傳war包config的例子
<?xml version="1.0" encoding="utf-8" ?>
<!--use ssh user password-->
<config>
    <threadPoolSize default="5">3</threadPoolSize>
    <servers>
        <server>
            <host>192.168.0.1</host>
            <userName>root</userName>
            <password>123456</password>
            <uploads>
                <upload>
                    <local>C:\javawebdeploy.war</local>
                    <remote>/coder/tomcat/apache-tomcat-7.0.55/webapps</remote>
                </upload>
            </uploads>
            <commands>
                    <command>sh /coder/tomcat/apache-tomcat-7.0.55/bin/shutdown.sh</command>
					<command>rm -rf /coder/tomcat/apache-tomcat-7.0.55/webapps/javawebdeploy</command>
					<command>sh /coder/tomcat/apache-tomcat-7.0.55/bin/startup.sh</command>
            </commands>
             <verify requestCount="51" timeDelay="20000" timeGap="2000">
               <httpapi method="get" url="http://xxx/app/info"/>
            </verify>
        </server>
    </servers>
</config>



  • 用maven打包的命令例子

autodeploy.bat

call maven-package.bat
pause
java -jar JavaAutoDeployClient-1.1.jar config.xml

maven-package.bat

mvn clean package -Pprod

用代碼自定義 加入lib JavaAutoDeployClient-1.1.jar

  • 代碼例子
    public static void main(String args[]){


        Map<String,String> uploadMap = new HashMap<String,String>();
        uploadMap.put("c:\\test.jar","/home");

        List<String> commands = new ArrayList<String>();
        commands.add("/home/restart.sh");

        List<HttpMethod> apis =new ArrayList<HttpMethod>();
        HttpGet httpGet = new HttpGet();
        httpGet.setUrl("http://xxxx/app/info");
        apis.add(httpGet);

        HttpPost httpPost = new HttpPost();
        httpPost.setUrl("http://xxxx/app/info");
        Map<String,String> params = new HashMap<String,String>();
        params.put("key","value");
        httpPost.setParams(params);

        apis.add(httpPost);

        AutoDeploy autoDeploy = AutoDeplyBuilder.create().
                setServerInfo("192.168.0.1","root","123456").
                setUploadFileInfo(uploadMap).
                setCommands(commands).
                setVerifyApi(apis).
                build();

        try {
            autoDeploy.start(new AutoDeploy.AutoDeployListener() {
                @Override
                public void finish() {

                }

                @Override
                public void verifySucess(List<String> log) {

                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章