【问题标题】:IntelliJ IDEA: how to put maven dependency into correct location of builded artifactIntelliJ IDEA:如何将 maven 依赖项放入已构建工件的正确位置
【发布时间】:2016-11-26 08:47:38
【问题描述】:

我在 IntelliJ IDEA 2016.1.3 中有 java 项目,我正在构建 jar 文件(构建 -> 构建工件... -> 构建),在我想使用外部库(org.apache. commons:commons-lang3:3.4),没什么大不了的,它在 IDE 中工作。但是提取的 jar 需要该依赖项。 该怎么做?

我已经尝试过项目结构 -> 项目设置 -> 工件 -> 并添加为库。

看jar神器:

想要的依赖 jar 位于 jar 工件的根文件夹中,但尝试使用该代码时仍然出现此错误。

Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils
....
....
Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang3.StringUtils
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 37 more

我错过了什么?

【问题讨论】:

  • 看起来你想要一个“胖罐子”,罐子里面有类路径罐子。这通常需要额外的处理;我使用 Spring Boot,Spring Boot Maven 插件就是这样做的。还有一些其他非 Spring 特定的重新包装器。
  • 谢谢@chrylis,我希望intelliJ 为我做这件事:( 最后,我很着急,我只需要一个方法countMatches
  • private int countMatches(@NotNull String source, @NotNull String pattern) { if (!source.isEmpty() && !pattern.isEmpty()) { int count = 0; for (int idx = 0; (idx = source.indexOf(pattern, idx)) != -1; idx += pattern.length()) { count++; } return count; } else { return 0; } }

标签: java maven intellij-idea jar artifacts


【解决方案1】:

正如我在上面的评论中提到的,我使用了我的自定义实现而不是使用整个库。

但在另一个项目中,我刚刚遇到了同样的问题,这次是 nanohttpd,这当然不像以前的用法那样容易实现:D

我发现artifact/maven依赖的创建顺序很重要 因为我像以前一样首先添加了依赖项,但在为工件创建配置之前。

瞧,依赖项已被提取,最终的工件运行良好:)

【讨论】:

    猜你喜欢
    • 2019-09-22
    • 2015-07-15
    • 1970-01-01
    • 2012-07-12
    • 2018-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多