Jar package by name, you can acquire the information GAV maven

Annoyance: When we do have a bunch of pieces of tripartite jar package, you want to turn into maven management requires a pom into a configuration file, and GAV have to collect information.

In order to quickly generate the following information, we can ....

GAV:groupId + artifactId + version

<dependency>

  <groupId></group>

  <artifactId></artifactId>

  <version></version>

</dependency>

 

1. unzip jar package file to extract information GAV

   Principle: pom.properties file jar package GAV recorded information. (Because not every jar package has pom.properties file, the file is no less than the query. At this point you can use Method 2, or manually search the Internet)

1.1 sh script mode (or script feel easy to use)

      getGAV.sh /tmp/lib/javax.servlet-3.0.1.jar test/lib/javax.servlet-3.0.1.jar

      Parameter transfer the file path on the line in support of multi-parameter

#!/bin/bash

##获取jar包的maven依赖GAV信息
function main()
{
    local notFoundList
    for var in $@
    do
        if [ ! -z "$var" ];then
            local jarName=`echo $var | sed 's#.*/##' | sed 's#.jar##g'`
            local dirName=`dirname $var`
            [ ! -d "$dirName/$jarName" ] && unzip $var -d $dirName/$jarName &>/dev/null
            if [ -d "$dirName/$jarName" ];then
                local pomProperties=`find $dirName/$jarName -name pom.properties`
                if [ "$pomProperties" != "" ];then
                    dos2unix $pomProperties &>/dev/null
                    echo "<dependency>"
                    echo "<groupId>"`grep "groupId" $pomProperties | cut -d'=' -f2`"</groupId>"
                    echo "<artifactId>"`grep "artifactId" $pomProperties | cut -d'=' -f2`"</artifactId>"
                    echo "<version>"`grep "version" $pomProperties | cut -d'=' -f2`"</version>"
                    echo "</dependency>"
                else
                    notFoundList="$var $notFoundList"
                fi
                [ -d "$dirName/$jarName" ] && rm -rf "$dirName/$jarName"
            else
                notFoundList="$var $notFoundList"
            fi
        fi
    done
    if [ "$notFoundList" != "" ];then
        echo "============="
        echo "notFoundList: $notFoundList"
        echo "============="
    fi
}

main $@

 

2. jar package name, the acquired dependency information GAV maven

   Principle: Access https://mvnrepository.com/search inquiry for information. (Because artifactId just the right version and not a hundred percent accurate, so the precision rate is not hundred percent)

       / ** 
         * jar package by name, the acquired dependency information maven (GAV: the groupId the artifactId Version) 
         * @param jarFilePaths jar package full path name or file
          * / 
        public  static  void getGavFromWeb (String ... jarFilePaths) { 
            List <String> = notFoundList new new the ArrayList <String> ();
             int successCount = 0 ;
             for (String jarFilePath: jarFilePaths) {
                 IF (! StringUtils.isEmpty (jarFilePath) && jarFilePath.endsWith ( "JAR." )) { 
                    String searchUrl = "HTTPS: //mvnrepository.com/search " ;
                    JAR File = new new File (jarFilePath);
                     // remove .jar suffix 
                    String JARname = jar.getName () substring (0, jar.getName () lastIndexOf (.. "." ));
                     // remove the version number, do not get some accurate artifactId, keyword search for 
                    String artifactIdSimple = jarName.replaceAll (- |, "" "[_] \\ * d." );
                     // get is not necessarily an accurate version, for filtering search results 
                    String versionSimple jarName.substring = (artifactIdSimple.length ()) ReplaceFirst ( "[- | _]", "." );
                     the try { 
                        the Document DOC = Jsoup.connect (searchUrl).data("q", ArtifactIdSimple) .get (); 
                        Elements PS = doc.getElementsByClass ( "IM-SUBTITLE" );
                         IF ! ( CollectionUtils.isEmpty (PS)) {
                             // the artifactId search results take the first 
                            Element p = ps.get (0 );
                             IF (p.childNodeSize ()>. 1 ) { 
                                String artifactUrl = p.child (. 1) .absUrl ( "the href" );
                                 // taken groupId artifactId and search results from the hyperlink in 
                                String [] ids = artifactUrl. split ( "/" );
                                The groupId String = IDS [ids.length - 2 ]; 
                                String the artifactId = IDS [ids.length -. 1 ]; 
                                
                                String Version = "" ; 
                                DOC = Jsoup.connect (artifactUrl) .get (); 
                                Elements AS = doc.getElementsByClass ( "vbtn Release" );
                                 // Version corresponding to the search results to take, if not then take the first 
                                IF (! CollectionUtils.isEmpty (AS)) {
                                     IF (!StringUtils.isEmpty(versionSimple)) {
                                        for (Element a : as) {
                                            if (versionSimple.equals(a.text())) {
                                                version = versionSimple;
                                                break;
                                            }
                                        }
                                    }
                                    if (StringUtils.isEmpty(version)) {
                                        version = as.get(0).text();
                                    }
                                }
                                System.out.println(StringUtils.format("<dependency>\n<groupId>{}</groupId>\n<artifactId>{}</artifactId>\n<version>{}</version>\n</dependency>", groupId, artifactId, version));
                                successCount++;
                            } else {
                                notFoundList.add(jarFilePath);
                            }
                        } else {
                            notFoundList.add(jarFilePath);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                        notFoundList.add(jarFilePath);
                    }
                } else {
                    notFoundList.add(jarFilePath);
                }
            }
            System.out.println();
            System.out.println(StringUtils.format("success: {}, failed: {}, sum: {}", successCount, notFoundList.size(), jarFilePaths.length));
            System.out.println(StringUtils.format("notFoundList: {}", notFoundList));
        }

 ps: code StringUtils.format () may be a compile error, is my own package of string concatenation, mainly to concatenate strings look good, spliced ​​into a normal string on the line.

   public static String format(String str, Object... args) {
     return String.format(str.replaceAll("\\{\\}", "%s"), args);
   }

Guess you like

Origin www.cnblogs.com/zhangzongjian/p/11784930.html