简体   繁体   English

maven:从JDK中排除jar

[英]maven: exclude jar from JDK

So, I've been searching SO for hours and hours and I'm completely running into the brick wall here. 所以,我一直在寻找数小时和数小时,我在这里完全碰到了砖墙。

My problem's quite simply: I've got a (pretty big) project that I'd like being built with Maven (so I can automate it all a bit). 我的问题很简单:我有一个(非常大的)项目,我想用Maven构建(所以我可以自动完成它)。 Everything works fine so far except for one major problem. 到目前为止一切正常,除了一个主要问题。

I've got a dependency called "java-plugin" - I don't know exactly the origin or the author, but it was in the dependencies of a dependency of mine - I added it to our own Nexus third party repository with the name the original jar was given. 我有一个名为“java-plugin”的依赖项 - 我不确切地知道它的来源或作者,但它是我的依赖项的依赖项 - 我将它添加到我们自己的Nexus第三方存储库中的名称给了原来的罐子。

This plugin gets added from my Nexus without any problems, but it has the following structure: 这个插件从我的Nexus中添加没有任何问题,但它具有以下结构:

- netscape
    -- javascript
        JSException.class
        JSObject.class
        JSUtil.class
    -- security
        ForbiddenTargetException.class
        ParameterizedTarget.class
        Principal.class
        Privilege.class
        PrivilegeManager.class
        PrivilegeTable.class
        Target.class
        UserDialogHelper.class
        UserTarget.class
- sun
    -- plugin
        ...
    -- plugin2
        ...
- com.sun.java.browser.plugin2
    ...

What's the problem? 有什么问题? As long as I was working in an Eclipse based project, I placed my JDK as the "last" one on my classpath. 只要我在基于Eclipse的项目中工作,我就将JDK作为我的类路径中的“最后一个”。 Now it's Maven and apparently Maven places the JDK first. 现在它是Maven,显然Maven将JDK放在第一位。 In my JDK I have the jfxrt.jar (part of Java FX). 在我的JDK中,我有jfxrt.jar (Java FX的一部分)。 This one also contains a netscape.javascript.JSObject object (and also a netscape.javascript.JSException object). 这个包含一个netscape.javascript.JSObject对象(以及一个netscape.javascript.JSException对象)。 It doesn't contains a netscape.javascript.JSUtil object on the other hand. 另一方面,它不包含netscape.javascript.JSUtil对象。 So Maven picks up the JSObject and the JSException from the JDK library, and the other classes he picks up from my own java-plugin dependency. 因此,Maven从JDK库中获取JSObjectJSException ,以及从我自己的java-plugin依赖项中获取的其他类。

Of course both classes are not the same. 当然两个班级都不一样。 Of course now I get compilation errors as the java-plugin dependency contains a 'getWindow' method in the JSObject class where the JDK library doesn't. 当然现在我得到了编译错误,因为java-plugin依赖包含JSObject类中的'getWindow'方法,而JDK库却没有。

The ideal situation would be to exclude jfxrt.jar from Maven, but I have absolutely no idea on how to do that. 理想的情况是从Maven中排除jfxrt.jar ,但我完全不知道如何做到这一点。 Any other solution would do too, as long as I could get this one to build with Maven. 任何其他解决方案也会这样做,只要我能用Maven构建这个解决方案。 Note: I'd rather not want to use the "endorse" mechanism in Java if possible, as that would require uploading this library to several different servers over and over again and would cause huge delays in deployment (as we'd always have to sent the file to our support team for yet another upload). 注意:如果可能的话,我宁愿不想在Java中使用“认可”机制,因为这需要一遍又一遍地将这个库上传到几个不同的服务器,并且会导致部署的大量延迟(因为我们总是需要将文件发送给我们的支持团队进行另一次上传)。

Thanks! 谢谢!


EDIT 编辑

