【发布时间】:2018-06-27 16:07:21
【问题描述】:
我有一个本地 jar,我需要将其作为依赖项添加到我的 maven 项目中,以包含在项目的已发布 jar 中。
它被放置在“my_project/lib/external1.jar”的项目中,起初我将它添加到依赖项中,如下所示:
<dependency>
<groupId>installExternalJars11</groupId>
<artifactId>externalJar11</artifactId>
<version>3.1.14</version>
<systemPath>${project.basedir}//lib/external1.jar</systemPath>
<scope>system</scope>
</dependency>
它编译成功,但 jar 内容不包含在生成的项目 jar 中,因此,当我使用项目 jar 时,我会为这些类获得 NoClassDefFoundError,尽管我正在使用以下插件来打包所有依赖项在输出罐子里
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
我发现有几个帖子推荐使用maven install插件来安装它,然后将其添加为依赖项,所以我遵循了这种方法如下:
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com</groupId>
<artifactId>waheed</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>waheed</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<version.maven-install-plugin>2.5.2</version.maven-install-plugin>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>${version.maven-install-plugin}</version>
<configuration>
<repositoryLayout>default</repositoryLayout>
<groupId>installExternalJars</groupId>
<artifactId>externalJar1</artifactId>
<version>3.1.14</version>
<file>${project.basedir}/lib/external1.jar</file>
<packaging>jar</packaging>
<generatePom>true</generatePom>
</configuration>
<executions>
<execution>
<id>install-ibm-foundation</id>
<phase>validates</phase>
<goals>
<goal>install-file</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>installExternalJars11</groupId>
<artifactId>externalJar11</artifactId>
<version>3.1.14</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
在 Eclipse 中,我使用以下错误突出显示依赖项:
Missing artifact installExternalJars11:externalJar11:jar:3.1.14
当我运行 maven build(来自 eclipse:右键单击 pom.xml > run as > maven build)时,我收到以下错误(显然 maven 正在我的 nexus 存储库中寻找 jar,尽管它在安装部分):
Failure to find installExternalJars11:externalJar11:jar:3.1.14
in http://ur_to_nexus/nexus/content/repositories/central/ was cached in
the local repository, resolution will not be reattempted until the
update interval of ubknexus has elapsed or updates are forced
尝试了这里提出的修复 https://stackoverflow.com/a/6112344/458999 但没有奏效
P.S:当我将 Jars 添加到 eclipse 中的构建路径(Java 构建路径 > 库 > 添加 Jars)并从 eclipse 运行项目(或从 eclipse 导出并运行导出的 jar)时,它可以工作
【问题讨论】:
-
将 jar 文件安装到您的存储库管理器中,这就是解决方案...
-
@khmarbaise 不是安装插件部分应该做的吗?我应该以不同的方式来做吗?
-
调用 maven-install-plugin 来安装一个 jar 完全是错误的方式......最好通过 Repository Manager 进行,它可以让你的构建更容易,并且会消除你的 pom 文件中的复杂性,而且还会阻止你在您的版本控制系统中提交二进制工件...另一件事是这个 jar 可能具有传递依赖项,这些依赖项永远不会以这种方式工作...
-
@khmarbaise 好的,我明白你的意思,但除此之外,是否可以使用本地 jar 作为依赖项?使用 Maven 安装插件?我想知道为什么它不适合我。