代码之家  ›  专栏  ›  技术社区  ›  flybywire

Maven:通过相对路径向jar添加依赖项

  •  205
  • flybywire  · 技术社区  · 15 年前

    但我不想将其添加到存储库中。原因是我需要我通常的maven命令,比如 mvn compile 等,以便开箱即用(无需开发人员要求(由他们自己将其添加到某个存储库中)。

    我希望jar位于源代码管理中的第三方库中,并通过pom.xml文件中的相对路径链接到它。

    9 回复  |  直到 15 年前
        1
  •  356
  •   radistao    4 年前

    我希望jar位于源代码管理中的第三方库中,并通过pom.xml文件中的相对路径链接到它。

    不用 A. system 作用域依赖项。这个 系统 应该避免范围限制,这种依赖关系在许多情况下(例如在汇编中)都不能很好地工作,它们带来的麻烦多于好处。

    因此,请声明项目的本地存储库:

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

    install:install-file localRepositoryPath 参数:

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

    看来 安装:安装文件 忽略 本地存储路径 当使用插件的2.2版时。但是,它适用于2.3及更高版本的插件。因此,请使用插件的完全限定名来指定版本:

    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

    系统 范围):

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

    这是一个比使用 范围,因为您的依赖关系将被视为好公民(例如,它将被包括在程序集中等)。

        2
  •  133
  •   Hendy Irawan    9 年前

    使用 system ${basedir} 是pom的目录。

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

    不过,建议您在存储库中安装jar,而不要将其提交给SCM——毕竟这正是maven试图消除的。

        3
  •  29
  •   Community CDub    7 年前

    这是另一种方法,除了我以前的答案在 Can I add jars to maven 2 build classpath without installing them?

    当使用多模块构建时,这将绕过限制,特别是当下载的JAR在父项目之外的子项目中引用时。通过在构建过程中创建POM和SHA1文件,这也减少了安装工作。它还允许文件驻留在项目中的任何位置,而无需修复名称或遵循maven存储库结构。

    这使用maven安装插件。为此,您需要设置一个多模块项目,并拥有一个表示生成的新项目,以便将文件安装到本地存储库中,并确保先安装一个。

    <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>
    

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

    <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>
    

    要安装多个文件,只需添加更多执行。

        4
  •  12
  •   RufusSC2    8 年前

    这对我有用:

    <dependency>
        <groupId>com.company.app</groupId>
        <artifactId>my-library</artifactId>
        <version>1.0</version>
        <scope>system</scope>
        <systemPath>${project.basedir}/lib/my-library.jar</systemPath>
    </dependency>
    

    然后,像这样手动为系统依赖项添加类路径

    <Class-Path>libs/my-library-1.0.jar</Class-Path>
    

    完整配置:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.4</version>
        <configuration>
            <archive>
                <manifestEntries>
                    <Build-Jdk>${jdk.version}</Build-Jdk>
                    <Implementation-Title>${project.name}</Implementation-Title>
                    <Implementation-Version>${project.version}</Implementation-Version>
                    <Specification-Title>${project.name} Library</Specification-Title>
                    <Specification-Version>${project.version}</Specification-Version>
                    <Class-Path>libs/my-library-1.0.jar</Class-Path>
                </manifestEntries>
                <manifest>
                    <addClasspath>true</addClasspath>
                    <mainClass>com.company.app.MainClass</mainClass>
                    <classpathPrefix>libs/</classpathPrefix>
                </manifest>
            </archive>
        </configuration>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>2.5.1</version>
        <executions>
            <execution>
                <id>copy-dependencies</id>
                <phase>package</phase>
                <goals>
                    <goal>copy-dependencies</goal>
                </goals>
                <configuration>
                    <outputDirectory>${project.build.directory}/libs/</outputDirectory>
                </configuration>
            </execution>
        </executions>
    </plugin>
    
        5
  •  9
  •   gunr2171    10 年前

    我以前 written about a pattern

    它与Pascal提出的解决方案非常相似,不过它将所有此类依赖项移动到一个专用的存储库模块中,这样您就不必在使用依赖项的任何地方重复它(如果是多模块构建)。

        6
  •  8
  •   Fulgencio Jara    10 年前

    ...
    
    <repositories>
       <repository>
           <id>lib_id</id>
           <url>file://${project.basedir}/lib</url>
       </repository>
    </repositories>
    
    ...
    
    <dependencies>
      ...
      <dependency>
          <groupId>com.mylibrary</groupId>
          <artifactId>mylibraryname</artifactId>
          <version>1.0.0</version>
      </dependency>
      ...
    </dependencies>
    
        7
  •  4
  •   Dean Hiller    12 年前

    我们切换到gradle,这在gradle中效果更好;)。我们只需指定一个文件夹,以便在这种临时情况下将jar放入其中。我们仍然在typicaly依赖项管理部分定义了大多数JAR(即与maven相同)。这只是我们定义的又一个依赖项。

        8
  •  2
  •   Community CDub    9 年前

    对Pascal发布的解决方案的一个小补充

    当我遵循这条路线时,我在安装ojdbcjar时在maven中遇到了一个错误。

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

    添加-DpomFile后,问题得到解决。

    $ 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
    
        9
  •  0
  •   user1942990    9 年前

    您可以使用eclipse生成可运行的Jar: