通过jar中的pom查看冲突的jar包

来源:互联网 发布:华道数据催收工作好吗 编辑:程序博客网 时间:2024/05/17 23:47

在使用maven进行项目管理时,有时 候引用了第三方的jar文件,这些jar文件里面一般包含一个pom文件,描述的是它自己依赖的jar包,在这个文件中引用的jar文件有可能会跟工程需要的jar产生版本冲突,这个时候就需要知道到底是哪个jar包引用了冲突的包,下面这段代码就是会搜索指定目录下的jar文件,读取其中的pom.xml。

package com.test; import java.io.File;import java.io.FileFilter;import java.io.InputStream;import java.util.ArrayList;import java.util.Enumeration;import java.util.List;import java.util.jar.JarEntry;import java.util.jar.JarFile;import java.util.regex.Pattern; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document;import org.w3c.dom.Node;import org.w3c.dom.NodeList;/** * 查找jar文件中的pom.xml文件,并且根据正则来查找dependencies节点中的 groupId,artifactId * @author Administrator * */public class POMFind {     /**     * 查找dependency节点     * 比较 groupId artifactId version(使用正则判断)     * 有匹配的结果返回true     *      * @param dependency     * @param groupIdReg     * @param artifactIdReg     * @param versionReg     * @return     */    public static boolean findDependency(Node dependency, String groupIdReg,            String artifactIdReg, String versionReg) {        String nodeName = dependency.getNodeName();        boolean b_groupId = false;        boolean b_artifactId = false;        boolean b_version = true;        if (null == groupIdReg && null == artifactIdReg && null == versionReg)            return false;        if (null == nodeName)            return false;        nodeName=nodeName.toLowerCase();        String textContent=String.valueOf(dependency.getTextContent());        if (null != groupIdReg && "groupid".equals(nodeName)) {            b_groupId = Pattern.compile(groupIdReg)                    .matcher(textContent).matches();        }        if (null != artifactIdReg && "artifactid".equals(nodeName)) {            b_artifactId = Pattern.compile(artifactIdReg)                    .matcher(textContent).matches();        }        // version 暂不使用//      if (null != versionReg && "version".equals(nodeName)) {//          b_version = Pattern.compile(versionReg)//                  .matcher(textContent).matches();//      }        // version不作为主要的查询条件,只作为groupId 或artifactId的附加条件        // 只有当groupId 或artifactId两者匹配时,才会对version进行匹配        //  单独只匹配version无意义        return (b_groupId || b_artifactId);    }     /**     * 获取jar文件中的pom文件解析出dependencies节点,然后再去解析dependency节点     * @param in     * @param groupIdReg     * @param artifactIdReg     * @param versionReg     * @return     * @throws Exception     */    public static boolean excutefind(InputStream in, String groupIdReg,            String artifactIdReg, String versionReg) throws Exception {        DocumentBuilderFactory builderFactory = DocumentBuilderFactory                .newInstance();        Document document = builderFactory.newDocumentBuilder().parse(in);        if(null == document.getElementsByTagName("dependencies").item(0))            return false;        //dependencies节点        NodeList nodeList = document.getElementsByTagName("dependencies")                .item(0).getChildNodes();        Node node = null;        for (int i = 0; i < nodeList.getLength(); i++) {            node = nodeList.item(i);            if ("dependency".equals(node.getNodeName()) && node.hasChildNodes()) {                //dependency 节点                NodeList tlist = node.getChildNodes();                for (int m = 0; m < tlist.getLength(); m++) {                    if (findDependency(tlist.item(m), groupIdReg,                            artifactIdReg, versionReg)) {                        return true;                    }                }            }        }        return false;    }    /**     * 找出basePath里面的所有jar文件     * @param basePath  一般为tomcat中工程的lib目录     * @param fileName   在jar文件中需要查找的文件名称 (pom.xml)     * @param groupIdReg     * @param artifactIdReg     * @param versionReg     * @return     */    public static List<File> find(String basePath, String fileName,            String groupIdReg, String artifactIdReg, String versionReg) {        List<File> list = new ArrayList<File>();        File root = new File(basePath);        File[] fileList = root.listFiles(new FileFilter(){            @Override            public boolean accept(File f) {                // TODO Auto-generated method stub                return f.getName().endsWith(".jar");            }         });        for (int i = 0; i < fileList.length; i++) {            try {                @SuppressWarnings("resource")                JarFile jarFile = new JarFile(fileList[i]);                //  jar文件中的所有JarEntry                Enumeration<JarEntry> entries = jarFile.entries();                while (entries.hasMoreElements()) {                    JarEntry jarEntry = (JarEntry) entries.nextElement();                    String name = jarEntry.getName();                    // 获取pom.xml文件                    if (null != name                            && fileName.equals(name.substring(                                    name.lastIndexOf("/") + 1, name.length()))) {                        boolean result=POMFind.excutefind(jarFile.getInputStream(jarEntry),                                groupIdReg, artifactIdReg, versionReg);                        if(result)                            list.add(fileList[i]);                    }                }            } catch (Exception e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }        return list;    }     /**     * @since 2014-3-20     * @param args     */    public static void main(String[] args) {        // TODO Auto-generated method stub        String basePath = "F:\\test\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp0\\wtpwebapps\\webapp\\WEB-INF\\lib\\";        String fileName = "pom.xml";        String artifactIdReg = "^XmlSchema$";        List<File> list = find(basePath, fileName, null, artifactIdReg, null);        for (File f : list) {            System.err.println(f.getName());        }    }}


原创粉丝点击