Maven: add a dependency to a jar by relative path

后端 未结 9 990
半阙折子戏
半阙折子戏 2020-11-21 23:21

I have a proprietary jar that I want to add to my pom as a dependency.

But I don\'t want to add it to a repository. The reason is that I want my usual maven commands

9条回答
  •  囚心锁ツ
    2020-11-22 00:12

    I want the jar to be in a 3rdparty lib in source control, and link to it by relative path from the pom.xml file.

    If you really want this (understand, if you can't use a corporate repository), then my advice would be to use a "file repository" local to the project and to not use a system scoped dependency. The system scoped should be avoided, such dependencies don't work well in many situation (e.g. in assembly), they cause more troubles than benefits.

    So, instead, declare a repository local to the project:

    
      
        my-local-repo
        file://${project.basedir}/my-repo
      
    
    

    Install your third party lib in there using install:install-file with the localRepositoryPath parameter:

    mvn install:install-file -Dfile= -DgroupId= \ 
                             -DartifactId= -Dversion= \
                             -Dpackaging= -DlocalRepositoryPath=
    

    Update: It appears that install:install-file ignores the localRepositoryPath when using the version 2.2 of the plugin. However, it works with version 2.3 and later of the plugin. So use the fully qualified name of the plugin to specify the version:

    mvn org.apache.maven.plugins:maven-install-plugin:2.3.1:install-file \
                             -Dfile= -DgroupId= \ 
                             -DartifactId= -Dversion= \
                             -Dpackaging= -DlocalRepositoryPath=
    

    maven-install-plugin documentation

    Finally, declare it like any other dependency (but without the system scope):

    
      your.group.id
      3rdparty
      X.Y.Z
    
    

    This is IMHO a better solution than using a system scope as your dependency will be treated like a good citizen (e.g. it will be included in an assembly and so on).

    Now, I have to mention that the "right way" to deal with this situation in a corporate environment (maybe not the case here) would be to use a corporate repository.

提交回复
热议问题