1.本地gradle依赖包编译成maven格式包
package com.dsq.utils;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.regex.Matcher;
/**
* 拷贝本地android .aar 依赖包到内网搭建环境使用
*/
public class CopyTool {
static Stringpath ="D:\\maven";
static StringstopName ="maven";
public static void main(String[] args) {
System.out.println("Begin to copy");
File folder =new File(path);
copyFileDo(folder);
modifyFileDo(folder);
modifyLastFileDo(folder);
delFileAndDir(folder);
System.out.println("Copy finished");
}
/**
* 递归方式删除指定文件夹内全部文件和文件夹
*
* @param file
*/
private static void copyFileDo(File file) {
//判断指定路径的文件或文件是否存在
if (file.exists() && file.isDirectory()) {
//返回当前路径下的所有文件和文件夹名称
File[] files = file.listFiles();
//遍历所有文件及文件夹
for (File f : files) {
String s = f.getPath().replaceAll("\\.", Matcher.quoteReplacement(File.separator));
File file1 =new File(s);
if (!file1.exists()) {
file1.mkdirs();
}
}
}
}
/**
* 递归方式删除指定文件夹内全部文件和文件夹
*
* @param file
*/
private static void modifyFileDo(File file) {
//判断指定路径的文件或文件是否存在
if (file.exists() && file.isDirectory()) {
//返回当前路径下的所有文件和文件夹名称
File[] files = file.listFiles();
//遍历所有文件及文件夹
for (File f : files) {
File file1 =new File(f.getPath().replaceAll("\\.", "#"));
f.renameTo(file1); //改名
}
}
}
/**
* 递归方式删除指定文件夹内全部文件和文件夹
*
* @param file
*/
private static void modifyLastFileDo(File file) {
//判断指定路径的文件或文件是否存在
if (file.exists() && file.isDirectory()) {
//返回当前路径下的所有文件和文件夹名称
File[] files = file.listFiles();
//遍历所有文件及文件夹
for (File f : files) {
if (f.isDirectory()) {
String s = f.getPath().replaceAll("#", Matcher.quoteReplacement(File.separator));
File file1 =new File(s);
if (!file1.exists()) {
file1.mkdirs();
}
modifyLastFileDo(f);
}else {
String[] split = f.getPath().split(Matcher.quoteReplacement(File.separator));
StringBuffer sb =new StringBuffer();
for (int i =0; i < split.length; i++) {
if (i != split.length -2) {
sb.append(split[i]);
}
if (i != split.length -1) {
sb.append(Matcher.quoteReplacement(File.separator));
}
}
try {
String s = sb.toString().replaceAll("#", Matcher.quoteReplacement(File.separator));
copyFile(f.getPath(), sb.toString().replaceAll("#", Matcher.quoteReplacement(File.separator)));
}catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
/**
* 递归方式删除指定文件夹内全部文件和文件夹
*
* @param file
*/
private static void copyFileAndDir(File file) {
//判断指定路径的文件或文件是否存在
if (file.exists() && file.isDirectory()) {
//返回当前路径下的所有文件和文件夹名称
File[] files = file.listFiles();
//遍历所有文件及文件夹
for (File f : files) {
if (f.isDirectory()) {
copyFileAndDir(f);
}else {
try {
copyDir(f.getPath(), f.getPath().replaceAll(".", "\\"));
}catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
/**
* 递归方式删除指定文件夹内全部文件和文件夹
*
* @param file
*/
private static void delFileAndDir(File file) {
//判断指定路径的文件或文件是否存在
if (file.exists() && file.isDirectory()) {
//返回当前路径下的所有文件和文件夹名称
File[] files = file.listFiles();
//遍历所有文件及文件夹
for (File f : files) {
if (f.isDirectory() && (f.getName().length() >=40 || f.getPath().contains("#"))) {
// delFileAndDir1(f);
deleteFile(f);
}else {
delFileAndDir(f);
}
// //如果是文件就删除
// if (f.isFile()) {
// //删除文件
// f.delete();
// } else {
// //如果是目录,则调用递归方法
// delFileAndDir(f);
// }
// //删除空目录
// f.delete();
}
}
}
/**
* 先根遍历序递归删除文件夹
*
* @param dirFile 要被删除的文件或者目录
* @return 删除成功返回true, 否则返回false
*/
public static boolean deleteFile(File dirFile) {
// 如果dir对应的文件不存在,则退出
if (!dirFile.exists()) {
return false;
}
if (dirFile.isFile()) {
return dirFile.delete();
}else {
for (File file : dirFile.listFiles()) {
deleteFile(file);
}
}
return dirFile.delete();
}
/**
* 递归方式删除指定文件夹内全部文件和文件夹
*
* @param file
*/
private static void delFileAndDir1(File file) {
//判断指定路径的文件或文件是否存在
if (file.exists() && file.isDirectory()) {
//返回当前路径下的所有文件和文件夹名称
File[] files = file.listFiles();
//遍历所有文件及文件夹
for (File f : files) {
//如果是文件就删除
if (f.isFile()) {
//删除文件
f.delete();
}else {
//如果是目录,则调用递归方法
delFileAndDir1(f);
}
//删除空目录
f.delete();
}
}
}
/**
* 处理文件夹中带点好的。;例如:D:/test/com.ifind.android/
*/
public static void processDoForld() {
File file =new File(path);
if (file.exists()) {
LinkedList list =new LinkedList<>();
File[] files = file.listFiles();
for (int i =0; i < files.length; i++) {
File file2 = files[i];
if (file2.isDirectory()) {
//文件夹
File desFile =creatforld(file2);
}else {
//文件//目前不存在
}
}
}
}
/**
* 处理文件夹中带点好的。;例如:D:/test/com.ifind.android/
*/
public static void processDotForld() {
File file =new File(path);
if (file.exists()) {
LinkedList list =new LinkedList<>();
File[] files = file.listFiles();
for (int i =0; i < files.length; i++) {
File file2 = files[i];
if (file2.isDirectory()) {
//文件夹
File desFile =creatforld(file2);
copyFileToDes(file2, desFile);
}else {
//文件//目前不存在
}
}
}
}
/**
* 文件夹拷贝
*
* @param source
* @param des
*/
public static void copyFileToDes(File source, File des) {
try {
copyDir(source.getPath(), des.getPath());
}catch (Exception e) {
// TODO: handle exception
}
}
/**
* 文件夹拷贝
*
* @param sourcePath
* @param newPath
* @throws IOException
*/
public static void copyDir(String sourcePath, String newPath)throws IOException {
File file =new File(sourcePath);
String[] filePath = file.list();
if (!(new File(newPath)).exists()) {
(new File(newPath)).mkdirs();
}
for (int i =0; i < filePath.length; i++) {
if ((new File(sourcePath + file.separator + filePath[i])).isDirectory()) {
copyDir(sourcePath + file.separator + filePath[i], newPath + file.separator + filePath[i]);
}
if (new File(sourcePath + file.separator + filePath[i]).isFile()) {
copyFile(sourcePath + file.separator + filePath[i], newPath + file.separator + filePath[i]);
}
}
}
public static void copyFile(String oldPath, String newPath)throws IOException {
File oldFile =new File(oldPath);
File file =new File(newPath);
FileInputStream in =new FileInputStream(oldFile);
FileOutputStream out =new FileOutputStream(file);
byte[] buffer =new byte[2097152];
//while((in.read(buffer)) != -1){
// out.write(buffer);
//}
DataInputStream dis =new DataInputStream(new BufferedInputStream(in));
DataOutputStream dos =new DataOutputStream(new BufferedOutputStream(out));
int length;
while ((length = dis.read(buffer)) != -1) {
dos.write(buffer, 0, length);
}
dos.flush();
dos.close();
dis.close();
// boolean delete = oldFile.delete();
// if (delete) {
// Log.d("aa", "删除成功");
// }
}
/**
* 创建文件夹
*
* @param file
*/
public static Filecreatforld(File file) {
String path = file.getAbsolutePath();
// String path = path2;
if (path !=null) {
String temp ="files-2.1";
temp =stopName;
String temS[] = path.split(temp);
if (temS !=null && temS.length ==2) {
String firstName = temS[0];
String dotName = temS[1];
if (dotName !=null) {
String[] lasts = dotName.split("\\.");
int count = lasts.length;
if (count <2) {
return null;
}
String pathNew = firstName + temp;
for (int i =0; i < count; i++) {
if (i ==0) {
pathNew = pathNew + lasts[i];
}else {
pathNew = pathNew +"\\" + lasts[i];
}
}
if (pathNew !=null && !pathNew.equals("")) {
File fileForld =new File(pathNew);
if (!fileForld.exists()) {
fileForld.mkdirs();
}
return fileForld;
}
}
}
}
return null;
}
public static ArrayListgetFile(File file) {
ArrayList list =new ArrayList<>();
if (file.isDirectory()) {
File[] filesTemp = file.listFiles();
for (int i =0; i < filesTemp.length; i++) {
ArrayList result =getFile(filesTemp[i]);
list.addAll(result);
}
}else {
list.add(file);
}
return list;
}
// 创建目录
public static boolean createDir(String destDirName) {
File dir =new File(destDirName);
if (dir.exists()) {// 判断目录是否存在
System.out.println("创建目录失败,目标目录已存在!");
return false;
}
if (!destDirName.endsWith(File.separator)) {// 结尾是否以"/"结束
destDirName = destDirName + File.separator;
}
if (dir.mkdirs()) {// 创建目标目录
System.out.println("创建目录成功!" + destDirName);
return true;
}else {
System.out.println("创建目录失败!");
return false;
}
}
public static void copyToLastForld() {
File file =new File(path);
if (file.exists()) {
LinkedList list =new LinkedList<>();
File[] files = file.listFiles();
for (int i =0; i < files.length; i++) {
File file2 = files[i];
if (file2.isDirectory()) {
//文件夹
proceessForld(file2);
}else {
//文件//目前不存在
}
}
}
}
private static void proceessForld(File file) {
File[] files = file.listFiles();
for (int i =0; i < files.length; i++) {
File file2 = files[i];
if (file2.isDirectory()) {
//文件夹
proceessForld(file2);
}else {
//文件//目前不存在//判断是否进行拷贝
try {
proceessFile(file2);
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
private static void proceessFile(File file)throws FileNotFoundException {
if (file !=null) {
String path = file.getAbsolutePath();
if (path !=null) {
String[] lasts =splitString(path);
if (lasts !=null && lasts.length >0) {
int count = lasts.length;
String last = lasts[count -1];
String last2 = lasts[count -2];
if (last2 !=null && last2.length() >20) {
//拷贝到上一级目录
String des =null;
if (count <2) {
return;
}
for (int i =0; i < count -2; i++) {
if (i ==0) {
des = lasts[i];
}else {
des = des +"\\\\" + lasts[i];
}
}
des = des +"\\\\" + last;
String strParentDirectory = file.getParent();
File parentFile =new File(strParentDirectory);
strParentDirectory = parentFile.getParent() +"\\" + last;
copy(file, path, strParentDirectory);
}else {
// System.out.println("source = "+path);
}
// System.out.println("source = "+path);
// System.out.println("des = "+des);
}
}
}
}
private static String[]splitString(String path) {
String[] lasts =null;
lasts = path.split("\\\\");
int count = lasts.length;
boolean isFirst =true;
for (int i =0; i < count; i++) {
String str = lasts[i];
if (str !=null && str.contains(".")) {
if (isFirst) {
isFirst =false;
System.out.println("\n\n\n\n");
System.out.println("path=" + path +"");
}
System.out.println("str=" + str +"");
}
}
return lasts;
}
/**
* copy动作
*
* @param file
* @param source
* @param des
* @throws FileNotFoundException
*/
private static void copy(File file, String source, String des)throws FileNotFoundException {
if (file !=null) {
FileInputStream fis =null;
FileOutputStream fot =null;
byte[] bytes =new byte[1024];
int temp =0;
File desFile =new File(des);
if (desFile.exists()) {
return;
}
try {
fis =new FileInputStream(file);
fot =new FileOutputStream(desFile);
while ((temp = fis.read(bytes)) != -1) {
fot.write(bytes, 0, temp);
fot.flush();
}
}catch (IOException e) {
e.printStackTrace();
}finally {
if (fis !=null) {
try {
fis.close();
}catch (IOException e) {
e.printStackTrace();
}
}
if (fot !=null) {
try {
fot.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
private static StringgetContent(String content) {
String str = content;
if (content !=null && content.length() >4) {
str = content.substring(0, 4);
}
return str;
}
}
2.将maven底下的包拷贝到内网环境,根目录创建mavenimpor.sh文件,拷贝下面代码进去
#!/bin/bash# copy and run this script to the root of the repository directory containing files# this script attempts to exclude uploading itself explicitly so the script name is important# Get command line paramswhile getopts ":r:u:p:" opt; do case $opt in r) REPO_URL="$OPTARG" ;; u) USERNAME="$OPTARG" ;; p) PASSWORD="$OPTARG" ;; esacdone find . -type f -not -path './mavenimport\.sh*' -not -path '*/\.*' -not -path '*/\^archetype\-catalog\.xml*' -not -path '*/\^maven\-metadata\-local*\.xml' -not -path '*/\^maven\-metadata\-deployment*\.xml' | sed "s|^\./||" | xargs -I '{}' curl -u "$USERNAME:$PASSWORD" -X PUT -v -T {} ${REPO_URL}/{} ;
3.当前目录打开cmd -u maven 用户名 -p maven密码 -r 指定要上传的maven仓库
输入 mavenimport.sh -u root -p 123456 -r http://127.0.0.1:8081/nexus/content/repositories/thirdparty/