Maven: add a dependency to a jar by relative path

后端 未结 9 1007
半阙折子戏
半阙折子戏 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:06

    Using the system scope. ${basedir} is the directory of your pom.

    <dependency>
        <artifactId>..</artifactId>
        <groupId>..</groupId>
        <scope>system</scope>
        <systemPath>${basedir}/lib/dependency.jar</systemPath>
    </dependency>
    

    However it is advisable that you install your jar in the repository, and not commit it to the SCM - after all that's what maven tries to eliminate.

    0 讨论(0)
  • 2020-11-22 00:07

    One small addition to the solution posted by Pascal

    When I followed this route, I got an error in maven while installing ojdbc jar.

    [INFO] --- maven-install-plugin:2.5.1:install-file (default-cli) @ validator ---
    [INFO] pom.xml not found in ojdbc14.jar
    

    After adding -DpomFile, the problem was resolved.

    $ mvn install:install-file -Dfile=./lib/ojdbc14.jar -DgroupId=ojdbc \
       -DartifactId=ojdbc -Dversion=14 -Dpackaging=jar -DlocalRepositoryPath=./repo \
       -DpomFile=~/.m2/repository/ojdbc/ojdbc/14/ojdbc-14.pom
    
    0 讨论(0)
  • 2020-11-22 00:09

    I've previously written about a pattern for doing this.

    It is very similar to the solution proposed by Pascal, though it moves all such dependencies into a dedicated repository module so that you don't have to repeat it everywhere the dependency is used if it is a multi-module build.

    0 讨论(0)
  • 2020-11-22 00:09

    You can use eclipse to generate a runnable Jar : Export/Runable Jar file

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

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

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

    mvn install:install-file -Dfile=<path-to-file> -DgroupId=<myGroup> \ 
                             -DartifactId=<myArtifactId> -Dversion=<myVersion> \
                             -Dpackaging=<myPackaging> -DlocalRepositoryPath=<path>
    

    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=<path-to-file> -DgroupId=<myGroup> \ 
                             -DartifactId=<myArtifactId> -Dversion=<myVersion> \
                             -Dpackaging=<myPackaging> -DlocalRepositoryPath=<path>
    

    maven-install-plugin documentation

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

    <dependency>
      <groupId>your.group.id</groupId>
      <artifactId>3rdparty</artifactId>
      <version>X.Y.Z</version>
    </dependency>
    

    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.

    0 讨论(0)
  • 2020-11-22 00:16

    This is another method in addition to my previous answer at Can I add jars to maven 2 build classpath without installing them?

    This will get around the limit when using multi-module builds especially if the downloaded JAR is referenced in child projects outside of the parent. This also reduces the setup work by creating the POM and the SHA1 files as part of the build. It also allows the file to reside anywhere in the project without fixing the names or following the maven repository structure.

    This uses the maven-install-plugin. For this to work, you need to set up a multi-module project and have a new project representing the build to install files into the local repository and ensure that one is first.

    You multi-module project pom.xml would look like this:

    <packaging>pom</packaging>
    <modules>
    <!-- The repository module must be first in order to ensure
         that the local repository is populated -->
        <module>repository</module>
        <module>... other modules ...</module>
    </modules>
    

    The repository/pom.xml file will then contain the definitions to load up the JARs that are part of your project. The following are some snippets of the pom.xml file.

    <artifactId>repository</artifactId>
    <packaging>pom</packaging>
    

    The pom packaging prevents this from doing any tests or compile or generating any jar file. The meat of the pom.xml is in the build section where the maven-install-plugin is used.

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-install-plugin</artifactId>
                <executions>
                    <execution>
                            <id>com.ibm.db2:db2jcc</id>
                            <phase>verify</phase>
                            <goals>
                                <goal>install-file</goal>
                            </goals>
                            <configuration>
                                <groupId>com.ibm.db2</groupId>
                                <artifactId>db2jcc</artifactId>
                                <version>9.0.0</version>
                                <packaging>jar</packaging>
                                <file>${basedir}/src/jars/db2jcc.jar</file>
                                <createChecksum>true</createChecksum>
                                <generatePom>true</generatePom>
                            </configuration>
                    </execution>
                    <execution>...</execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    

    To install more than one file, just add more executions.

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