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

时间:2010-02-09 14:30:22

标签: java maven-2 build-process build dependencies

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

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

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

可以这样做吗?怎么样?

9 个答案:

答案 0 :(得分:324)

  

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

如果您真的想要这个(了解,如果您不能使用公司存储库),那么我的建议是使用项目本地的“文件存储库”并不使用 system作用域依赖。应该避免使用system范围,这种依赖在许多情况下(例如在汇编中)不能很好地工作,它们会带来更多麻烦而不是好处。

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

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

使用带有localRepositoryPath参数的install:install-file安装您的第三方lib:

<击>

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

<击>

更新:使用插件2.2版时,install:install-file似乎忽略了localRepositoryPath。但是,它适用于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

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

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

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

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

答案 1 :(得分:112)

使用system范围。 ${basedir}是您的目录。

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

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

答案 2 :(得分:27)

除了我之前在Can I add jars to maven 2 build classpath without installing them?

的回答之外,这是另一种方法

这将在使用多模块构建时达到极限,特别是如果在父项之外的子项目中引用了下载的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>

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

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

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>

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

答案 3 :(得分:9)

我之前written about a pattern这样做了。

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

答案 4 :(得分: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 :(得分:4)

我们切换到gradle,这在gradle中工作得更好;)。我们只是指定一个文件夹,我们可以将jar放入这样的临时情况。我们仍然将大多数罐子定义为典型的依赖管理部分(即与maven相同)。这只是我们定义的一个依赖项。

所以基本上现在我们可以把我们想要的任何jar放到我们的lib目录中进行临时测试,如果它不是某个maven存储库的话。

答案 6 :(得分:4)

基本上,将其添加到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>

答案 7 :(得分:2)

Pascal发布的解决方案的一小部分内容

当我遵循这条路线时,我在安装ojdbc jar时遇到了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

答案 8 :(得分:0)

您可以使用eclipse生成可运行的Jar: 导出/可运行Jar文件