如何通过java程序找到最新的jar版罐子?

时间:2012-12-04 12:21:28

标签: java

在我的项目中有40到50个jar文件可用,每次都需要很多时间才能找到每个jar的最新版本。你有没有人帮我为此编写一个java程序

2 个答案:

答案 0 :(得分:3)

您可能只想使用maven:http://maven.apache.org/ 或者是其他依赖项管理器,比如Ivy。

答案 1 :(得分:1)

At the time of ant-build please call this method
public void ExpungeDuplicates(String filePath) {
    Map<String,Integer> replaceJarsMap = null;
    File folder = null;
    File[] listOfFiles = null;
    List<String> jarList = new ArrayList<String>();
    String files = "";
    File deleteFile = null;
    Iterator<String> mapItr = null;
    //String extension ="jar";
    try {
            folder = new File(filePath);
        listOfFiles = folder.listFiles();
        for (int i = 0; i < listOfFiles.length; i++) {
            if (listOfFiles[i].isFile()) {
                files = listOfFiles[i].getName();
                jarList.add(files);
            }
        }
        if (jarList.size() > 0) {
            replaceJarsMap = PatternClassifier.findDuplicatesOrLowerVersion(jarList);  
            System.err.println("Duplicate / Lower Version - Total Count : "+replaceJarsMap.size());
            mapItr = replaceJarsMap.keySet().iterator();
            while (mapItr.hasNext()) {
                String key = mapItr.next();
                int repeat = replaceJarsMap.get(key);
                System.out.println( key +" : "+repeat);
                for (int i = 0; i <repeat; i++) {
                    deleteFile = new File(filePath + System.getProperty ("file.separator")+key);
                    try{
                    if (deleteFile != null && deleteFile.exists()){
                        if(deleteFile.delete()){
                            System.err.println(key +" deleted");
                        }
                    }
                    }catch (Exception e) {
                    }
                }
            }
        }

} catch (Exception e) {
    // TODO: handle exception
}
}

您只需要将Lib的路径提供给此函数。此方法将找到文件的所有重复版本或较低版本。   下面给出了关键的功能......从你提供的文件列表中找出重复的内容。

public static Map<String,Integer> findDuplicatesOrLowerVersion(List<String> fileNameList) {
    List<String> oldJarList = new ArrayList<String>();
    String cmprTemp[] = null;
    boolean match = false;
    String regex = "",regexFileType = "",verInfo1 = "",verInfo2 = "",compareName = "",tempCompareName = "",tempJarName ="";
    Map<String,Integer> duplicateEntryMap = new HashMap<String, Integer>();
    int count = 0;
    Collections.sort(fileNameList, Collections.reverseOrder());
    try{
        int size = fileNameList.size();
        for(int i = 0;i<size;i++){
            cmprTemp = fileNameList.get(i).split("[0-9\\._]*");
            for(String s : cmprTemp){
                compareName += s;
            }
            regex = "^"+compareName+"[ajr0-9_\\-\\.]*";
            regexFileType = "[0-9a-zA-Z\\-\\._]*\\.jar$"; 
            if( fileNameList.get(i).matches(regexFileType) && !oldJarList.contains(fileNameList.get(i))){
                for(int j = i+1 ;j<size;j++){
                    cmprTemp = fileNameList.get(j).split("[0-9\\._]*");
                    for(String s : cmprTemp){
                        tempCompareName += s;
                    }
                    match = (fileNameList.get(j).matches(regexFileType) && tempCompareName.matches(regex));
                    if(match){
                            cmprTemp = fileNameList.get(i).split("[a-zA-Z\\-\\._]*");
                            for(String s : cmprTemp){
                                verInfo1 += s;
                            }
                            verInfo1 += "000";
                            cmprTemp = fileNameList.get(j).split("[a-zA-Z\\-\\._]*");
                            for(String s : cmprTemp){
                                verInfo2 += s;
                            }
                            verInfo2 += "000";
                            int length = 0;
                            if(verInfo1.length()>verInfo2.length()){
                                length = verInfo2.length();                         
                        }else{
                            length = verInfo1.length();
                        }
                        if(Long.parseLong(verInfo1.substring(0,length))>=Long.parseLong(verInfo2.substring(0,length))){
                            count = 0;
                            if(!oldJarList.contains(fileNameList.get(j))){
                                oldJarList.add(fileNameList.get(j));
                                duplicateEntryMap.put(fileNameList.get(j),++count);
                            }else{
                                count = duplicateEntryMap.get(fileNameList.get(j));
                                duplicateEntryMap.put(fileNameList.get(j),++count);
                            }
                        }else{
                            tempJarName = fileNameList.get(i);
                        }
                        match = false;verInfo1 = "";verInfo2 = "";
                }
                    tempCompareName = "";
                }
                if(tempJarName!=null && !tempJarName.equals("")){
                    count = 0;
                    if(!oldJarList.contains(fileNameList.get(i))){
                        oldJarList.add(fileNameList.get(i));
                        duplicateEntryMap.put(fileNameList.get(i),++count);
                    }else{
                        count = dupl    icateEntryMap.get(fileNameList.get(i));
                        duplicateEntryMap.put(fileNameList.get(i),++count);
                    }
                    tempJarName = "";
                }
            }
            compareName = "";
        }
    }catch (Exception e) {
        e.printStackTrace();
    }
    return duplicateEntryMap;
}    

findDuplicatesOrLowerVersion(List fileNameList)功能任务 - 只需找到重复项并粘贴地图,其中包含文件名和较低版本重复的时间。

Try this. The remaining file exist in the folder should be latest or files with out duplicates.Am using this for finding the oldest files.on the basis of that it will find the old and delete it.
This am only checking the name..Futher improvement you can made.

Where PatternClassifier is a class which contains the second method given here.