批量从jar包中提取pom.xml
将非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的更多相关文章
- maven-将依赖的 jar包一起打包到项目 jar 包中
前言: 有时候在项目开发中,需要很多依赖的 jar 包,其中依赖的 jar 包也会依赖其他的 jar 包,导致jar 包的管理很容易不全,以下有两种方法可以规避这个问题. 一.在pom.xml 文件中 ...
- 搜索某个目录下所有jar包中的mapper目录下的xml文件
rm -rf /mapper/* find /data/app/app-*/lib ! -path "*xnpush*" ! -path "*portal*" ...
- Maven中基于POM.xml的Profile来动态切换配置信息
[转载:https://blog.csdn.net/blueheart20/article/details/52838093] 1. Maven中的profile设置 Maven是目前主流的项目代码结 ...
- Jar中的Java程序如何读取Jar包中的资源文件
Jar中的Java程序如何读取Jar包中的资源文件 比如项目的组织结构如下(以idea中的项目为例): |-ProjectName |-.idea/ //这个目录是idea中项目的属性文件夹 |-s ...
- 解决springboot读取jar包中文件的问题
转载自: https://www.oschina.net/question/2272552_2269641 https://stackoverflow.com/questions/25869428/c ...
- 自行实现的jar包中,日志库的适配实现
日常情况下,我们自己都会自行实现一些基础的jar包,如dao包.service包或一些其他完成特定功能的jar包.如果没有一套调试日志信息,出现问题时想查找问题非常不方便.可能大多数小伙伴都会有自 ...
- maven本地安装jar包同时生成pom文件
maven 本地安装jar包:mvn install:install-file -Dfile=本地路径/ojdbc12.jar -DgroupId=com.oracle -DartifactId=oj ...
- html或者jsp页面引用jar包中的js文件
一,页面上引用jar包中的js文件的方法 使用java web框架AppFuse的时候发现,jquery.bootstrap等js框架都封装到jar包里面了.这些js文件通过一个wro4j的工具对其进 ...
- 如何在大量jar包中搜索特定字符
欢迎关注我的社交账号: 博客园地址: http://www.cnblogs.com/jiangxinnju/p/4781259.html GitHub地址: https://github.com/ji ...
随机推荐
- spider RPC管理接口
为了在独立管理模式下尽可能的容易运行时排查问题,spider中间件提供了一系列restful api用于动态管理当前节点的路由,下游节点等.目前支持的RESTFUL API如下所示: 功能 服务号 R ...
- 详细介绍Mysql各种存储引擎的特性以及如何选择存储引擎
最近业务上有要求,要实现类似oracle 的dblink linux版本 Server version: 5.6.28-0ubuntu0.14.04.1 (Ubuntu) 修改配置文件 /etc/ ...
- Atitit 游戏的原理与概论attilax总结
Atitit 游戏的原理与概论attilax总结 1. 游戏历史2 1.1.1. 盘点PC游戏史上最重要的50款游戏2 1.1.2. 回味人类文明进程 五款经典的历史游戏2 2. 游戏类型(主要分为6 ...
- json简单使用
web工程中如何将大量数据从服务器端传送到浏览器一直是很重要的一个问题. 其中一个解决方法是在服务器端将将数据封装成json格式,然后传给前台.废话不多说,下面讲干货. 1.要用json必须下载一个库 ...
- iOS - 静态库的创建与使用
在日常项目开发中,不论是为了两个公司项目上的业务交流还是为了减少项目的编译时间,有的时候我们会把项目中的私密内容打包成静态库,或者是把项目中变动较少一部分打包成静态库以便提高编译效率,那么下面我们就来 ...
- 如何禁止内部viewPager滑动
众所周知,viewPager是能够滑动的,但有时候我们需要禁止它的滑动(微笑地面对*----*). 情况是这样的: activity中有一个viewPager,viewPager中加入3个Fragme ...
- 教你怎么半天搞定Docker
首先,不要把docker想的那么高大,它不就是先做个镜像,然后通过docker像虚拟机一样跑起来嘛...docker其实在真实业务场景中还是非常有局限性的.Dockerfile脚本也没那么好写,有些应 ...
- mysql数据类型
一.数值类型 Mysql支持所有标准SQL中的数值类型,其中包括严格数据类型(INTEGER,SMALLINT,DECIMAL,NUMBERIC),以及近似数值数据类型(FLOAT,REAL,DOUB ...
- 利用SQL Server 2008 R2创建自动备份计划
本文主要利用SQL Server 2008 R2自带的"维护计划"创建一个自动备份数据的任务. 首先,启动 Sql Management studio,确保"SQL Se ...
- SCNU ACM 2016新生赛初赛 解题报告
新生初赛题目.解题思路.参考代码一览 1001. 无聊的日常 Problem Description 两位小朋友小A和小B无聊时玩了个游戏,在限定时间内说出一排数字,那边说出的数大就赢,你的工作是帮他 ...