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>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…