将非maven项目转换为maven项目,首要第一步就是提取原工程依赖jar里的pom.xml,拼成<dependency>节点

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Scanner;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile; public class PomExtractor {
private StringBuilder stringBuilder = new StringBuilder(10240);
private String groupId;
private String artifactId;
private String version; private PomExtractor() { } public static PomExtractor getInstance() {
return PomExtractorHolder.pomExtractor;
} public static void main(String[] args) {
if (args.length == 0) {
System.out.println("入参是jar所在的文件夹!");
return;
}
File cwd = new File(args[0]);
File[] archives = cwd.listFiles(new jarFilter());
PomExtractor pomExtractor = PomExtractor.getInstance();
for (int j = 0; j < archives.length; j++) {
pomExtractor.commence();
pomExtractor.analyzeJarFile(archives[j]);
}
} public void commence() {
stringBuilder.setLength(0);
} private void resetData() {
groupId = null;
artifactId = null;
version = null;
} public void analyzeJarFile(File file) {
if (!file.exists()) return;
try (ZipFile zipFile = new ZipFile(file)) {
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
if (entry.getName().endsWith("pom.xml")) {
resetData();
boolean hasParent = false;
boolean enterParent = false;
try (Scanner scanner = new Scanner(zipFile.getInputStream(entry), "US-ASCII")) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.contains("parent")) {
enterParent = !enterParent;
hasParent = true;
} else if (line.contains("<groupId>")) {
groupId = line;
} else if (!enterParent && line.contains("<artifactId>")) {
artifactId = line;
} else if (line.contains("<version>")) {
version = line;
} else if (!hasParent && groupId != null && artifactId != null && version != null) {
break;
} else if (line.contains("<dependencies>") || line.contains("<dependency>") || line.contains("<properties>") || line.contains("<profiles>") || line.contains("<plugins>")) {
break;
}
}
if (groupId != null && artifactId != null && version != null) {
System.out.println("<dependency>");
System.out.println(groupId);
System.out.println(artifactId);
System.out.println(version);
System.out.println("</dependency>");
} else {
stringBuilder.append("pom.xml解析异常,当前jar文件是" + file.getCanonicalPath() + ",解析失败的文件是" + entry.getName());
}
}
}
}
} catch (IOException ex) {
System.out.println(ex.toString());
}
return;
} static class jarFilter implements FileFilter {
public boolean accept(File pathName) {
String upcase = pathName.getName().toUpperCase();
return upcase.endsWith(".JAR");
}
} private static class PomExtractorHolder {
private final static PomExtractor pomExtractor = new PomExtractor();
}
}

Classpath resource reader

How do I get just the jar URL from a jar: URL containing a “!” and a specific file in the jar

Get a File or URI object for a file inside an archive with Java

