使用DeployTask类远程部署war文件的Java代码

时间:2017-07-09 20:53:14

标签: java tomcat deployment war

我编写了下面的代码,通过使用已经由Tomcat实现的类DeployTask在tomcat服务器上远程部署war文件。

import java.io.File;
import org.apache.catalina.ant.DeployTask;

public class TestClass {
    public static void main(String[] args) {
        String filePath = "resources/StudentWS.war";
        TestClass test=new TestClass();
        ClassLoader classLoader = test.getClass().getClassLoader();
        File warFile = new       
        File(classLoader.getResource(filePath).getFile());

        String webPath="StudentWS";
        String url = "http://localhost:8080/manager/text";

        DeployTask deployTask=new DeployTask();
        deployTask.setUrl(url);
        deployTask.setUsername("deployer");
        deployTask.setPassword("admin");       
        deployTask.setWar(warFile.getAbsolutePath());
        deployTask.setPath(webPath);
        deployTask.execute();
    }
}

这是tomcat上用户部署者的配置:

 < role rolename="manager-script"/>

 < role rolename="manager-jmx"/>

 < user username="deployer" password="admin" roles="manager-script,manager-jmx"/>

但是我收到了以下错误:

Exception in thread "main" java.io.IOException: Error writing request body to server
at org.apache.catalina.ant.AbstractCatalinaTask.execute(AbstractCatalinaTask.java:270)
at org.apache.catalina.ant.DeployTask.execute(DeployTask.java:178)
at TestClass.main(TestClass.java:20)
Caused by: java.io.IOException: Error writing request body to server
at sun.net.www.protocol.http.HttpURLConnection$StreamingOutputStream.checkError(HttpURLConnection.java:3518)
at sun.net.www.protocol.http.HttpURLConnection$StreamingOutputStream.write(HttpURLConnection.java:3501)
at java.io.BufferedOutputStream.write(BufferedOutputStream.java:122)
at org.apache.catalina.ant.AbstractCatalinaTask.execute(AbstractCatalinaTask.java:219)
... 2 more
Suppressed: java.io.IOException: insufficient data written
    at sun.net.www.protocol.http.HttpURLConnection$StreamingOutputStream.close(HttpURLConnection.java:3540)
    at java.io.FilterOutputStream.close(FilterOutputStream.java:159)
    at org.apache.catalina.ant.AbstractCatalinaTask.execute(AbstractCatalinaTask.java:222)
    ... 2 more

我想知道我错过了什么,或者是否有一个java代码通过使用下面的url远程部署war文件而不使用maven:

http://localhost:8080/manager/text/deploy?path=/StudentWS&war=file:Path_To_War_File

1 个答案:

答案 0 :(得分:1)

我只是遇到了同样的问题。问题似乎是我们需要使用“ localwar”属性。我查看了源代码,并以不同的方式对待war属性,并且无法使用该属性部署本地文件。 将您的代码更改为:

GenMat<-function(SN){
  mat<-matrix(G1,nrow=5,ncol=5,byrow=T)
  dimnames(mat)=list(nameslist)
}

更新

我刚刚意识到,这仅在要部署到的服务器是本地服务器时才有效。如果是远程服务器,我认为它不会起作用。部署任务本身一定有问题,导致它失败。