Tomcat: remote programmatic deploy?

前端 未结 3 1488
迷失自我
迷失自我 2020-12-17 03:05

I\'d like to deploy to Tomcat programatically remotely, What are my options? I know about /manager/deploy. Is it possible over JMX? Even an MBean not commin

3条回答
  •  无人及你
    2020-12-17 03:56

    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=

    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

    
    
    
    
    

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

    
        org.apache.httpcomponents
        httpclient
        4.3
    
    
    
        org.apache.httpcomponents
        httpclient
        4.3
    
    
    
        org.apache.httpcomponents
        httpmime
        4.3
    
    

提交回复
热议问题