通过程序调用tomcat的manager的text模式的命令来自动部署项目

来源:互联网 发布:淘宝产品摄影价格表 编辑:程序博客网 时间:2024/05/20 20:46

现在我需要做一个tomcat 的自动部署的功能 就是在不重启tomcat的情况下 可以发布 重启、升级应用

1、tomcat的manager的text的模式是可以实现这一功能的 

public final static String WEB_LIST="http://localhost:8080/manager/text/list";

public final static String STOP_URL="http://localhost:8080/manager/stop?path=/"; //停止

public final static String START_URL="http://localhost:8080/manager/start?path=/";//启动

public final static String DEPLOY_URL="http://localhost:8080/manager/text/deploy?path=/"; //部署

public final static String UNDEPLOY_URL="http://localhost:8080/manager/text/undeploy?path=/";//卸载

2、由于manager的text命令访问是需要用户名密码才能访问的,所以必须放开访问权限

<role rolename="manager-gui"/>

<role rolename="manager-script"/>

<role rolename="manager-jmx"/>

<user username="tomcat" password="tomcat" roles="manager-gui,manager-script,manager-jmx"/>

3、程序访问通过HttpURLConnection(我试过Httpclient 但是好像没办法设置用户名密码 )去访问,代码如下

 
URL url = null;         BufferedReader breader = null;         InputStream is = null;         StringBuffer resultBuffer = new StringBuffer();         try {// String UNDEPLOY_URL="http://localhost:8080/manager/text/undeploy?path=/"  appName就是你需要卸载的应用             url = new URL(TomcatCommon.UNDEPLOY_URL+appName);             String userPassword = TomcatCommon.UESERNAME + ":" + TomcatCommon.PASSWORD;//此处为用户名密码             String encoding = new sun.misc.BASE64Encoder().encode (userPassword.getBytes());//在classpath中添加rt.jar包,在%java_home%/jre/lib/rt.jar             HttpURLConnection conn = (HttpURLConnection) url.openConnection();             conn.setRequestProperty("Authorization", "Basic " + encoding);            int statusCode= conn.getResponseCode();             is = conn.getInputStream();             breader = new BufferedReader(new InputStreamReader(is));             String line = "";             while ((line = breader.readLine()) != null) {                 resultBuffer.append(line);             }           //http请求失败    if (statusCode != HttpStatus.SC_OK) {     LOGGER.info("step-2:卸载"+appName+" ERROR!");     throw new RuntimeException("step-2:卸载"+appName+" ERROR!");    }else{     String responseBody = new String(resultBuffer.toString());     //说明不成功     if(!responseBody.contains("OK")){      LOGGER.info("step-2:卸载"+appName+" ERROR!");      throw new RuntimeException("step-2:卸载"+appName+" ERROR!");     }    }    LOGGER.info("step-2:卸载"+appName+"成功!");            } catch (MalformedURLException e) {             e.printStackTrace();             LOGGER.error("step-2:卸载"+appName+" ERROR !"+e.getMessage());         } finally {             if(breader != null)                 breader.close();             if(is != null)                 is.close();         }
4、其他命令调用方式都可以参照次方式调用
5、附上我自己写的代码
http://note.youdao.com/share/?id=6a29d17eabdc0ad64309745c3d300a0f&type=notebook

0 0
原创粉丝点击