So, my plugin dependency is something which is also in the JDK - even better! 所以,我的插件依赖是JDK中的东西 - 甚至更好! I don't need my java-plugin, I've got enough with my JDK which includes plugin.jar automatically (it's in my ${java.home}/lib/plugin.jar ). 我不需要我的java-plugin,我已经用我的JDK自动包含了plugin.jar(它在我的${java.home}/lib/plugin.jar )。

Now I have this situation: 现在我有这种情况:

Maven中的依赖问题

As you can see, the jfxrt.jar comes first, before the plugin.jar. 如您所见,jfxrt.jar首先出现在plugin.jar之前。 I can see why Maven, or Java more generally, stops looking as soon as it finds the first netscape.javascript.JSObject (which is in jfxrt.jar). 我可以看到为什么Maven,或者更普遍的Java,一旦找到第一个netscape.javascript.JSObject (在jfxrt.jar中)就停止查找。 But I really need it to load the second JSObject class (which unfortunately happens to be in the same package and with the same name). 但是我真的需要它来加载第二个JSObject类(不幸的是它恰好在同一个包中并且具有相同的名称)。 How the hell am I supposed to do this? 我该怎么办呢? And why did this work without a charm in Eclipse, when I wasn't using Maven, and why doesn't it work in IntelliJ, together with Maven? 当我没有使用Maven时,为什么这在Eclipse中没有魅力,为什么它不能与Intelliven一起使用?

Thanks in advance! 提前致谢!

Ok, so I figured it out. 好的,所以我明白了。 Apparently this was much more of an IntelliJ issue than it was of a classpath issue. 显然,这更像是一个IntelliJ问题,而不是类路径问题。

So IntelliJ automatically adds the full JDK (including all jars in /jre/lib) to the classpath - first, before all the Maven dependencies. 因此,IntelliJ会自动将完整的JDK(包括/ jre / lib中的所有jar)添加到类路径中 - 首先,在所有Maven依赖项之前。 So that caused my project to be going weird: I had a netscape.javascript.JSObject in jfxrt.jar , in plugin.jar and in my Maven plugin (these jars were added in this order). 所以这导致我的项目变得奇怪:我在jfxrt.jarplugin.jar和我的Maven插件中有一个netscape.javascript.JSObject (这些jar按此顺序添加)。 The first JSObject that was found was the one in jfxrt.jar , which caused the problem. 找到的第一个JSObject是jfxrt.jar中的jfxrt.jar ,导致了这个问题。

It worked in Eclipse as there, I could alter the classpath order and had added my Maven plugin BEFORE the JDK - so the order became java-plugin.jar (first one, correct JSObject class), jfxrt.jar , plugin.jar . 它在Eclipse中工作,我可以改变类路径顺序,并在JDK之前添加了我的Maven插件 - 所以顺序变成了java-plugin.jar (第一个,正确的JSObject类), jfxrt.jarplugin.jar

It would have worked when I'd use an older JDK - this jfxrt.jar was only added in JDK7. 当我使用较旧的JDK时它会工作 - 这个jfxrt.jar只在JDK7中添加。

It works on Jenkins, if I still have my java-plugin.jar from Maven, because Jenkins does not automatically add the JDK libraries (only the core). 它适用于Jenkins,如果我还有来自Maven的java-plugin.jar ,因为Jenkins不会自动添加JDK库(只有核心)。 That was how I cleared it out: 这就是我清除它的方式:

  • I removed my own java.plugin.jar and replaced it by a dependency on plugin.jar . 我删除了自己的java.plugin.jar ,并将其替换为对plugin.jar的依赖。

     <dependency> <groupId>com.sun.jdk</groupId> <artifactId>plugin</artifactId> <version>${version.java-plugin}</version> <type>jar</type> <scope>system</scope> <systemPath>${java.home}/lib/plugin.jar</systemPath> </dependency> 
  • I still had the problem in IntelliJ, but arrived to remove the jfxrt.jar from the imported JDK libraries in IntelliJ itself - which does change the .iml file but of course not anything that would be of use for Jenkins. 我仍然遇到IntelliJ中的问题,但是从IntelliJ本身导入的JDK库中删除了jfxrt.jar - 这确实改变了.iml文件,但当然不是任何可用于Jenkins的东西。 (You can edit these settings by right clicking on the External Libraries > < 1.7> folder in the Project Browser, and then clicking Open Library Settings ). (您可以通过右键单击项目浏览器中的外部库> <1.7>文件夹,然后单击Open Library Settings来编辑这些设置。

  • Once jfxrt.jar was removed, everything worked. 删除jfxrt.jar后,一切正常。 I reasoned that this wouldn't change anything in Jenkins, but on the other hand, then the problem maybe wouldn't even exist on Jenkins. 我推断这不会改变詹金斯的任何东西,但另一方面,詹金斯甚至可能不存在问题。 So I simply tried and commited my code to SVN to build it on Jenkins. 所以我只是尝试将我的代码提交给SVN,以便在Jenkins上构建它。

Magically, it works now on Jenkins too. 可悲的是,它现在也适用于詹金斯。 I tried and removed my dependency on plugin.jar , then I get the classical "could not find symbol" compilation error on Jenkins - while I don't get that error in IntelliJ, due to the automatically importing of the JDK on the classpath. 我尝试并删除了对plugin.jar依赖,然后我在Jenkins上得到了经典的“找不到符号”编译错误 - 而我在IntelliJ中没有得到错误,因为在类路径上自动导入了JDK。

Long story short: By editing my project settings in IntelliJ I got it working locally, by simply trusting Maven I got it working on Jenkins, the way it should. 简而言之:通过在IntelliJ中编辑我的项目设置,我让它在本地工作,只需信任Maven,我就让它以Jenkins的方式工作。

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

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