简体   繁体   English

Maven package 工作,但 Intellij 的构建失败

[英]Maven package works but Intellij's build fails

I have a JDK 1.7 project with a maven dependency to a local jar in my maven repo.在我的 maven 存储库中,我有一个 JDK 1.7 项目,其中 maven 依赖于本地 jar。

I'm unable to build the project using Intellij, with the errors that a symbol cannot be found (the symbol is a class importing packages from the local jar)我无法使用 Intellij 构建项目,出现找不到符号的错误(该符号是 class 从本地 jar 导入包)

But I can successfully build the project using 'mvn package'.但我可以使用“mvn package”成功构建项目。

I've spent so much time looking for solutions, things I've already done:我花了很多时间寻找解决方案,我已经做过的事情:

  • Invalidated cache无效缓存
  • 'mvn clean install' the local jar dependecy 'mvn clean install' 本地 jar 依赖
  • Marked to 'autoload snapshots' in Intellij's maven configuration在 Intellij 的 maven 配置中标记为“自动加载快照”
  • Reimpoted maven in Intellij在 Intellij 中重新导入 maven
  • Syncornized同步化

This error happend to my couple of times before, but usually just closing the Intellij and /or doing the 'mvn clean install' did the trick.这个错误发生在我之前几次,但通常只是关闭 Intellij 和/或执行“mvn clean install”就可以了。

Please help.请帮忙。

I know this is late but executing mvn idea:idea solves the issue.我知道这已经晚了,但执行mvn idea:idea解决了这个问题。

The command re-generates the crucial IntelliJ IDEA files: ipr , iml , and iws , and thus re-align them with your pom.xml .该命令重新生成关键的 IntelliJ IDEA 文件: iprimliws ,从而将它们与您的pom.xml重新对齐。

Documentation: https://maven.apache.org/plugins/maven-idea-plugin文档: https ://maven.apache.org/plugins/maven-idea-plugin

The plugin is retired but still (as of 05.06.2021) works like magic :)该插件已退役,但仍然(截至 2021 年 6 月 5 日)像魔术一样工作 :)

Try to delete all .iml and your POM.尝试删除所有 .iml 和您的 POM。 Then copy and paste your POM again to your project and re-open and re-import it.然后将您的 POM 再次复制并粘贴到您的项目中,然后重新打开并重新导入它。

我发现我的 .iml 文件和 pom 存在冲突并导致了问题。

Maven ReImport worked for me like below... Maven ReImport为我工作,如下所示...

在此处输入图像描述

Adding some more tips since this is the first thing that came up in the search when I was having similar problems (project builds in mvn, not in IntelliJ), in case it might help someone else.添加更多提示,因为这是我遇到类似问题时搜索中出现的第一件事(项目构建在 mvn 中,而不是 IntelliJ 中),以防它可能对其他人有所帮助。

When I had this it was a different fix on Windows and Mac.当我有这个时,它在 Windows 和 Mac 上是一个不同的修复程序。

On Mac, I went into ItellijIDEA / Preferences / Build, Execution, Deployment / Build Tools / Maven / Maven Home Directory and set it to use my separately installed Maven rather than the bundled Maven.在 Mac 上,我进入 ItellijIDEA / Preferences / Build, Execution, Deployment / Build Tools / Maven / Maven Home Directory 并将其设置为使用我单独安装的 Maven 而不是捆绑的 Maven。 All the build problems went away.所有的构建问题都消失了。

On Windows, I was seeing exceptions in the idea.log related to SSL and my project tree had only Java under the External Libraries branch.在 Windows 上,我在 idea.log 中看到与 SSL 相关的异常,并且我的项目树在 External Libraries 分支下只有 Java。 From looking at other sites it sounds like IntelliJ uses its own private JDK instead of the system one (even though in my case I already had the exact same JDK version installed).从其他站点来看,听起来 IntelliJ 使用自己的私有 JDK 而不是系统的(即使在我的情况下,我已经安装了完全相同的 JDK 版本)。 My system JDK has certs installed in the keystore for the corporate Nexus server that the IntelliJ private JDK didn't have, so exceptions were being thrown when it tried to get files from Nexus.我的系统 JDK 在公司 Nexus 服务器的密钥库中安装了 IntelliJ 私有 JDK 没有的证书,因此当它尝试从 Nexus 获取文件时会引发异常。 For this the solution was to quit IntelliJ, set an environment variable called IDEA_JDK to point to the JDK I already had installed that had the proper certs and restart IntelliJ.为此,解决方案是退出 IntelliJ,设置一个名为 IDEA_JDK 的环境变量以指向我已经安装的具有正确证书的 JDK,然后重新启动 IntelliJ。 All the build problems went away.所有的构建问题都消失了。

