Remote deployment of Tomcat war file using java code

I am trying to deploy a web service RESTful

to a web server with Tomcat 8

from Eclipse

.

I tried using HttpClient

using the code from the second answer of this post: Tomcat: Remote Program Deployment , but I am getting this exception java.net.SocketException: Connection reset by peer: socket write error.

I also tried HttpURLConnection

using the code from the first answer of this post: how to upload, upload file from tomcat server with username, password in swing but I also get an error.

What could be the reason? Is there another way? Thank.

+3


source to share


2 answers


... he's on a different machine. I can use Tomcat Manager to deploy a web service, but I would like to do it from java code with a http PUT request.

For this to be possible, the deployment folder must be accessible from an HTTP server or web application. This is generally a bad idea for security reasons.

You can do this programmatically using Java (or another language) by invoking any of the many file transfer utilities: ftp, scp, network filesystem, etc.



Note that after you have copied an artifact (like a war file) to your Tomcat host, you can tell Tomcat to deploy it remotely using the deployment manager URL. From the doc:

In this example, a web application located in the / path / to / foo directory on the Tomcat server is deployed as a web application context named / footoo.

http: // localhost: 8080 / manager / text / deploy? path = / footoo & war = file: / path / to / foo

This example deploys the ".war" file /path/to/bar.war to the Tomcat server as a web application context named / bar. Note that there is no path parameter, so the default context path is the same as the file name of the web application archive without the .war extension.

http: // localhost: 8080 / manager / text / deploy? war = jar: file: /path/to/bar.war! /

Your code could copy the artifact via scp (or whatever), and on successful call the manager url with the appropriate arguments. A two-step process in one pass of code.

+2


source


Thank you very much for your response! It works! I used sftp to upload a file to the webapps folder of the Tomcat server. Since in server.xml autodeploy = true I didn't need to make an HTTP PUT request. Here is my code based on this link :



String SFTPHOST = "1.2.3.4";
int SFTPPORT = 22;
String SFTPUSER = "root";
String SFTPPASS = "password";
String SFTPWORKINGDIR = "/home/username/apache-tomcat-8.0.23/webapps/";
Session session = null;
Channel channel = null;
ChannelSftp channelSftp = null;
try {
        JSch jsch = new JSch();
        session = jsch.getSession(SFTPUSER, SFTPHOST, SFTPPORT);
        session.setPassword(SFTPPASS);
        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.connect();
        channel = session.openChannel("sftp");
        channel.connect();
        channelSftp = (ChannelSftp) channel;
        channelSftp.cd("..");
        channelSftp.cd(SFTPWORKINGDIR);
        File f = new File("path/to/war");
        channelSftp.put(new FileInputStream(f), f.getName());
} catch (Exception ex) {
        ex.printStackTrace();

}

      

+1


source







All Articles