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

fly*_*ire 221 java dependencies build-process maven-2 build

我有一个专有的jar,我想作为依赖添加到我的pom.

但我不想将它添加到存储库.原因是我希望我的常用maven命令等mvn compile开箱即用.(没有要求开发人员将其自己添加到某个存储库中).

我希望jar在源代码控制中位于第3方库中,并通过pom.xml文件的相对路径链接到它.

可以这样做吗?怎么样?

Pas*_*ent 332

我希望jar在源代码控制中位于第3方库中,并通过pom.xml文件的相对路径链接到它.

如果你真的想这个(明白,如果你不能使用公司资源库),那么我的建议是使用"文件库"本地的项目,并没有使用一个system范围依赖.system应避免使用范围,这种依赖在许多情况下(例如在装配中)不能很好地工作,它们会带来更多麻烦而不是好处.

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

<repositories>
  <repository>
    <id>my-local-repo</id>
    <url>file://${basedir}/my-repo</url>
  </repository>
</repositories>
Run Code Online (Sandbox Code Playgroud)

在那里使用安装您的第三方的lib install:install-filelocalRepositoryPath参数:

mvn install:install-file -Dfile=<path-to-file> -DgroupId=<myGroup> \ 
                         -DartifactId=<myArtifactId> -Dversion=<myVersion> \
                         -Dpackaging=<myPackaging> -DlocalRepositoryPath=<path>
Run Code Online (Sandbox Code Playgroud)

更新:似乎install:install-file忽略了localRepositoryPath使用插件版本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>
Run Code Online (Sandbox Code Playgroud)

maven-install-plugin文档

最后,声明它像任何其他依赖项(但没有system范围):

<dependency>
  <groupId>your.group.id</groupId>
  <artifactId>3rdparty</artifactId>
  <version>X.Y.Z</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

这是恕我直言,比使用system范围更好的解决方案,因为您的依赖将被视为一个好公民(例如,它将被包括在一个程序集等等).

现在,我必须提到在公司环境中处理这种情况的"正确方法"(可能不是这里的情况)将是使用公司存储库.

  • @ leif81因为然后将repo和库检入SCM存储库 - >执行源检出的任何人都拥有构建库/应用程序副本所需的一切. (5认同)
  • 我遇到了与@lemon相同的问题,我通过使用单个`.`来修复`basedir /./ my-local-repo`. (4认同)
  • 这是一个好主意,但在Maven 2.2.1上,安装插件似乎忽略了`localRepositoryPath` ... (2认同)
  • 为什么要声明本地回购?为什么不让它和其余的一起进入 ~/.m2/ 。 (2认同)
  • 包装必须为jar,因此-Dpackaging = jar (2认同)

Boz*_*zho 121

使用system范围.${basedir}是你的pom目录.

<dependency>
    <artifactId>..</artifactId>
    <groupId>..</groupId>
    <scope>system</scope>
    <systemPath>${basedir}/lib/dependency.jar</systemPath>
</dependency>
Run Code Online (Sandbox Code Playgroud)

但是,建议您将jar安装在存储库中,而不是将其提交给SCM - 毕竟maven试图消除它.

  • 必须尽可能避免范围系统.在存储库中安装JAR是一个更好的解决方案...... (14认同)
  • 是的,如果可能的话.他明确表示他不想把它放在存储库中.我添加了一条评论,指出这不是一个好习惯.但它确实有效. (12认同)

Arc*_*ano 27

这是除了我以前的答案之外的另一种方法我可以将jar添加到maven 2构建类路径而不安装它们吗?

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

这使用maven-install-plugin.为此,您需要设置一个多模块项目,并拥有一个代表构建的新项目,以便将文件安装到本地存储库中,并确保首先是一个.

你的多模块项目pom.xml看起来像这样:

<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>
Run Code Online (Sandbox Code Playgroud)

然后,repository/pom.xml文件将包含用于加载属于项目一部分的JAR的定义.以下是pom.xml文件的一些片段.

<artifactId>repository</artifactId>
<packaging>pom</packaging>
Run Code Online (Sandbox Code Playgroud)

pom包装阻止它进行任何测试或编译或生成任何jar文件.pom.xml的内容位于使用maven-install-plugin的构建部分.

<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>
Run Code Online (Sandbox Code Playgroud)

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


Bre*_*ter 9

我之前写过关于这样做的模式.

它与Pascal提出的解决方案非常相似,尽管它将所有这些依赖项移动到专用存储库模块中,这样如果它是一个多模块构建,则不必在使用依赖项的任何地方重复它.


Ful*_*ara 8

基本上,将其添加到 pom.xml 中:

...

<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>
Run Code Online (Sandbox Code Playgroud)


Ruf*_*SC2 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>
Run Code Online (Sandbox Code Playgroud)

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

<Class-Path>libs/my-library-1.0.jar</Class-Path>
Run Code Online (Sandbox Code Playgroud)

完整配置:

<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>
Run Code Online (Sandbox Code Playgroud)


Dea*_*ler 5

我们切换到 gradle,这在 gradle 中效果更好;)。我们只需指定一个文件夹,我们可以将 jar 放入其中,以应对类似的临时情况。我们仍然在典型的依赖管理部分定义了大部分 jars(即与 Maven 相同)。这只是我们定义的又一个依赖项。

所以基本上现在我们可以将任何我们想要的 jar 放入 lib 目录中进行临时测试(如果它不是 Maven 存储库中的某个地方)。