批量从jar包中提取pom.xml的更多相关文章

  1. maven-将依赖的 jar包一起打包到项目 jar 包中

    前言: 有时候在项目开发中,需要很多依赖的 jar 包,其中依赖的 jar 包也会依赖其他的 jar 包,导致jar 包的管理很容易不全,以下有两种方法可以规避这个问题. 一.在pom.xml 文件中 ...

  2. 搜索某个目录下所有jar包中的mapper目录下的xml文件

    rm -rf /mapper/* find /data/app/app-*/lib ! -path "*xnpush*" ! -path "*portal*" ...

  3. Maven中基于POM.xml的Profile来动态切换配置信息

    [转载:https://blog.csdn.net/blueheart20/article/details/52838093] 1. Maven中的profile设置 Maven是目前主流的项目代码结 ...

  4. Jar中的Java程序如何读取Jar包中的资源文件

    Jar中的Java程序如何读取Jar包中的资源文件 比如项目的组织结构如下(以idea中的项目为例): |-ProjectName |-.idea/  //这个目录是idea中项目的属性文件夹 |-s ...

  5. 解决springboot读取jar包中文件的问题

    转载自: https://www.oschina.net/question/2272552_2269641 https://stackoverflow.com/questions/25869428/c ...

  6. 自行实现的jar包中,日志库的适配实现

    ​ 日常情况下,我们自己都会自行实现一些基础的jar包,如dao包.service包或一些其他完成特定功能的jar包.如果没有一套调试日志信息,出现问题时想查找问题非常不方便.可能大多数小伙伴都会有自 ...

  7. maven本地安装jar包同时生成pom文件

    maven 本地安装jar包:mvn install:install-file -Dfile=本地路径/ojdbc12.jar -DgroupId=com.oracle -DartifactId=oj ...

  8. html或者jsp页面引用jar包中的js文件

    一,页面上引用jar包中的js文件的方法 使用java web框架AppFuse的时候发现,jquery.bootstrap等js框架都封装到jar包里面了.这些js文件通过一个wro4j的工具对其进 ...

  9. 如何在大量jar包中搜索特定字符

    欢迎关注我的社交账号: 博客园地址: http://www.cnblogs.com/jiangxinnju/p/4781259.html GitHub地址: https://github.com/ji ...

随机推荐

  1. Ext JS 如何动态加载JavaScript创建窗体

    JavaScript不需要编译即可运行,这让JavaScript构建的应用程序可以变得很灵活.我们可以根据需要动态从服务器加载JavaScript脚本来创建和控制UI来与用户交互.下面结合Ext JS ...

  2. 深度剖析 | 基于大数据架构的BI应用

    说起互联网.电商的数据分析,更多的是谈应用案例,如何去实践数据化管理运营.而这里,我们要从技术角度分享关于数据的技术架构干货,如何应用BI. 原文是云猴网BI总经理王卫东在帆软大数据上的演讲,以下是整 ...

  3. Android 手机卫士--绑定sim卡序列号

    现在开始具体 处理每一个导航页面的逻辑,首先看第二个导航页 本文地址:http://www.cnblogs.com/wuyudong/p/5949775.html,转载请注明出处. 这里需要实现绑定s ...

  4. DXGI 1.5新特性

    为了支持更具弹性和的输出设定和副本机制,DXGI1.5添加了下面这些功能: 高动态范围(HDR)和宽色域(WCG) 可变刷新率显示 复制输出 奉献和再生资源 高动态范围(HDR)和宽色域(WCG) H ...

  5. yii2权限控制rbac之rule详细讲解

    作者:白狼 出处:http://www.manks.top/yii2_rbac_rule.html 本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留 ...

  6. JAVA编程思想(第四版)学习笔记----11.5 List,11.6迭代器

    Collection类的层次结构图(来源与网络)如下所示: 接口:Iterator<T> public interface Iterable<T> Iterable<T& ...

  7. xml类型使用注意事项

    xml 的数据类型在平常的开发也是很常用的,燃鹅.也是有一些地方需要留意.今天我就分享几个测试的例子. 使用 xquery.exist (有但不仅仅限于)的注意事项.通常使用来判断节点是否存在,值是否 ...

  8. Java并发基础框架AbstractQueuedSynchronizer初探(ReentrantLock的实现分析)

    AbstractQueuedSynchronizer是实现Java并发类库的一个基础框架,Java中的各种锁(RenentrantLock, ReentrantReadWriteLock)以及同步工具 ...

  9. 3-EM的安装和使用

    EM的安装和使用 一.EM工具的安装和使用 1.保证数据库启动 2.保证侦听启用 3.通过这个命令可以查看到侦听端口1521的状态信息 4.启动em工具 5.查看em工具是否运行 6.登陆网站并进行操 ...

  10. nginx整理

    一.为什么选择Nginx搭建Web服务器 Apache和Nginx是目前使用最火的两种Web服务器,Apache出现比Nginx早.Apache HTTP Server(简称Apache)是世界使用排 ...