导出依赖
之前我们知道 dependency:copy-dependencies
可以将依赖拷贝到指定目录,但是不含目录结构,仅是jar列表
现在的需求是导出一个tree 依赖
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.nio.charset.Charset; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.regex.Pattern; public class Main { public static void main(String[] args) { File[] files = new File(".").listFiles(f -> { if (f.isDirectory() || f.isHidden()) { return false; } String name = f.getName(); if (name.endsWith(".txt")) { return true; } return false; }); if(files.length == 0) { return; } Map<String,List<String>> result = new LinkedHashMap<String,List<String>>(); for (File file : files) { String content = read(file); one(result,content); } } public static void one(Map<String, List<String>> result, String s) { String[] lines = s.split("\n|\r\n"); String in = ""; String out = ""; for (String line : lines) { if(line.contains("--in")) { in = line.replace("--in", "").trim(); }else if (line.contains("--out")) { out = line.replace("--out", "").trim(); }else { String injar; String inpom; if(line.endsWith(";")) { line = line.substring(0,line.length()-1); } injar = line; inpom = line.replace(".jar", ".pom"); String jar = join(out,injar.replace(in, "")); copyFile(injar, jar); String pom = join(out,inpom.replace(in, "")); copyFile(inpom, pom); } } } /** * 读取内容<文本形式可以看懂> * * @param fromWhichFile 从哪个文件读 * @param fileCharset 该文件的编码 * @return */ public static String read(File fromWhichFile) { StringBuffer sb = new StringBuffer(); BufferedReader bufferedReader = null; try { FileInputStream fis = new FileInputStream(fromWhichFile); InputStreamReader isr = new InputStreamReader(fis, Charset.forName("utf-8")); bufferedReader = new BufferedReader(isr); char[] buf = new char[2048]; int size; while ((size = bufferedReader.read(buf)) != -1) { sb.append(buf, 0, size); } } catch (Exception e) { throw new RuntimeException("读取文件失败", e); } finally { try { if (bufferedReader != null) { bufferedReader.close(); } } catch (IOException e) { } } return sb.toString(); } /** * 返回linux风格的路径(适用windows) * @param split 文件或目录,或者组合 * @return */ public static String join(String... split) { if (split == null) { return null; } StringBuffer sb = new StringBuffer(); // 不支持. .. // // \ \\ 全部替换为/ // 所有元素开头的/需要移除,除第一个元素 // 所有元素要以/结尾,除最后一个元素 Pattern backSlash = Pattern.compile("\\\\"); Pattern multiSlash = Pattern.compile("/[/]*/"); for (int i = 0; i < split.length; i++) { if (split[i] == null) { continue; } String replaced = backSlash.matcher(split[i]).replaceAll("/"); replaced = multiSlash.matcher(replaced).replaceAll("/"); if(split.length == 0) { if(replaced.endsWith("/")) { replaced = replaced.substring(0, replaced.length() - 1); } }else if(i == 0){ if(!replaced.endsWith("/")) { replaced = replaced + "/"; } }else if(i == split.length - 1){ if(replaced.startsWith("/")) { replaced = replaced.substring(1); } if(replaced.endsWith("/")) { replaced = replaced.substring(0, replaced.length() - 1); } }else { if(replaced.startsWith("/")) { replaced = replaced.substring(1); } if(!replaced.endsWith("/")) { replaced = replaced + "/"; } } sb.append(replaced); } return sb.toString(); } public static void copyFile(String in, String out) { try { File infile = new File(in); File outfile = new File(out); File outparent = outfile.getParentFile(); if(!outparent.exists()) { outparent.mkdirs(); } InputStream is = new FileInputStream(infile); OutputStream os = new FileOutputStream(outfile); copyStream(is,os); is.close(); os.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void copyStream(InputStream in, OutputStream out) throws IOException { final int MAX = 4096; byte[] buf = new byte[MAX]; for (int bytesRead = in.read(buf, 0, MAX); bytesRead != -1; bytesRead = in.read(buf, 0, MAX)) { out.write(buf, 0, bytesRead); } } }
上面是工具
配置文件如下:
--in D:\mvnrepository\
--out D:\data\
D:\mvnrepository\commons-beanutils\commons-beanutils\1.8.0\commons-beanutils-1.8.0.jar;
D:\mvnrepository\commons-logging\commons-logging\1.1.1\commons-logging-1.1.1.jar;
--in 是本地maven仓库地址
--out 是导出目录
完整jar列表如何获取呢?
mvn dependency:build-classpath
可以得到
[INFO] Dependencies classpath: D:\mvnrepository\org\springframework\boot\spring-boot-starter-web\2.2.2.RELEASE\spring-boot-starter-web-2.2.2.RELEASE.jar;D:\mvnrepository\org\springframework\boot\spring-boot-starter\2.2.2.RELEAS E\spring-boot-starter-2.2.2.RELEASE.jar;D:\mvnrepository\org\springframework\boot\spring-boot\2.2.2.RELEASE\spring-boot-2.2.2.RELEASE.jar;D:\mvnrepository\org\springframework\boot\spring-boot-auto configure\2.2.2.RELEASE\spring-boot-autoconfigure-2.2.2.RELEASE.jar;D:\mvnrepository\org\springframework\boot\spring-boot-starter-logging\2.2.2.RELEASE\spring-boot-starter-logging-2.2.2.RELEASE.ja r;D:\mvnrepository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;D:\mvnrepository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;D:\mvnrepository\org\slf4j\slf4j-api\1.7 .29\slf4j-api-1.7.29.jar;D:\mvnrepository\org\apache\logging\log4j\log4j-to-slf4j\2.12.1\log4j-to-slf4j-2.12.1.jar;D:\mvnrepository\org\apache\logging\log4j\log4j-api\2.12.1\log4j-api-2.12.1.jar;D :\mvnrepository\org\slf4j\jul-to-slf4j\1.7.29\jul-to-slf4j-1.7.29.jar;D:\mvnrepository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;D:\mvnrepository\org\springf ramework\spring-core\5.2.2.RELEASE\spring-core-5.2.2.RELEASE.jar;D:\mvnrepository\org\springframework\spring-jcl\5.2.2.RELEASE\spring-jcl-5.2.2.RELEASE.jar;D:\mvnrepository\org\yaml\snakeyaml\1.25 \snakeyaml-1.25.jar;D:\mvnrepository\org\springframework\boot\spring-boot-starter-json\2.2.2.RELEASE\spring-boot-starter-json-2.2.2.RELEASE.jar;D:\mvnrepository\com\fasterxml\jackson\core\jackson- databind\2.10.1\jackson-databind-2.10.1.jar;D:\mvnrepository\com\fasterxml\jackson\core\jackson-annotations\2.10.1\jackson-annotations-2.10.1.jar;D:\mvnrepository\com\fasterxml\jackson\core\jackso n-core\2.10.1\jackson-core-2.10.1.jar;D:\mvnrepository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.10.1\jackson-datatype-jdk8-2.10.1.jar;D:\mvnrepository\com\fasterxml\jackson\datatype\ jackson-datatype-jsr310\2.10.1\jackson-datatype-jsr310-2.10.1.jar;D:\mvnrepository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.10.1\jackson-module-parameter-names-2.10.1.jar;D:\m vnrepository\org\springframework\boot\spring-boot-starter-tomcat\2.2.2.RELEASE\spring-boot-starter-tomcat-2.2.2.RELEASE.jar;D:\mvnrepository\org\apache\tomcat\embed\tomcat-embed-core\9.0.29\tomcat -embed-core-9.0.29.jar;D:\mvnrepository\org\apache\tomcat\embed\tomcat-embed-el\9.0.29\tomcat-embed-el-9.0.29.jar;D:\mvnrepository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.29\tomcat-embe d-websocket-9.0.29.jar;D:\mvnrepository\org\springframework\boot\spring-boot-starter-validation\2.2.2.RELEASE\spring-boot-starter-validation-2.2.2.RELEASE.jar;D:\mvnrepository\jakarta\validation\j akarta.validation-api\2.0.1\jakarta.validation-api-2.0.1.jar;D:\mvnrepository\org\hibernate\validator\hibernate-validator\6.0.18.Final\hibernate-validator-6.0.18.Final.jar;D:\mvnrepository\org\jbo ss\logging\jboss-logging\3.4.1.Final\jboss-logging-3.4.1.Final.jar;D:\mvnrepository\com\fasterxml\classmate\1.5.1\classmate-1.5.1.jar;D:\mvnrepository\org\springframework\spring-web\5.2.2.RELEASE\ spring-web-5.2.2.RELEASE.jar;D:\mvnrepository\org\springframework\spring-beans\5.2.2.RELEASE\spring-beans-5.2.2.RELEASE.jar;D:\mvnrepository\org\springframework\spring-webmvc\5.2.2.RELEASE\spring- webmvc-5.2.2.RELEASE.jar;D:\mvnrepository\org\springframework\spring-aop\5.2.2.RELEASE\spring-aop-5.2.2.RELEASE.jar;D:\mvnrepository\org\springframework\spring-context\5.2.2.RELEASE\spring-context -5.2.2.RELEASE.jar;D:\mvnrepository\org\springframework\spring-expression\5.2.2.RELEASE\spring-expression-5.2.2.RELEASE.jar [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------
经过简单的替换,将"\r\n" 替换为"",再将";"替换成";\r\n"
D:\mvnrepository\org\springframework\boot\spring-boot-starter-web\2.2.2.RELEASE\spring-boot-starter-web-2.2.2.RELEASE.jar; D:\mvnrepository\org\springframework\boot\spring-boot-starter\2.2.2.RELEASE\spring-boot-starter-2.2.2.RELEASE.jar; D:\mvnrepository\org\springframework\boot\spring-boot\2.2.2.RELEASE\spring-boot-2.2.2.RELEASE.jar; D:\mvnrepository\org\springframework\boot\spring-boot-autoconfigure\2.2.2.RELEASE\spring-boot-autoconfigure-2.2.2.RELEASE.jar; D:\mvnrepository\org\springframework\boot\spring-boot-starter-logging\2.2.2.RELEASE\spring-boot-starter-logging-2.2.2.RELEASE.jar; D:\mvnrepository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar; D:\mvnrepository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar; D:\mvnrepository\org\slf4j\slf4j-api\1.7.29\slf4j-api-1.7.29.jar; D:\mvnrepository\org\apache\logging\log4j\log4j-to-slf4j\2.12.1\log4j-to-slf4j-2.12.1.jar; D:\mvnrepository\org\apache\logging\log4j\log4j-api\2.12.1\log4j-api-2.12.1.jar; D:\mvnrepository\org\slf4j\jul-to-slf4j\1.7.29\jul-to-slf4j-1.7.29.jar; D:\mvnrepository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar; D:\mvnrepository\org\springframework\spring-core\5.2.2.RELEASE\spring-core-5.2.2.RELEASE.jar; D:\mvnrepository\org\springframework\spring-jcl\5.2.2.RELEASE\spring-jcl-5.2.2.RELEASE.jar; D:\mvnrepository\org\yaml\snakeyaml\1.25\snakeyaml-1.25.jar; D:\mvnrepository\org\springframework\boot\spring-boot-starter-json\2.2.2.RELEASE\spring-boot-starter-json-2.2.2.RELEASE.jar; D:\mvnrepository\com\fasterxml\jackson\core\jackson-databind\2.10.1\jackson-databind-2.10.1.jar; D:\mvnrepository\com\fasterxml\jackson\core\jackson-annotations\2.10.1\jackson-annotations-2.10.1.jar; D:\mvnrepository\com\fasterxml\jackson\core\jackson-core\2.10.1\jackson-core-2.10.1.jar; D:\mvnrepository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.10.1\jackson-datatype-jdk8-2.10.1.jar; D:\mvnrepository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.10.1\jackson-datatype-jsr310-2.10.1.jar; D:\mvnrepository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.10.1\jackson-module-parameter-names-2.10.1.jar; D:\mvnrepository\org\springframework\boot\spring-boot-starter-tomcat\2.2.2.RELEASE\spring-boot-starter-tomcat-2.2.2.RELEASE.jar; D:\mvnrepository\org\apache\tomcat\embed\tomcat-embed-core\9.0.29\tomcat-embed-core-9.0.29.jar; D:\mvnrepository\org\apache\tomcat\embed\tomcat-embed-el\9.0.29\tomcat-embed-el-9.0.29.jar; D:\mvnrepository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.29\tomcat-embed-websocket-9.0.29.jar; D:\mvnrepository\org\springframework\boot\spring-boot-starter-validation\2.2.2.RELEASE\spring-boot-starter-validation-2.2.2.RELEASE.jar; D:\mvnrepository\jakarta\validation\jakarta.validation-api\2.0.1\jakarta.validation-api-2.0.1.jar; D:\mvnrepository\org\hibernate\validator\hibernate-validator\6.0.18.Final\hibernate-validator-6.0.18.Final.jar; D:\mvnrepository\org\jboss\logging\jboss-logging\3.4.1.Final\jboss-logging-3.4.1.Final.jar; D:\mvnrepository\com\fasterxml\classmate\1.5.1\classmate-1.5.1.jar; D:\mvnrepository\org\springframework\spring-web\5.2.2.RELEASE\spring-web-5.2.2.RELEASE.jar; D:\mvnrepository\org\springframework\spring-beans\5.2.2.RELEASE\spring-beans-5.2.2.RELEASE.jar; D:\mvnrepository\org\springframework\spring-webmvc\5.2.2.RELEASE\spring-webmvc-5.2.2.RELEASE.jar; D:\mvnrepository\org\springframework\spring-aop\5.2.2.RELEASE\spring-aop-5.2.2.RELEASE.jar; D:\mvnrepository\org\springframework\spring-context\5.2.2.RELEASE\spring-context-5.2.2.RELEASE.jar; D:\mvnrepository\org\springframework\spring-expression\5.2.2.RELEASE\spring-expression-5.2.2.RELEASE.jar
再筛选目标依赖,执行main就可以了(工具只拷贝jar和pom)
├─commons-beanutils │ └─commons-beanutils │ └─1.8.0 │ commons-beanutils-1.8.0.jar │ commons-beanutils-1.8.0.pom │ └─commons-logging └─commons-logging └─1.1.1 commons-logging-1.1.1.jar commons-logging-1.1.1.pom