How to add local jar files to a Maven project?

后端 未结 30 3378
悲&欢浪女
悲&欢浪女 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:12

    Add local jar libraries, their sources and javadoc to a Maven project

    If you have pre-compiled jar files with libraries, their sources and javadoc, then you can install them to your local Maven repository like this:

    mvn install:install-file
        -Dfile=awesomeapp-1.0.1.jar \
        -DpomFile=awesomeapp-1.0.1.pom \
        -Dsources=awesomeapp-1.0.1-sources.jar \
        -Djavadoc=awesomeapp-1.0.1-javadoc.jar \
        -DgroupId=com.example \
        -DartifactId=awesomeapp \
        -Dversion=1.0.1 \
        -Dpackaging=jar
    

    Then in your project you can use this libraries:

    
    
        com.example
        awesomeapp
        1.0.1
    
    

    See: maven-install-plugin usage.


    Or you can build these libraries yourself with their sources and javadoc using maven-source-plugin and maven-javadoc-plugin, and then install them.

    Example project: library

    
    
        4.0.0
    
        https://example.com/awesomeapp
    
        com.example
        awesomeapp
        awesomeapp
        1.0.1
        jar
    
        
            12
        
    
        
            awesomeapp
            install
    
            
                
                    org.apache.maven.plugins
                    maven-compiler-plugin
                    3.8.1
                    
                        ${java.version}
                        ${java.version}
                        UTF-8
                    
                
                
                    true
                    org.apache.maven.plugins
                    maven-source-plugin
                    3.2.1
                    
                        
                            attach-sources
                            jar
                        
                    
                
                
                    true
                    org.apache.maven.plugins
                    maven-javadoc-plugin
                    3.2.0
                    
                        
                            attach-javadocs
                            jar
                        
                    
                
            
        
    
    

    Execute maven install goal:

    mvn install
    

    Check your local Maven repository:

    ~/.m2/repository/com/example/awesomeapp/1.0.1/
     ├─ _remote.repositories
     ├─ awesomeapp-1.0.1.jar
     ├─ awesomeapp-1.0.1.pom
     ├─ awesomeapp-1.0.1-javadoc.jar
     └─ awesomeapp-1.0.1-sources.jar
    

    Then you can use this library:

    
    
        com.example
        awesomeapp
        1.0.1
    
    

提交回复
热议问题