开发者

Tomcat: remote programmatic deploy?

I'd like to deploy to Tomcat programatically remotely, What are my options? 开发者_如何学CI know about /manager/deploy. Is it possible over JMX? Even an MBean not comming with Tomcat is okay.

Edit: It seems that deploying using /manager/deploy doesn't work - if I do POST request with multipart format containing a file, the servlet returns 405 Method not allowed. Also, the 6.0.32 code of the servlet doesn't seem to implement remote deployment - am I wrong? How to do that?

Thanks.


Since I found this via google as well, i want to share my deployment and undeployment solution for tomcat 7

-) as ondra-zizka pointed out it is as easy as doing a Put request to the correct URL tough the URL has changed under tomcat7 to /manager/text/deploy?path=&update=

<context-do-deploy-to> needs to start with a forward slash e.g: /deployMe

-) you might need to set permissions for accessing manager app add this to TOMCAT_HOME/conf/tomcat-users.xml

note: the tomcat doc warns you not to give the same user access to more than one role

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

-) sample code for deploying a web app to an 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 dependencies

<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>


I was following old docs. The upload is done by a PUT request at /manager/deploy?path=<context-do-deploy-to>&update=<true|false>

Also there's an Ant task which uses the PUT methods internally.

Tomcat's JMX MBeans do not allow remote deployment.


This is a top google result for some reason. the maven tomcat6 plugin is excellent, assuming you have the manager installed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