package com.kk.demo.exe;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.Properties;
import org.junit.Test;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
import ch.ethz.ssh2.StreamGobbler;
/**
* @author kk
*
*/
public class ExecuteCMD {
public static void main(String[] args) {
ExecuteCMD c = new ExecuteCMD();
c.exeMaven();
c.connectLiux();
}
/**
* 执行maven打包命令
* cd到需要打包项目的pom.xml文件下运行:
* mvn clean install -Dmaven.test.skip=true -Dmaven.javadoc.skip=true
* Clean跳过测试直接install
*/
@Test
public void exeMaven(){
String CMD = "cmd /c d: & cd D:\\MyWorkspace\\dlm\\dlm-admin & mvn clean install -Dmaven.test.skip=true -Dmaven.javadoc.skip=true";
try {
Process process = Runtime.getRuntime().exec(CMD);
BufferedReader br = new BufferedReader(
new InputStreamReader(process.getInputStream(),"GBK"));
String line = "";
while((line = br.readLine()) != null){
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 连接linux
* 执行 上传war包 ,部署, 并查看tomcat输出
*/
@Test
public void connectLiux(){
try {
//连接
JSch jsch = new JSch();
Session session = jsch.getSession("dlcxa","120.79.18.21",22);
session.setPassword("qwer1234.");
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.setTimeout(3000 * 60 *6 );
session.connect();
//上传文件
String scr = "C:\\Users\\VULCAN\\Desktop\\00000.txt";
String dst = "/javadata/XAJAVA/tomcat/document/war/00000.txt";
ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
channelSftp.connect();
//上传
channelSftp.put(scr, dst, ChannelSftp.OVERWRITE);
channelSftp.quit();
StringBuilder CMD = new StringBuilder();
//跳转目录
CMD.append("cd /javadata/XAJAVA/tomcat/document/war &&");
//执行shell脚本
//CMD.append(" sh dlm.sh &&");
CMD.append(" cd /javadata/XAJAVA/tomcat/dlm-tomcat/logs &&");
//查看tomcat输出
CMD.append(" tail -f catalina.out");
//执行命令
ChannelExec channelExec = (ChannelExec) session.openChannel("exec");
channelExec.setCommand(CMD.toString());
channelExec.setInputStream(null);
channelExec.setErrStream(System.err);
channelExec.connect();
InputStream in = channelExec.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in,Charset.forName("UTF-8")));
//输出返回
String buf = "";
while((buf = reader.readLine()) != null){
System.out.println(buf);
}
//关闭
reader.close();
channelExec.disconnect();
} catch (JSchException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SftpException e) {
e.printStackTrace();
}
}
}
maven命令行打包并上传linux
猜你喜欢
转载自blog.csdn.net/fx9590/article/details/83986150
今日推荐
周排行