How to add local jar files to a Maven project?

后端 未结 30 3926
悲&欢浪女
悲&欢浪女 2020-11-21 04:58

How do I add local jar files (not yet part of the Maven repository) directly in my project\'s library sources?

30条回答
  •  萌比男神i
    2020-11-21 05:16

    I want to share a code where you can upload a folder full of jars. It's useful when a provider doesn't have a public repository and you need to add lots of libraries manually. I've decided to build a .bat instead of call directly to maven because It could be Out of Memory errors. It was prepared for a windows environment but is easy to adapt it to linux OS:

    import java.io.File;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Date;
    import java.util.jar.Attributes;
    import java.util.jar.JarFile;
    import java.util.jar.Manifest;
    
    public class CreateMavenRepoApp {
    
        private static final String OCB_PLUGIN_FOLDER = "C://your_folder_with_jars";
    
        public static void main(String[] args) throws IOException {
    
        File directory = new File();
        //get all the files from a directory
        PrintWriter writer = new PrintWriter("update_repo_maven.bat", "UTF-8");
        writer.println("rem "+ new Date());  
        File[] fList = directory.listFiles();
        for (File file : fList){
            if (file.isFile()){               
            String absolutePath = file.getAbsolutePath() ;
            Manifest  m = new JarFile(absolutePath).getManifest();
            Attributes attributes = m.getMainAttributes();
            String symbolicName = attributes.getValue("Bundle-SymbolicName");
    
            if(symbolicName!=null &&symbolicName.contains("com.yourCompany.yourProject")) {
                String[] parts =symbolicName.split("\\.");
                String artifactId = parts[parts.length-1];
                String groupId = symbolicName.substring(0,symbolicName.length()-artifactId.length()-1);
                String version = attributes.getValue("Bundle-Version");
                String mavenLine= "call mvn org.apache.maven.plugins:maven-install-plugin:2.5.1:install-file -Dfile="+ absolutePath+" -DgroupId="+ groupId+" -DartifactId="+ artifactId+" -Dversion="+ version+" -Dpackaging=jar ";
                writer.println(mavenLine);          
            }
    
            }
        }
        writer.close();
        }
    
    }
    

    After run this main from any IDE, run the update_repo_maven.bat.

提交回复
热议问题