IDE uses its own bundled copy of Maven when doing builds. IDE 在构建时使用它自己的 Maven 捆绑副本。 If maven on terminal is different from IDE, then you want them to be the same.如果终端上的 maven 与 IDE 不同,那么您希望它们相同。

GOTO (On a Mac)- IntelliJ Idea -> Preferences -> Build,Execution,Deployment -> Build Tools -> Maven. GOTO(在 Mac 上)- IntelliJ Idea -> 首选项 -> 构建、执行、部署 -> 构建工具 -> Maven。 Set the path for your Maven Home Directory To find the path for Maven which you use from terminal, type command mvn -version and that should print out bunch of details including the dir path.设置Maven 主目录的路径 要从终端查找使用的 Maven 路径,请键入命令mvn -version ,这应该会打印出包括 dir 路径在内的大量详细信息。 Put that in Maven Home Directory .把它放在Maven Home Directory中。

To be safe you can also do the same for JDK location为了安全起见,您也可以对 JDK 位置执行相同操作

==== Some Other Things that can cause such issues === ==== 可能导致此类问题的其他一些事情 ===

If doing mvn package or mvn clean install builds, then it has to do something with IDE caches.如果进行 mvn package 或 mvn clean install 构建,那么它必须对 IDE 缓存做一些事情。 Among other things, this one usually helps with such errors -->除其他外,这通常有助于解决此类错误 -->

File -> Invalidate Caches文件 -> 使缓存无效

I know I am pretty late to this conversation, but just wanted to post this in case someone else comes across this problem.我知道我在这次谈话中已经很晚了,但只是想发布这个以防其他人遇到这个问题。 Another reason why this happens is because you are not referring to the right repository.发生这种情况的另一个原因是您没有引用正确的存储库。 Intellij picks up the bundled version of a jar by default instead of the one from your own maven repository. Intellij 默认选择捆绑版本的 jar,而不是来自您自己的 maven 存储库的版本。 Confirm that the repository connected is the right one.确认连接的存储库是正确的。

从命令提示符执行命令: mvn idea:idea并右键单击项目-> 从菜单中重新导入依赖项

在我的情况下,我的 iml 不同步,所以我删除了特定模块的 iml,然后在 maven 选项下选择 unignore project 以重新创建 iml

