将非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. Maven自定义绑定插件目标:创建项目的源码jar

    <build> <plugins> <!-- 自定义绑定,创建项目的源码jar --> <plugin> <groupId>org.apac ...

  2. json简单使用

    web工程中如何将大量数据从服务器端传送到浏览器一直是很重要的一个问题. 其中一个解决方法是在服务器端将将数据封装成json格式,然后传给前台.废话不多说,下面讲干货. 1.要用json必须下载一个库 ...

  3. 如何利用FineReport制作动态树报表

    在对数据字段进行分类管理时,利用动态树折叠数据是一个很好的方法,也就是点击数据前面的加号才展开对应下面的数据,如下图.那这样的效果在制作报表时该如何实现呢? 下面以报表工具FineReport为例介绍 ...

  4. canvas 制作flappy bird(像素小鸟)全流程

    flappy bird制作全流程: 一.前言 像素小鸟这个简单的游戏于2014年在网络上爆红,游戏上线一段时间内appleStore上的下载量一度达到5000万次,风靡一时, 近年来移动web的普及为 ...

  5. xamarin 一般错误解决办法

    1. android_m2repository_r错误 问题描述: Unzipping failed. Please download https://dl-ssl.google.com/androi ...

  6. Mac OS X中bogon的处理

    起因 最近写一个项目,之前在eclipse里的tomcat启动是没问题的,最近不知怎么了,启动的时候ehcache会报一个错误,说java.net.UnknownHostException: bogo ...

  7. 简易版自定义BaseServlet

    这几天在学Java Web,一直在思考Servlet重用的问题,就用java的反射机制实现自定义的简易版BaseServlet; 该方式有点像struts2 利用映射获取前端的参数.有兴趣的同学可以自 ...

  8. mongodb 3.x 之实用新功能窥看[2] ——使用$lookup做多表关联处理

    这篇我们来看mongodb另一个非常有意思的东西,那就是$lookup,我们知道mongodb是一个文档型的数据库,而且它也是最像关系型数据库的 一种nosql,但是呢,既然mongodb是无模式的, ...

  9. 异步HTTPHandler的实现

    使用APM的方式实现 using System; using System.Web; using System.Threading; class HelloWorldAsyncHandler : IH ...

  10. UVA10325 The Lottery(容斥原理)

    题意: 给n,m,和m个数(k1~km).求1~n中有多少个数不是(k1~km)中任意一数的倍数. 题解: 容斥模板题.反面考虑,a的倍数有n/a个:既是a,也是b的倍数,即lcm(a,b)的倍数有n ...