Generate Maven dependency to be added to POM file

Suppose you have lot of .jar files in your lib folder.Adding each of the jar entry into the POM.xml file is quite tedious. So I came up with the below java program which will generate the dependency tag. A user can just paste its output directly into the POM.xml The below utility also generates the mvn install scripts that can be used to-- add jars to the local maven repo.
 package com.prdc.spring3;  
 import java.io.File;  
 public class DependencyGenerator {  
      private String pom;  
      private String install;  
      public static String generate_dependency_pom(String lib_location){  
           String pom = null;  
           String files=null;  
           File folder = new File(lib_location);  
           File[] listOfFiles = folder.listFiles();  
           for (int i = 0; i < listOfFiles.length; i++) {  
                if (listOfFiles[i].isFile()) {  
                     files = listOfFiles[i].getName();  
                     pom = "\n\t<dependency> \n \t\t<groupId>"+ listOfFiles[i].getName().substring(0,files.lastIndexOf("-"))  
                               + "</groupId>"  
                               + "\n\t\t<artifactId>"  
                               + listOfFiles[i].getName().replaceAll(".jar", "")  
                               + "</artifactId>"  
                               + " \n\t\t<version>"  
                               + files.substring(files.lastIndexOf("-") + 1)  
                                         .replaceAll(".jar", "") + "</version>"  
                               + " \n\t\t<scope>provided</scope>\n\t</dependency>";  
                     System.out.println(pom);  
                }  
           }  
           return pom;  
      }  
      public static String generate_lib_install_scripts(String lib_location){  
           String install = null;  
           String pom = null;  
           String files=null;  
           File folder = new File(lib_location);  
           File[] listOfFiles = folder.listFiles();  
           for (int i = 0; i < listOfFiles.length; i++) {  
                if (listOfFiles[i].isFile()) {  
                     files = listOfFiles[i].getName();  
                     install = install  
                               + "\n mvn install:install-file -Dfile=E:\\Spring_Struts2_Integration\\src\\main\\webapp\\WEB-INF\\lib\\"  
                               + listOfFiles[i].getName()  
                               + " -DgroupId="  
                               + listOfFiles[i].getName().substring(0,  
                                         files.lastIndexOf("-"))  
                               + " -DartifactId="  
                               + listOfFiles[i].getName().replaceAll(".jar", "")  
                               + " -Dversion="  
                               + files.substring(files.lastIndexOf("-") + 1)  
                                         .replaceAll(".jar", "")  
                               + " -Dpackaging=jar ";  
                }  
           }  
           return install;  
      }  
      public static void main(String[] args) {  
           // Directory path here  
           String install = null;  
           String pom=null;  
           String path = "E:\\Spring_Struts2_Integration\\src\\main\\webapp\\WEB-INF\\lib";  
           DependencyGenerator dg=new DependencyGenerator();  
           pom=dg.generate_dependency_pom(path);  
           install=dg.generate_lib_install_scripts(path);  
           System.out.println(pom);  
           System.out.println(install);  
      }  
 }  

Comments

Popular posts from this blog

defining functions clojure

Integrating Struts2 with Spring Security using Custom Login Form