This simple procedure takes about 1 minute without any risc.这个简单的过程大约需要 1 分钟,没有任何风险。

  • Close IntelliJ关闭 IntelliJ
  • Delete ONLY the IntelliJ specific files like .idea/*, *.iml, .settings, etc. Suggested was deleting the POM as well, but that is not necessary IMHO.删除 IntelliJ 特定文件,如 .idea/*、*.iml、.settings 等。建议也删除 POM,但恕我直言,这不是必需的。
  • Then re-create the project in IntelliJ using: File > New > Project from existing Sources > follow the steps.然后使用以下方法在 IntelliJ 中重新创建项目: File > New > Project from existing Sources > 按照步骤操作。 Be aware NOT to import multiple profiles or (same projects).请注意不要导入多个配置文件或(相同的项目)。

"symbol cannot be found" means that intellij is not able to resolve the "dependency". “找不到符号”意味着 intellij 无法解决“依赖关系”。 So, you can force "Dependency analysis" by clicking "Analyze" -> "Analyze Dependencies" then select the "Analysis Scope"因此,您可以通过单击“分析”->“分析依赖项”来强制“依赖项分析”,然后选择“分析范围”

Many valuable tips in the other answers here, just want to add the solution that worked for my problem .这里的其他答案中有许多有价值的提示,只是想添加对我的问题有效的解决方案。

We recently started using a private Maven repository and always do mvn -s settings.xml when invoking the Maven cmdline.我们最近开始使用私有 Maven 存储库,并且在调用 Maven cmdline 时总是执行mvn -s settings.xml IntelliJ does of course not know about the private repo, so it needs a little help. IntelliJ 当然不知道私有仓库,所以它需要一点帮助。

Go to Settings -> Build, Execution, Deployment -> Build tools -> Maven and override the "User Settings file" option.转到设置 -> 构建、执行、部署 -> 构建工具 -> Maven 并覆盖“用户设置文件”选项。

在此处输入图像描述

如果您使用的是 gradle ,请尝试以下命令。这解决了我的问题

gradle cleanIdea idea

I had the same problem.我有同样的问题。 Go to:去:

File -> Project Structure -> Module .文件 -> 项目结构 -> 模块 Select your module and under " Dependencies " tab, make sure that all the maven dependencies are added.选择您的模块并在“依赖项”选项卡下,确保添加了所有 Maven 依赖项。

To make sure you are warned about a similar issue in the idea editor: switch from "Dependencies tab" to "Sources" and add mark your src as source.为了确保您在创意编辑器中收到类似问题的警告:从“依赖项”选项卡切换到“源”并添加将您的 src 标记为源。

I had the similar problem and I realized my maven dependencies was not updated in intellij.我遇到了类似的问题,我意识到我的 Maven 依赖项没有在 intellij 中更新。 The solution was to Reload All Maven Projects in maven window which force intellij to reload maven dependencies.解决方案是在 maven 窗口中重新加载所有 Maven 项目,这会强制 intellij 重新加载 maven 依赖项。 在此处输入图像描述

I had a similar problem, where below options din't work.我遇到了类似的问题,以下选项不起作用。

  1. invalidating cache and restart through File menu通过文件菜单invalidating cache and restart
  2. mvn idea:idea

What resolved the issue:什么解决了这个问题:

  1. Close IntelliJ completely完全关闭 IntelliJ
  2. From terminal, execute git clean -xdf on the repository.从终端,在存储库上执行git clean -xdf This removes all generated classes and idea/iml/ipr files.这将删除所有生成的类和idea/iml/ipr 文件。 Other VCS should have similar macros.其他 VCS 应该有类似的宏。
  3. Run mvn clean package运行mvn clean package
  4. Open IntelliJ with idea .idea .

Key thing here was to close intellij, and do these steps from terminal.这里的关键是关闭 intellij,并从终端执行这些步骤。 Somewhere IntelliJ was still caching old files if trying from within IntelliJ.如果从 IntelliJ 中尝试,IntelliJ 仍在某个地方缓存旧文件。 This is with version IntelliJ IDEA 2021.3.2 (Ultimate Edition) .这是IntelliJ IDEA 2021.3.2 (Ultimate Edition) 版本

I had the same problem.我有同样的问题。 I've changed maven version from 3.0.5 to 3.0.4 in Idea settings and all works fine now.我在 Idea 设置中将 maven 版本从 3.0.5 更改为 3.0.4,现在一切正常。 Don't know why that helps me.不知道为什么这对我有帮助。

I tried invalidating cache, changing the Maven to installed version and nothing worked.我尝试使缓存无效,将 Maven 更改为已安装的版本,但没有任何效果。

Closing and recreating a new project resolved it.关闭并重新创建一个新项目解决了它。

我关闭了项目并通过单击根 pom(不仅仅是文件夹,而是 pom 文件本身)再次导入它。

I tried everything above with no joy.我没有高兴地尝试了上面的一切。 However I did one thing that finally got it to work:但是我做了一件事最终让它工作:

I went to C:\Users\.IntellijJIdea2018.2 and just deleted the entire folder.我去了 C:\Users\.IntellijJIdea2018.2 并删除了整个文件夹。

It removes all the settings and cache for IntelliJ.它会删除 IntelliJ 的所有设置和缓存。 I had to reimport my project from scratch but a simple remove and reimport hadn't worked before, but this finally got it working again.我不得不从头开始重新导入我的项目,但之前简单的删除和重新导入并没有奏效,但这最终让它再次工作。

Delete the .idea folder and close the IDE删除 .idea 文件夹并关闭 IDE

open the IDE again and import the project which will do a fresh import because .IDEA folder is deleted by us earlier再次打开 IDE 并导入将重新导入的项目,因为 .IDEA 文件夹已被我们之前删除

Check the external libraries module and if it is not populated with the required dependencies then check if the maven settings.xml location is given correctly in the IDE检查外部库模块,如果它没有填充所需的依赖项,则检查 IDE 中是否正确给出了 maven settings.xml 位置

There is also a possibility which was not mentioned.还有一种没有提到的可能性。 If you import library from com.sun.* .如果您从com.sun.*导入库。 It will also cause this problem.也会导致这个问题。

Programs works when run in ide, cause these classes in com.sun.* are all in jre/lib directory, it will be loaded when running.程序在 ide 中运行时有效,因为com.sun.*中的这些类都在jre/lib目录下,运行时会被加载。 But when you run mvn package , these libraries won't be accessible because of the restriction of oracle since java 1.6(these libraries was deprecated in a way).但是当您运行mvn package时,由于自 java 1.6 以来 oracle 的限制,这些库将无法访问(这些库在某种程度上已被弃用)。 To solve this problem, you can add要解决此问题,您可以添加

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                    <compilerArguments>
                        <verbose />
                        <bootclasspath>C:/Program Files/Java/jdk1.8.0_161/jre/lib/rt.jar</bootclasspath>
                    </compilerArguments>
                </configuration>
            </plugin>
        </plugins>
    </build>

remember to replace the cootclasspath with you own path of rt.jar记得用你自己的rt.jar路径替换 cootclasspath

You can search Access restriction: The type XXX is not accessible due to restriction on required library for more detail.您可以搜索Access restriction: The type XXX is not accessible due to restriction on required library获取更多详细信息。

If there are erroneous modules configured them do these steps:如果配置了错误的模块,请执行以下步骤:

  • Go to Project Settings转到项目设置
  • Go to Modules转到模块
  • Delete modules that should not be there删除不应该存在的模块
  1. From maven tab click + and choose pom.xml从 maven 选项卡单击 + 并选择 pom.xml
  2. From maven tab click download sources and documentation从 Maven 选项卡单击下载源和文档
  3. On project structure(where you can view project files/directories) right click the project you're trying to build and choose Build Module Project Name.在项目结构(您可以在其中查看项目文件/目录)上,右键单击您要构建的项目并选择构建模块项目名称。
  4. From tab Run- Edit Configurations with + add Application and fill the below fields: i.从选项卡运行 - 使用 + 添加应用程序编辑配置并填写以下字段: i. Main Class- Manually choose from Project tab select the main class ii.主类 - 从项目选项卡中手动选择主类 ii。 Use classpath of module - choose the application name iii.使用模块的类路径 - 选择应用程序名称 iii。 Shorten command line - classpath file缩短命令行 - 类路径文件
  5. Now simply run the app.现在只需运行该应用程序。

In my case the problem was in different Project Java SDK version.就我而言,问题出在不同的 Project Java SDK 版本中。 When I was building project from terminal, maven used Java 8, and when I was building from IDEA, it used Java 11.当我从终端构建项目时,maven 使用 Java 8,当我从 IDEA 构建时,它使用 Java 11。

Go to File > Project Structure... and in Project Settings set Project SDK to the version you need.转到 File > Project Structure... 并在 Project Settings 中将 Project SDK 设置为您需要的版本。

For me the problem was that I had Ajc compiler selected in the IntelliJ Java Compiler settings .对我来说,问题是我在IntelliJ Java Compiler settings中选择了Ajc 编译器。 Once I changed it back to Javac everything worked fine.一旦我将它改回Javac ,一切正常。

For me in Mac, I solved it by deleting the Intellij Idea Appication from my computer and deleting the cache files in ~/Library/Application Support/JetBrains/IntelliJIdea2020.1 ~/Library/Caches/JetBrains/IntelliJIdea2020.1对于 Mac 中的我,我通过从计算机中删除 Intellij Idea Appication 并删除 ~/Library/Application Support/JetBrains/IntelliJIdea2020.1 ~/Library/Caches/JetBrains/IntelliJIdea2020.1 中的缓存文件来解决它

Then re-install it, open my project, and build it successfully!然后重新安装,打开我的项目,构建成功!

I encountered the same problem.我遇到了同样的问题。 I can successfully run the mvn clean package command in terminal but fail to run or build from IntellJ.我可以在终端中成功运行mvn clean package命令,但无法从 IntellJ 运行或构建。 I did the following actions but they all failed.我做了以下动作,但都失败了。

  • Invalidated cache无效缓存
  • run mvn clean package or mvn clean package in terminal在终端中运行mvn clean packagemvn clean package
  • Marked to 'autoload snapshots' in Intellij's maven configuration在 Intellij 的 Maven 配置中标记为“自动加载快照”
  • Syncornize or Reimport同步或重新导入
  • Delete all the IntellJ related files like .idea and reimport project删除所有与 IntellJ 相关的文件,如 .idea 并重新导入项目
  • remove all the dependency files and then run mvn clean package删除所有依赖文件,然后运行mvn clean package
  • Switch Java version or use embedded Java in IntellJ在 IntellJ 中切换 Java 版本或使用嵌入式 Java
  • run mvn idea:idea运行mvn idea:idea
  • Change Maven Home更改 Maven 主页
  • mark src directory as source in the module tab在模块选项卡中将 src 目录标记为源

The last thing I did worked: Delete the .idea and .iml in the project, delete all the dependency files and then delete the IntellJ from the computer.我做的最后一件事是:删除项目中的 .idea 和 .iml,删除所有依赖文件,然后从计算机中删除 IntellJ。 After reinstalled the IntellJ my project built successfully.重新安装 IntellJ 后,我的项目构建成功。 I wish I could have a better way to solve this.我希望我能有更好的方法来解决这个问题。

Another reason for this error is usage of dependencies such as lombok in pom.xml file.此错误的另一个原因是在 pom.xml 文件中使用了依赖项,例如 lombok。 If you are using lombok annotations such as @Data in the java file, then you need to install lombok plugin in intellij to get rid of "Errors" in IDE.如果你在java文件中使用了@Data等lombok注解,那么你需要在intellij中安装lombok插件来摆脱IDE中的“错误”。

I also was getting the same problem.我也遇到了同样的问题。 From terminal using maven command (mvn clean install -DskipTests=true) project was built successfully.从终端使用 maven 命令 (mvn clean install -DskipTests=true) 项目已成功构建。 But in intellij it was unable to identify the jar spring library files.但在 intellij 中,它无法识别 jar spring 库文件。

Fixed it using the following steps:使用以下步骤修复它:

  1. close your IDE(intellij)关闭你的 IDE(intellij)
  2. delete sudo "rm -rf /<your_project_location>/.idea/" (/home/user/projects/test_proj/.idea/)删除 sudo "rm -rf /<your_project_location>/.idea/" (/home/user/projects/test_proj/.idea/)
  3. open your IDE again.再次打开你的IDE。 It will reindex your files and issue will be resolved它将重新索引您的文件,问题将得到解决

RCA: I suspect it happens due to permission issue in linux RCA:我怀疑这是由于 linux 中的权限问题而发生的

Such problem occurs when there is a mismatch between your system JVM/Maven configuration and JVM/Maven configuration of your IntelliJ IDE.当您的系统 JVM/Maven 配置与 IntelliJ IDE 的 JVM/Maven 配置不匹配时,就会出现此类问题。 I was facing the similar issue.我面临着类似的问题。 I was successfully able to rum "mvn clean install" command but IntelliJ Build was failing.我成功地能够朗姆酒"mvn clean install"命令,但 IntelliJ Build 失败了。

I also tried with "mvn idea:idea" command but it did not work because "idea" plugin is no longer exist.我也尝试使用"mvn idea:idea"命令,但它不起作用,因为"idea"插件不再存在。 It has been retired.它已经退役了。

Finally, I found a mismatch between my MAVEN_HOME path and IntelliJ default maven settings.最后,我发现我的MAVEN_HOME路径和 IntelliJ 默认的 maven 设置不匹配。 So, my problem got fixed after correcting the IntelliJ default maven settings.因此,在更正 IntelliJ 默认 maven 设置后,我的问题得到了解决。

You can find the settings on following location.您可以在以下位置找到设置。 Go to File -> Settings -> Build, Execution, Deployment in your IntelliJ ide.在 IntelliJ ide 中转到文件 -> 设置 -> 构建、执行、部署

在此处输入图像描述

Make sure that the path of "User Settings File" and "Local Repository" is same as the path of environment variable "MAVEN_HOME" or "M2_HOME" .确保“用户设置文件”“本地存储库”的路径与环境变量“MAVEN_HOME”“M2_HOME”的路径相同。

This seems to be issue with intellij in identifying your maven jars from local.Go to File->Project Structure->Libraries And check if any maven library showing in red (means its not identified by build ) This seems to be issue with intellij in identifying your maven jars from local.Go to File->Project Structure->Libraries And check if any maven library showing in red (means its not identified by build )

Now wherever you see in red remove them using below minutes (-) icon现在,无论您在哪里看到红色,请使用以下分钟 (-) 图标将其删除在此处输入图像描述

Now add them manually from your system using below plus(+) icon现在使用下面的加号(+)图标从您的系统手动添加它们在此处输入图像描述

Build now and it should work.立即构建,它应该可以工作。

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

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