【问题标题】:IntelliJ cannot resolve symbol 3rd party jar with MavenIntelliJ 无法使用 Maven 解析符号 3rd 方 jar
【发布时间】:2015-10-20 09:19:29
【问题描述】:

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

不幸的是,使用来自第 3 方 JAR 的符号创建 Java 类仍未解决。红色灯泡显示,并且没有我所期望的“导入类”选项。

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

有什么想法吗?

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

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

<?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>

【问题讨论】:

  • groupId、artifactId 和 version 是否正确? (我的意思是它们与您安装依赖项时使用的相同吗?)您是否在 IntelliJ 中更新了 Maven 项目?
  • 也许“Maven - 下载源”会有所帮助,但不确定
  • pom设置没问题,否则一开始就不会导入IntelliJ,对吧?我可以导航到图书馆并查看源代码。如果我尝试直接引用一个类,例如 com.x.y. 它不起作用。 IntelliJ 能够自动完成 com.x.y。但它不会在下拉列表中显示 ,尽管它存在于源 jar 中!。
  • 你试过 mvn idea:idea
  • @awsome 我不会以这种方式导入项目。我只将 pom.xml 引入 IntelliJ。

标签: java maven intellij-idea


【解决方案1】:

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

<?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 的罕见情况是,新项目是第 3 方项目的某种扩展/插件。然后它可以使用与第 3 方使用的相同的 Maven 插件版本、依赖项等。这使得新的扩展/插件与第三方软件的其他部分配合得很好。

具体例子试试

<?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>

【讨论】:

  • @hajder,这让你更进一步了吗?
猜你喜欢
  • 1970-01-01
  • 2014-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-07
  • 2018-01-10
  • 2016-10-12
相关资源
最近更新 更多