繁体   English   中英

Maven 项目中的非 Maven 依赖项

[英]Non-Maven dependecies in a Maven project

如何在 IntelliJ IDEA 中添加外部 jar 文件作为 Maven 项目的依赖项? 因为当我将其添加到依赖项列表中并尝试使用 Maven 进行编译时,我收到了找不到该依赖项的错误。

理想情况下,您应该使用mvn deploy:deploy-file将 JAR 部署到您的存储库。

如果这是不可能的,您可以将依赖项scope设置为system ,然后在依赖项中包含一个systemPath ,该依赖项为 jar 提供该路径。 这在POM 参考 - 依赖项中进行了解释,并带有警告,即任何依赖于具有system范围依赖项的工件的工件也将期望通过systemPath找到 jar。

你可以

  • 像这样定义系统/本地依赖项:

     <dependency> <groupId>example</groupId> <artifactId>example</artifactId> <version>1.0.0</version> <scope>system</scope> <systemPath>lib/example-1.0.0.jar</systemPath> </dependency>

正如 Gimby 指出的那样,请注意系统依赖项应该“就在那里”,因此它们不会与您的工件一起打包和部署。 请参阅此问题以供参考。

  • 将工件安装到您的本地存储库中:

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

步骤 1:使用pom.xml的目标install-file配置maven-install-plugin

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-install-plugin</artifactId>
    <executions>
        <execution>
            <id>install-external-non-maven-jar-MWS-Client-into-local-maven-repo</id>
            <phase>clean</phase>
            <configuration>
                <repositoryLayout>default</repositoryLayout>
                <groupId>com.amazonservices.mws</groupId>
                <artifactId>mws-client</artifactId>
                <version>1.0</version>
                <file>${project.basedir}/lib/MWSClientJavaRuntime-1.0.jar</file>
                <packaging>jar</packaging>
                <generatePom>true</generatePom>
            </configuration>
            <goals>
                <goal>install-file</goal>
            </goals>
        </execution>
    </executions>
</plugin>

务必请编辑该file根据您的实际文件路径(建议在路径是将一些文件夹内的这些外部非Maven的罐子,让我们说lib ,并把这个lib文件夹的项目中,以使用特定项目的相对路径和避免添加系统特定的绝对路径。

如果您有多个外部 jar,只需对同一maven-install-plugin其他 jar 重复<execution>

第 2 步:一旦您在pom.xml文件中配置了如上所示的maven-install-plugin ,您必须像往常一样在pom.xml使用这些 jar:

    <dependency>
        <groupId>com.amazonservices.mws</groupId>
        <artifactId>mws-client</artifactId>
        <version>1.0</version>
    </dependency>

请注意, maven-install-plugin只会将您的外部 jar 复制到您本地的.m2 maven 存储库。 而已。 它不会自动将这些 jar 作为 Maven 依赖项包含到您的项目中。

这是一个小点,但有时很容易错过。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM