How to add local jar files to a Maven project?

后端 未结 30 3257
悲&欢浪女
悲&欢浪女 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条回答
  • 2020-11-21 05:13

    Of course you can add jars to that folder. But maybe it does not what you want to achieve...

    If you need these jars for compilation, check this related question: Can I add jars to maven 2 build classpath without installing them?

    Also, before anyone suggests it, do NOT use the system scope.

    0 讨论(0)
  • 2020-11-21 05:15

    This is a short syntax for newer versions:

    mvn install:install-file -Dfile=<path-to-file>
    

    It works when the JAR was built by Apache Maven - the most common case. Then it'll contain a pom.xml in a subfolder of the META-INF directory, which will be read by default.

    Source: http://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-21 05:18
    <dependency>
        <groupId>group id name</groupId>
        <artifactId>artifact name</artifactId>
        <version>version number</version>
        <scope>system</scope>
        <systemPath>jar location</systemPath>
    </dependency>
    
    0 讨论(0)
  • 2020-11-21 05:18

    One way is to upload it to your own Maven repository manager (such as Nexus). It's good practice to have an own repository manager anyway.

    Another nice way I've recently seen is to include the Maven Install Plugin in your build lifecycle: You declare in the POM to install the files to the local repository. It's a little but small overhead and no manual step involved.

    http://maven.apache.org/plugins/maven-install-plugin/install-file-mojo.html

    0 讨论(0)
  • 2020-11-21 05:18

    In Apache Maven 3.5.4, I had to add double quotation. Without double quotation it wasn't worked for me.

    example: mvn install:install-file "-Dfile=location to the jar file" "-DgroupId=group id" "-DartifactId=artifact id" "-Dversion=version" "-Dpackaging=package type"

    0 讨论(0)
提交回复
热议问题