1:pom.xml引用文件
<dependencies>
<!-- https://mvnrepository.com/artifact/commons-net/commons-net -->
<!--ftp-->
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.6</version>
</dependency>
<!--sftp-->
<!-- https://mvnrepository.com/artifact/com.jcraft/jsch -->
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.54</version>
</dependency>
<!--
Apache Commons Compress software defines an API for working with compression
and archive formats. These include: bzip2, gzip(即.gz), pack200, lzma, xz, Snappy,
traditional Unix Compress, DEFLATE, DEFLATE64, LZ4, Brotli, Zstandard and ar, cpio,
jar, tar, zip, dump, 7z, arj.
-->
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-compress -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.18</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<!-- spring的核心依赖 -->
<!--1)spring核心依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<!--spring ioc依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<!--spring 扩展依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<!--2)spring dao层依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<!--3)spring web相关依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<!--4) spring test相关依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.8</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.8</version>
</dependency>
</dependencies>
拉取代码方法
public static boolean getCodeByGit(String url,String brancdName,String localPath
,String username,String password){
//1.克隆代码
try {
System.out.println("=============开始下载部署代码=============");
Git.cloneRepository()
.setURI(url).setBranch(brancdName)
.setDirectory(new File(localPath))
.setCredentialsProvider(new UsernamePasswordCredentialsProvider(username,password))
.call();
Thread.sleep(4000);
} catch (GitAPIException e) {
e.printStackTrace();
return false;
}catch (InterruptedException e){
e.printStackTrace();
}
return GitUtils.getFileBoolean(localPath);
}
判断拉取的代码文件内容是否为空
//判断文件夹大小
public static boolean getFileBoolean(String path){
File file = new File(path);
if (file.exists() && !file.isFile() && file.listFiles().length>0)return true;
return false;
}
替换pom问价内容代码
public static boolean modifyFileContent(String fileName,String oldStr,String newStr)throws Exception{
File file = new File(fileName);
if (file.exists()){
logger.info("======开始替换pom文件内容========");
RandomAccessFile raf = null;
raf = new RandomAccessFile(fileName,"rw");
String line = null;
long lastPonit = 0;//记住上一次的偏移量
boolean flag = false;
while ((line = raf.readLine()) != null){
final long point = raf.getFilePointer();
if (line.contains(oldStr)){
String str = line.replace(oldStr,newStr);
raf.seek(lastPonit);
raf.writeBytes(str);
flag = true;
}
lastPonit = point;
}
raf.close();
logger.info("======替换pom文件内容完成========");
return flag;
}else {
System.out.println("未找到文件,请从git上拉取最新的代码项目");
return false;
}
}
压缩代码文件
public static boolean compress(String source , String target,String fileName) throws Exception {
List<File> list = getFiles(source);
if(list.size()<=0){
System.out.println("source file is empty , please check [ "+source+" ]....");
log.info("source file is empty , please check[%s]....",source);
return false;
}
File file = new File(target);
if(!file.exists()){
file.mkdirs();
}
compressTar(list,source,target,fileName);
System.out.println("=============end cpmpressTar==========");
return true;
}
public static List<File> getFiles(String path) {
List<File> list = new LinkedList<File>();
File file = new File(path);
File[] tempList = file.listFiles();
if(null != tempList && tempList.length>0){
for (int i = 0; i < tempList.length; i++) {
if (tempList[i].isFile()) {
list.add(new File(tempList[i].getPath()));
}
if (tempList[i].isDirectory()) {
List tmpList = getFiles(tempList[i].getPath());
if(null !=tmpList && tmpList.size()>0){
list.addAll(tmpList);
}
}
}
}
return list;
}
public static File compressTar(List<File> list,String inPutPath, String outPutPath, String fileName) throws Exception {
System.out.println("=============start cpmpressTar==========");
File outPutFile = new File(outPutPath + File.separator + fileName + ".tar.gz");
File tempTar = new File("temp.tar");
try (FileInputStream fis = new FileInputStream(pack(list,inPutPath, tempTar))) {
try (BufferedInputStream bis = new BufferedInputStream(fis, BUFFER_SIZE)) {
try (FileOutputStream fos = new FileOutputStream(outPutFile)) {
try (GZIPOutputStream gzp = new GZIPOutputStream(fos)) {
int count;
byte[] data = new byte[BUFFER_SIZE];
while ((count = bis.read(data, 0, BUFFER_SIZE)) != -1) {
gzp.write(data, 0, count);
}
}
}
}
}
try {
Files.deleteIfExists(tempTar.toPath());
} catch (IOException e) {
e.printStackTrace();
}
return outPutFile;
}
public static File pack(List<File> files,String inPutPath, File target) throws IOException {
try (FileOutputStream out = new FileOutputStream(target)) {
try (BufferedOutputStream bos = new BufferedOutputStream(out, BUFFER_SIZE)) {
TarArchiveOutputStream os = new TarArchiveOutputStream(bos);
try {
//解决文件名过长问题
os.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
for (File file : files) {
//去掉文件前面的目录
os.putArchiveEntry(new TarArchiveEntry(file, file.getAbsolutePath().replace(inPutPath,"")));
try (FileInputStream fis = new FileInputStream(file)) {
IOUtils.copy(fis, os);
os.closeArchiveEntry();
}
}
}catch (Exception e){
e.printStackTrace();
}finally {
os.close();
}
}
}
return target;
}
链接ftp上传文件
public void upload(String remoteFileName,String locaFileName){
FTPClient ftp=null;
try {
ftp = new FTPClient();
ftp.addProtocolCommandListener( new PrintCommandListener( new PrintWriter( System.out ), true ) );
//连接ftp服务器
connect( ftp );
//设置属性
setProperty( ftp );
//上传文件
upload( ftp, remoteFileName, locaFileName );
//退出
logout( ftp );
} catch (Exception e) {
e.printStackTrace();
}finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException f) {
}
}
}
}
private void connect(FTPClient ftp) throws Exception{
//连接服务器
ftp.connect( "10.252.60.221", 21);
int reply = ftp.getReplyCode();
//是否连接成功
if ( !FTPReply.isPositiveCompletion( reply ) ) {
throw new ConnectException( "10.252.60.221"+" 服务器拒绝连接" );
}
//登陆
if (!ftp.login("prjmt", "prjmt0bsij")) {
throw new ConnectException( "用户名或密码错误" );
}
}
private void upload(FTPClient ftp, String remoteFileName,
String locaFileName) throws Exception{
//上传
InputStream input;
input = new FileInputStream(locaFileName);
ftp.storeFile(remoteFileName, input);
input.close();
}