我想以编程方式远程部署到Tomcat ,我的选择是什么?我知道/ manager/deploy.它可能超过JMX吗?即使是没有与Tomcat合作的MBean也没关系.
编辑:似乎部署使用/ manager/deploy不起作用 - 如果我使用包含文件的多部分格式执行POST请求,则servlet返回405方法不允许.此外,servlet的6.0.32代码似乎没有实现远程部署 - 我错了吗?怎么做?
谢谢.
由于我也通过谷歌找到了这个,我想分享我的 tomcat 7 部署和取消部署解决方案
-) 正如ondra-zizka指出的那样,它就像对正确的 URL 执行 Put 请求一样简单,但是在 tomcat7 下 URL 已更改为 /manager/text/deploy?path=&update=
<context-do-deploy-to> 需要以正斜杠开头,例如:/deployMe
-) 您可能需要设置访问管理器应用程序的权限,将此添加到 TOMCAT_HOME/conf/tomcat-users.xml
注意:tomcat 文档警告您不要授予同一用户访问多个角色的权限
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="manager-jmx"/>
<user username="tomcat" password="s3cret" roles="manager-gui,manager-script,manager-jmx"/>
Run Code Online (Sandbox Code Playgroud)
-) 将 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;
}
}
Run Code Online (Sandbox Code Playgroud)
-) 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>
Run Code Online (Sandbox Code Playgroud)
我正在关注旧文档。上传是通过PUT以下请求完成的/manager/deploy?path=<context-do-deploy-to>&update=<true|false>
还有一个 Ant 任务在内部使用 PUT 方法。
Tomcat 的 JMX MBean 不允许远程部署。
| 归档时间: |
|
| 查看次数: |
4326 次 |
| 最近记录: |