How to add local jar files to a Maven project?

如何直接在我的项目的库源中添加本地jar文件(尚未成为Maven存储库的一部分)?


Install the JAR into your local Maven repository as follows:

mvn install:install-file
   -Dfile=<path-to-file>
   -DgroupId=<group-id>
   -DartifactId=<artifact-id>
   -Dversion=<version>
   -Dpackaging=<packaging>
   -DgeneratePom=true

Where: <path-to-file>  the path to the file to load
   <group-id>      the group that the file should be registered under
   <artifact-id>   the artifact name for the file
   <version>       the version of the file
   <packaging>     the packaging of the file e.g. jar

Reference


您可以直接添加本地依赖项(正如在包含propriatery库的构建maven项目中提到的),如下所示:

<dependency>
    <groupId>com.sample</groupId>
    <artifactId>sample</artifactId>
    <version>1.0</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/src/main/resources/yourJar.jar</systemPath>
</dependency>

Firstly I would like to give credit for this answer to anonymous stackoverflow user - I am pretty sure I've seen similar answer here before - but now I cannot find it.

The best option for having local jar files as a dependency is to create local maven repository. Such repo is nothing else than proper directory structure with pom files in it.

On my example: I have master project on ${master_project} location and subroject1 is on ${master_project}/${subproject1}

then I am creating mvn repository in: ${master_project}/local-maven-repo

In pom file in subproject1 located ${master_project}/${subproject1}/pom.xml repository needs to be specified which would take file path as an url parameter:

<repositories>
    <repository>
        <id>local-maven-repo</id>
        <url>file:///${project.parent.basedir}/local-maven-repo</url>
    </repository>
</repositories>

Dependency can be specified as for any other repository. This makes your pom repository independent. For instance once desired jar is available in maven central you just need to delete it from your local repo and it will be pulled from default repo.

    <dependency>
        <groupId>org.apache.felix</groupId>
        <artifactId>org.apache.felix.servicebinder</artifactId>
        <version>0.9.0-SNAPSHOT</version>
    </dependency>

The last but not least thing to do is to add jar file to local repository using -DlocalRepositoryPath switch like here:

mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file  
    -Dfile=/some/path/on/my/local/filesystem/felix/servicebinder/target/org.apache.felix.servicebinder-0.9.0-SNAPSHOT.jar 
    -DgroupId=org.apache.felix -DartifactId=org.apache.felix.servicebinder 
    -Dversion=0.9.0-SNAPSHOT -Dpackaging=jar 
    -DlocalRepositoryPath=${master_project}/local-maven-repo

Onece jar file is installed such mvn repo can be committed to code repository and whole set up is system independent. (working example in github)

I agree that having JARs committed to source code repo is not a good practice but in real life quick and dirty solution sometimes is better than full blown nexus repo to host one jar that you cannot publish.

链接地址: http://www.djcxy.com/p/5048.html

上一篇: 关于C ++中指针和引用的混淆

下一篇: 如何将本地jar文件添加到Maven项目?