IntelliJ无法使用Maven解析符号第三方jar [英] IntelliJ cannot resolve symbol 3rd party jar with Maven

查看:118
本文介绍了IntelliJ无法使用Maven解析符号第三方jar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有第三方JAR,我通过 mvn install:install-file 命令创建了一个工件。工件被添加到项目中,模块的 pom.xml 并导入就好了。

I have a 3rd party JAR, which I've created an artifact for via the mvn install:install-file command. The artifact is added to the project, and the module's pom.xml and imported just fine.

创建Java带有来自第三方JAR的符号的类,遗憾的是仍未得到解决。红灯灯泡显示,并且没有像我期望的那样选择导入类。

Creating a Java class with a symbol from the 3rd party JAR, remains unresolved unfortunately. The red light bulb displays, and there's no option for "Import Class" as I would expect.

我试图使缓存无效重启但无济于事。

I've tried to Invalidate Caches | Restart but to no avail.

任何想法?

编辑如果我改为安装已编译的JAR,解析符号。为什么会这样?

Edit if I instead install the compiled JAR, the symbol is resolved. Why is this so?

编辑2 添加显示内部共享工件的pom.xml。

Edit 2 adding pom.xml displaying the internally shared artifact.

<?xml version="1.0" encoding="UTF-8"?>
<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>
    <packaging>pom</packaging>
    <name>Sandbox</name>
    <parent>
        <groupId>com.x.y</groupId>
        <artifactId>third-party-parent</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <groupId>com.x.y.z</groupId>
    <artifactId>thirdparty-z</artifactId>
    <version>1.0-SNAPSHOT</version>

     <dependencies>
         <dependency>
             <groupId>com.x.y.thirdparty</groupId>
             <artifactId>thirdparty-sources</artifactId>
             <version>1.0</version>
         </dependency>

         <dependency>
             <groupId>com.x.y.thirdparty</groupId>
             <artifactId>thirdparty-compiled</artifactId>
             <version>1.0</version>
         </dependency>
     </dependencies>

</project>


推荐答案

此Maven项目文件 pom .xml 包含修复内容,以及有关更改内容及其更改原因的一些提示/提示。

This Maven project file pom.xml contains the fixes, and some tips / hints about what is changed and why it has been changed.

<?xml version="1.0" encoding="UTF-8"?>
<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>

    <!--  Parent POM best at the top  -->
    <!--  WANRING: Here you import the parent of 3th party. This is (almost) never correct!!!  -->
    <!--  For a simple project, which only exists of 1 set of classes and unit tests, you do   -->
    <!--  not need a parent POM. Only for multi module Maven project, you will have a parent   -->
    <!--  POM. Or for organisation wide configuration, of repositories, Maven plugin           -->
    <!--  configuration, and so. When starting with a small project, leave out the parent POM  -->
    <!--  reference.                                                                           -->
    <parent>
        <groupId>com.x.y</groupId>
        <artifactId>third-party-parent</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <!--  Better keep all project stuff together  -->
    <groupId>com.x.y.z</groupId>
    <artifactId>your-project-name</artifactId>
    <packaging>pom</packaging>
    <name>My Little Sandbox</name>
    <version>1.0.0-SNAPSHOT</version>

    <dependencies>
        <!--  Here only a reference to the compiled jar is required!!!                   -->
        <!--  If a source jar is available, it will get pulled in automatically!!!       -->
        <!--  As this is done by (naming) convention of all jars, in a Maven repository  -->
        <dependency>
            <groupId>com.x.y.thirdparty</groupId>
            <artifactId>thirdparty</artifactId>
            <version>1.0</version>
        </dependency>
    </dependencies>

</project>

备注:新项目延伸第3方父母的罕见情况POM,是新项目是第三方项目的某种扩展/插件。然后,它可以使用相同的Maven插件版本,dependecies等,如第3方使用。新的 Extention / Plug-in 与第三方软件的其他部分配合得非常好。

Remark: The rare case where a new project extends a 3th party parent POM, is when the new project is some kind of extention / plug-in of the 3th party project. It then can use the same Maven Plug-in versions, dependecies, etc, etc, as the 3th party is using. Which makes it most lickly that the new Extention / Plug-in works well with the other parts, of the 3th party software.

尝试的具体示例

<?xml version="1.0" encoding="UTF-8"?>
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>org.organisation.play-ground</groupId>
    <artifactId>my-little-sandbox</artifactId>
    <packaging>pom</packaging>
    <name>My Little Sandbox</name>
    <version>1.0.0-SNAPSHOT</version>

    <dependencies>
        <!--  A dependency reuired for each phase  -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.12</version>
        </dependency>

        <!--  A dependency only needed during runtime phase  -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.1.3</version>
            <scope>runtime</scope>
        </dependency>

        <!--  A dependency only needed during compile and test phase  -->
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.9.8</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

这篇关于IntelliJ无法使用Maven解析符号第三方jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