Tomcat:远程程序化部署?

时间:2011-03-31 10:42:36

标签: tomcat deployment

我想远程部署到Tomcat 以编程方式,我有哪些选择? 我知道/ manager / deploy。 它可能超过JMX吗?即使是没有与Tomcat合作的MBean也没关系。

编辑:似乎部署使用/ manager / deploy不起作用 - 如果我使用包含文件的多部分格式执行POST请求,则servlet返回405方法不允许。此外,servlet的6.0.32代码似乎没有实现远程部署 - 我错了吗?怎么做?

感谢。

3 个答案:

答案 0 :(得分:4)

自从我通过谷歌发现这个,我想分享我的tomcat 7部署和取消部署解决方案

- )因为ondra-zizka指出它就像对正确的URL执行Put请求一样简单 严格的URL已经在tomcat7下更改为/ manager / text / deploy?path =& update =

<上下文-DO-部署-到大于需要以正斜杠开头,例如:/ deployMe

- )您可能需要设置访问管理器应用的权限 将其添加到TOMCAT_HOME / conf / tomcat-users.xml

注意:tomcat doc警告您不要授予同一用户访问多个角色的权限

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

- )用于将Web应用程序部署到apache tomcat的示例代码

package deployment;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

public class DeployManager{

    static CredentialsProvider credsProvider = new BasicCredentialsProvider();;

    public static void main(String args[]) throws ClientProtocolException, IOException{
        /*
         * warning only ever AuthScope.ANY while debugging
         * with these settings the tomcat username and pw are added to EVERY request
         */
        credsProvider.setCredentials(AuthScope.ANY,new UsernamePasswordCredentials("tomcat", "s3cret"));

//      deploy();
//      undeploy();
    }



    private static void deploy() throws ClientProtocolException, IOException {
        String url = "http://localhost:8080/manager/text/deploy?path=/deployMe&update=true";
        File file = new File ("deployMe.war") ;

        HttpPut req = new HttpPut(url) ;
        MultipartEntityBuilder meb = MultipartEntityBuilder.create();
        meb.addTextBody("fileDescription", "war file to deploy");
        //"application/octect-stream"
        meb.addBinaryBody("attachment", file, ContentType.APPLICATION_OCTET_STREAM, file.getName());

        req.setEntity(meb.build()) ;
        String response = executeRequest (req, credsProvider);

        System.out.println("Response : "+response);
    }

    public static void undeploy() throws ClientProtocolException, IOException{
        String url = "http://localhost:8080/manager/text/undeploy?path=/deployMe";
        HttpGet req = new HttpGet(url) ;
        String response = executeRequest (req, credsProvider);
        System.out.println("Response : "+response);
    } 

    private static String executeRequest(HttpRequestBase requestBase, CredentialsProvider credsProvider) throws ClientProtocolException, IOException {
        CloseableHttpClient client = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
        InputStream responseStream = null;
        String res = null;
        HttpResponse response = client.execute(requestBase) ;
        HttpEntity responseEntity = response.getEntity() ;
        responseStream = responseEntity.getContent() ;

        BufferedReader br = new BufferedReader (new InputStreamReader (responseStream)) ;
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line);
            sb.append(System.getProperty("line.separator"));
        }
        br.close() ;
        res = sb.toString();

        return res;
    }
}

- )maven依赖

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.3</version>
</dependency>

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.3</version>
</dependency>

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpmime</artifactId>
    <version>4.3</version>
</dependency>

答案 1 :(得分:3)

我正在关注旧文档。上传是通过PUT

/manager/deploy?path=<context-do-deploy-to>&update=<true|false>请求完成的

还有一个Ant内部使用PUT方法的任务。

Tomcat的JMX MBean不允许远程部署。

答案 2 :(得分:1)

出于某种原因,这是google的最佳结果。假设安装了管理器,maven tomcat6插件非常好。