Choose one dependency as precedent over another for an arbitrary list of packages in the IntelliJ Builder

huangapple go评论71阅读模式
英文:

Choose one dependency as precedent over another for an arbitrary list of packages in the IntelliJ Builder

问题

目标

我试图让我的Maven项目在编辑代码时使用IntelliJ构建器,因为该IDE提供了快速重建和热部署功能,可以加快我的工作速度。然而,由于我的Maven项目的设置方式,我遇到了障碍。

约束条件

我必须按照Maven的使用方式来操作。解决方案必须在IntelliJ中实现。

请注意,创建一个IntelliJ插件是一个选项。我意识到由于IntelliJ构建配置的限制,这可能是必要的。

问题

我的项目依赖于两个库,SharedLibASharedLibB,它们都包含com.shared包。SharedLibA是一个新的库,而SharedLibB是一个旧的遗留库。SharedLibASharedLibB包含的包在彼此中都没有,但是我的项目需要这些包。它们都有的包,com.shared,项目也需要。然而,SharedLibB是遗留/旧的,因此我们总是希望Tomcat优先使用SharedLibA中com.shared的类的实现,而不是SharedLibB中这些类的实现。我们通过一个Maven目标来实现这一点,该目标在打包WAR时将com.shared包下的类从SharedLibA提取到WEB-INF/classes目录下。

当我尝试在IntelliJ中构建我的项目时,它选择了com.shared.*中的错误实现,并且尽管通过Maven编译完全正常,却输出了编译错误。我被这种不灵活的Maven“hackjob”所困扰,但我仍然希望找到一种方法,让IntelliJ理解我希望在编译时使用SharedLibA的com.shared.*类,而不是SharedLibB的实现。我不能从考虑中移除SharedLibB,因为我需要SharedLibB中的其他类来编译我的项目。

期望的解决方案

我正在寻找一种类似于“当考虑com.shared.*类时,选择SharedLibA”或“当类路径解析中发生任何包冲突时,优先考虑SharedLibA”的逻辑,因为我相当确定在任何包冲突时都应该优先考虑SharedLibA

谢谢

我希望这解释得足够清楚,提前感谢任何参与讨论的人。

英文:

Goal

I'm trying to make my maven project use the IntelliJ builder when I'm editing code because the IDE offers fast rebuild and hot deployment features that could speed up my work. However I'm running into a roadblock due to how my maven project is set up.

Constraints

I have to use maven the way it's being used. The solution must be in IntelliJ

Note that creating an IntelliJ plugin is an option. I realize this may be necessary due to the limitations of IntelliJ build configuration.

The problem

My project depends on 2 libraries, SharedLibA and SharedLibB, which contain package com.shared. SharedLibA is a new library, and sharedLibB is and old legacy one. SharedLibA and SharedLibB contain packages that are not contained in the other, which are needed by my project. The package they both have, com.shared is needed by the project too. However SharedLibB is legacy/old so we always want Tomcat to prioritize the implementation of classes in com.shared from SharedLibA, not the implementations of those classes in SharedLibB. We accomplish this by having a maven goal that extracts classes under package com.shared from SharedLibA to WEB-INF/classes when the WAR is packaged.

When I try to build my project in IntelliJ, it picks up the wrong implementation of classes in com.shared.* and spews out compilation errors despite compilation working perfectly via maven. I'm stuck with this inflexible maven hackjob but I still want to find a way to make IntelliJ understand that I want SharedLibA's com.shared.* classes to be used in compilation not SharedLibB's. I can't remove SharedLibB from consideration because I need the other classes in SharedLibB to compile my project.

Desired Solution

I'm looking for some kind of logic like "when com.shared.* classes are considered, choose SharedLibA" or "when any package conflict happens in classpath resolution, prioritize ShareLibA" because I'm pretty sure SharedLibA should be prioritized in any package conflict.

Thanks

I hope that explains it well enough, thank you in advance to anyone who chimes in here.

答案1

得分: 1

IDE根据为模块配置的依赖项 构建 类路径和构建路径。它还考虑 它们的顺序 - 第一个匹配给定类的依赖项将被使用。

对于基于Maven的项目,IDE从模块的Maven pom.xml文件中获取依赖项列表和它们的顺序。因此,您可以在pom.xml文件中设置顺序。

另一个提到的选项是 排除库项目。但更好的解决方案是正确配置Maven构建。

英文:

IDE builds the classpath and buildpath based on the dependencies which are configured for the module. It also considers their order - the 1st dependency that match given class will be used.

For Maven-based projects the dependencies list and their order IDE takes from the Maven pom.xml file of the module. So you can set the order in pom.xml file.

Another mentioned option is to exclude the library items. But the better solution would be to configure Maven build properly.

答案2

得分: 0

你应该尝试访问项目中的 ".IML" 文件,在那里:
你可以创建或修改一个模块。

在这里,你可以包含或排除库,定义它们的使用优先级,
你可以使用以下片段作为启发:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="8">
  <component name="SharedB" LANGUAGE_LEVEL="JDK_1_8">
    <output url="file://$MODULE_DIR$/target/classes" />
    <output-test url="file://$MODULE_DIR$/target/test-classes" />
    <content url="file://$MODULE_DIR$">
      <sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
      ...
      <excludeFolder url="file://$MODULE_DIR$/target" />
    </content>
    ...
    <orderEntry type="inheritedJdk" />
    <orderEntry type="sourceFolder" forTests="false" />
    ...
  </component>
</module>

<!-- end snippet -->

如果你对这个解决方案感到不舒服,你可以使用Intellij的模块设置:

https://www.jetbrains.com/help/idea/library.html#excluded_lib_items

英文:

You should try to go to your project ".IML" file and there :
you can create or modify a module.

In there you can include or exclude the libs or defining its use priority
you could use this snippet to inspire

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule=&quot;true&quot; type=&quot;JAVA_MODULE&quot; version=&quot;8&quot;&gt;
  &lt;component name=&quot;SharedB&quot; LANGUAGE_LEVEL=&quot;JDK_1_8&quot;&gt;
    &lt;output url=&quot;file://$MODULE_DIR$/target/classes&quot; /&gt;
    &lt;output-test url=&quot;file://$MODULE_DIR$/target/test-classes&quot; /&gt;
    &lt;content url=&quot;file://$MODULE_DIR$&quot;&gt;
      &lt;sourceFolder url=&quot;file://$MODULE_DIR$/src/main/java&quot; isTestSource=&quot;false&quot; /&gt;
...
    &lt;excludeFolder url=&quot;file://$MODULE_DIR$/target&quot; /&gt;
    &lt;/content&gt;
...
    &lt;orderEntry type=&quot;inheritedJdk&quot; /&gt;
    &lt;orderEntry type=&quot;sourceFolder&quot; forTests=&quot;false&quot; /&gt;
...
  &lt;/component&gt;
&lt;/module&gt;

<!-- end snippet -->

if you are not confortable with this solution you could use the module settings of Intellij

https://www.jetbrains.com/help/idea/library.html#excluded_lib_items

huangapple
  • 本文由 发表于 2020年10月3日 02:29:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/64176602.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定