首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Tomcat:以编程方式远程部署服务

Tomcat:以编程方式远程部署服务
EN

Stack Overflow用户
提问于 2015-11-24 14:48:54
回答 1查看 639关注 0票数 0

我正在尝试创建一个在tomcat服务器上部署WAR文件的服务。该类将input作为WAR文件的路径,并在run中的类包含以下代码后自动将其部署到tomcat服务器。请帮我解决这个问题。

代码语言:javascript
复制
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=C:/Users/Jainesh_Trivedi/Desktop/WAR/AutohostDemo1_1145.war";
    File file = new File("C:\\Users\\Jainesh_Trivedi\\Desktop\\WAR\\AutohostDemo1_1145.war");

    HttpPut req = new HttpPut(url);
    MultipartEntityBuilder meb = MultipartEntityBuilder.create();
    meb.addTextBody("Sample", "C:\\Users\\Jainesh_Trivedi\\Desktop\\WAR\\AutohostDemo1_1145.war");

    // "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;
}
}

错误是:

代码语言:javascript
复制
INFO: I/O exception (java.net.SocketException) caught when processing request: Connection reset by peer: socket write error
Nov 24, 2015 10:39:23 AM org.apache.http.impl.execchain.RetryExec execute
INFO: Retrying request
Exception in thread "main" java.net.SocketException: Connection reset by peer: socket write error
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(Unknown Source)
    at java.net.SocketOutputStream.write(Unknown Source)
    at org.apache.http.impl.io.SessionOutputBufferImpl.streamWrite(SessionOutputBufferImpl.java:117)
    at org.apache.http.impl.io.SessionOutputBufferImpl.flushBuffer(SessionOutputBufferImpl.java:129)
    at org.apache.http.impl.io.SessionOutputBufferImpl.write(SessionOutputBufferImpl.java:158)
    at org.apache.http.impl.io.ContentLengthOutputStream.write(ContentLengthOutputStream.java:114)
    at org.apache.http.entity.mime.content.FileBody.writeTo(FileBody.java:120)
    at org.apache.http.entity.mime.AbstractMultipartForm.doWriteTo(AbstractMultipartForm.java:150)
    at org.apache.http.entity.mime.AbstractMultipartForm.writeTo(AbstractMultipartForm.java:173)
    at org.apache.http.entity.mime.MultipartFormEntity.writeTo(MultipartFormEntity.java:97)
    at org.apache.http.impl.DefaultBHttpClientConnection.sendRequestEntity(DefaultBHttpClientConnection.java:156)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.http.impl.conn.CPoolProxy.invoke(CPoolProxy.java:138)
    at com.sun.proxy.$Proxy0.sendRequestEntity(Unknown Source)
    at org.apache.http.protocol.HttpRequestExecutor.doSendRequest(HttpRequestExecutor.java:237)
    at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:122)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:254)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:177)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:77)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:95)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)
    at com.autohost.java.DeployManager.executeRequest(DeployManager.java:68)
    at com.autohost.java.DeployManager.deploy(DeployManager.java:51)
    at com.autohost.java.DeployManager.main(DeployManager.java:35)
EN

回答 1

Stack Overflow用户

发布于 2015-11-24 15:17:37

你的目标是在tomcat上部署war。我建议使用maven。如果你已经在你的项目中使用了maven,那么就使用tomcat插件。它将为您创建一个war并将其部署到tomcat上。

假设您使用的是Tomcat7。在pom.xml的<build>标记中添加以下内容。

代码语言:javascript
复制
     <build>
        <finalName>AutohostDemo1_1145</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <url>http://localhost:8080/manager/text</url>
                    <server>TomcatServer</server>
                    <path>/AutohostDemo1_1145</path>
                </configuration>
            </plugin>
        </plugins>
    </build>

让你的tomcat保持运行。从存储库中获取所有最新代码。然后从项目的根目录(包含pom.xml文件的目录)运行以下命令。

代码语言:javascript
复制
mvn tomcat7:redeploy

此命令将创建一个war文件并将其部署到tomcat。我们甚至不需要手动重启tomcat。

如果要在远程服务器上执行此操作,则可以在服务器上克隆存储库一次。之后,无论何时需要在tomcat上部署新的war,都可以使用telnet进行连接&从项目的根目录(在远程服务器上)运行相同的命令。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33887140

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档