将非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. Android 手机卫士10--应用管理器

    1.添加不同类型条目 class MyAdapter extends BaseAdapter{ //获取数据适配器中条目类型的总数,修改成两种(纯文本,图片+文字) @Override public ...

  2. 五个典型的 JavaScript 面试题

    阅读原文 在IT界,需要大量的 JavaScript 开发者.如果你的能力能够胜任这一角色,那么你将获得许多更换工作和提高薪水的机会.但是在你被公司录取之前,你需要展现你的技术实力,以便通过面试环节. ...

  3. [deviceone开发]-Star分享的几个示例

    一.简介 这个是star早期分享的几个示例,都非常实用,包括弹出的菜单,模拟支付密码输入等.初学者推荐.也可以直接使用.二.效果图 三.相关下载 https://github.com/do-proje ...

  4. 安装cocoapods以及更新cocoapods

    安装 1.设置ruby的软件源 这是因为ruby的软件源rubygems.org因为使用亚马逊的云服务,被我天朝屏蔽了,需要更新一下ruby的源,过程如下: gem sources -l #(查看当前 ...

  5. Android 多个listview的实现

    正好,今天项目中需要,先写了个demo,给大家参考参考. 先上图,需要的自己,看看具体的代码实现步骤 大概说一下实现步骤: 1.布局中先用 scrollview 包裹 LinearLayout < ...

  6. 让你少走弯路的搭建树莓派的Net与NodeJS运行环境

      树莓派是当前最火的嵌入计算平台没有之一,树莓派可以给我们无数的想象,树莓派的高性能.低功耗.低成本.可扩展性(最新的树莓派原生支持WIFI和蓝牙,这功能太赞了)深受大家的喜爱.虽然树莓派到目前为止 ...

  7. Linux系统下面挂载u盘

    1.先插好u盘到Linux服务器,然后查看u盘挂载到哪个目录下面. [root@localhost ~]# /sbin/fdisk -l 2.挂载到u目录下面 [root@localhost ~]# ...

  8. EL表达式杂项

    1.<%@ page isELIgnored="false" %> 是否忽略EL表达式,如果值为ture,那么  ${..}这样的会直接原样输出,不会进行EL表达式计算 ...

  9. Heartbeat使用梳理

    在日常的集群系统架构中,一般用到Heartbeat的主要就2种:1)高可用(High Availability)HA集群, 使用Heartbeat实现,也称为"双机热备", &qu ...

  10. Mac OSX+VirtualBox+Vagrant+CentOS初体验

    1.安装VirtualBox VirtualBox下载地址 免费小巧非常适用,根据自己机器系统选择下载包 VirtualBox 安装包 2.安装并使用Vagrant 1)Vagrant下载地址 选择下 ...