简体   繁体   English

Maven 项目中的非 Maven 依赖项

[英]Non-Maven dependecies in a Maven project

How can I add an external jar file as a dependency for a Maven project in IntelliJ IDEA?如何在 IntelliJ IDEA 中添加外部 jar 文件作为 Maven 项目的依赖项? Because when I add it in the dependency list and try to compile with Maven, I got an error that that dependency couldn't be found.因为当我将其添加到依赖项列表中并尝试使用 Maven 进行编译时,我收到了找不到该依赖项的错误。

Ideally you should deploy the JAR to your repository using mvn deploy:deploy-file .理想情况下,您应该使用mvn deploy:deploy-file将 JAR 部署到您的存储库。

If that's not possible, you can set the dependencies scope to system and then include a systemPath in the dependency which gives that path to the jar.如果这是不可能的,您可以将依赖项scope设置为system ,然后在依赖项中包含一个systemPath ,该依赖项为 jar 提供该路径。 This is explained in POM Reference - dependencies and comes with a warning that any artifact that depends on the artifact with the system scope dependency will also expect to find the jar via the systemPath .这在POM 参考 - 依赖项中进行了解释,并带有警告,即任何依赖于具有system范围依赖项的工件的工件也将期望通过systemPath找到 jar。

You can either你可以

  • define a system/local dependency like this:像这样定义系统/本地依赖项:

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

As Gimby pointed out, be aware that system dependencies are expected to 'just be there', so they will not be packaged and deployed with your artifact.正如 Gimby 指出的那样,请注意系统依赖项应该“就在那里”,因此它们不会与您的工件一起打包和部署。 See this question for reference.请参阅此问题以供参考。

  • install the artifact into your local repo:将工件安装到您的本地存储库中:

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

Step 1: Configure the maven-install-plugin with the goal install-file in your pom.xml步骤 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>

Make sure to edit the file path based on your actual file path (recommended is to place these external non-maven jars inside some folder, let's say lib , and place this lib folder inside your project so as to use project-specific relative path and avoid adding system specific absolute path.务必请编辑该file根据您的实际文件路径(建议在路径是将一些文件夹内的这些外部非Maven的罐子,让我们说lib ,并把这个lib文件夹的项目中,以使用特定项目的相对路径和避免添加系统特定的绝对路径。

If you have multiple external jars, just repeat the <execution> for other jars within the same maven-install-plugin .如果您有多个外部 jar,只需对同一maven-install-plugin其他 jar 重复<execution>

Step 2: Once you have configured the maven-install-plugin as shown above in your pom.xml file, you have to use these jars in your pom.xml as usual:第 2 步:一旦您在pom.xml文件中配置了如上所示的maven-install-plugin ,您必须像往常一样在pom.xml使用这些 jar:

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

Note that the maven-install-plugin only copies your external jars to your local .m2 maven repository.请注意, maven-install-plugin只会将您的外部 jar 复制到您本地的.m2 maven 存储库。 That's it.而已。 It doesn't automatically include these jars as maven dependencies to your project.它不会自动将这些 jar 作为 Maven 依赖项包含到您的项目中。

It's a minor point, but sometimes easy to miss.这是一个小点,但有时很容易错过。

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

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