Friday, 6 January 2012

Bulk upload of maven artifacts to remote repo

If you have an artifact you want to upload to a remote repository (i.e. internal company repo) then you use the command:
mvn deploy:deploy-file -Durl=file://C:\m2-repo \
                       -DrepositoryId=some.id \
                       -Dfile=your-artifact-1.0.jar \
                       [-DpomFile=your-pom.xml] \


However if you have more than a few files and they're in a nested directory structure (like your local repo) then this is not the way to do it. There doesn't seem to be any tool for either maven or nexus to do this, so here's a quick and dirty way I hacked together:
/**
 * run as java Importer [dirtosearch]
 **/
public class Importer {
   private String directory;
   private final static String repositoryId = "repo1";
   private final static String url = "http://remoteserver/nexus/content/repositories/repo1/";
   private final static String mvn = "C:/java/apache-maven-3.0.3/bin/mvn.bat";

   /**
    * @param directory
    */
   public Importer(String directory) {
      super();
      this.directory = directory;
   }

   public static void main(String[] args) {
      Importer importer = new Importer(args[0]);
      importer.go();
   }

   private void go() {
      try {
         File dir = new File(directory);

         doDir(dir);
      }
      catch (Throwable e) {
         e.printStackTrace();
      }
   }

   private void doDir(File dir) throws IOException, InterruptedException {
      File[] listFiles = dir.listFiles(new PomFilter());
      if (listFiles != null) {
         for (File pom : listFiles) {
            doPom(pom);
         }
      }

      File[] listDirs = dir.listFiles(new DirFilter());
      if (listDirs != null) {
         for (File subdir : listDirs) {
            doDir(subdir);
         }
      }
   }

   private void doPom(File pom) throws IOException, InterruptedException {
      File base = pom.getParentFile();
      String fileName = pom.getName();
      String jarName = fileName.substring(0, fileName.length() - 3) + "jar";
      File jar = new File(base.getAbsolutePath() + "/" + jarName);

      String exec = mvn + " deploy:deploy-file -DrepositoryId=" + repositoryId + " -Durl=" + url;
      if (jar.exists()) {
         exec += " -Dfile=\"" + jar.getAbsolutePath() + "\"";
      }
      else {
         exec += " -Dfile=\"" + pom.getAbsolutePath() + "\"";
      }
      exec += " -DpomFile=\"" + pom.getAbsolutePath() + "\"";
      exec(exec);

   }

   private void exec(String exec) throws InterruptedException, IOException {
      System.out.println(exec);
      Process p = Runtime.getRuntime().exec(exec);
      String line;
      BufferedReader bri = new BufferedReader(new InputStreamReader(p.getInputStream()));
      BufferedReader bre = new BufferedReader(new InputStreamReader(p.getErrorStream()));
      while ((line = bri.readLine()) != null) {
         System.out.println(line);
      }
      bri.close();
      while ((line = bre.readLine()) != null) {
         System.out.println(line);
      }
      bre.close();
      p.waitFor();
      System.out.println("Done.");
   }

   private class PomFilter implements java.io.FileFilter {

      @Override
      public boolean accept(File pathname) {
         return pathname.getName().endsWith(".pom");
      }
   }

   private class DirFilter implements java.io.FileFilter {

      @Override
      public boolean accept(File pathname) {
         return pathname.isDirectory();
      }
   }
}

1 comment: