简体   繁体   English

包中包含具有相同名称的对象和包

[英]Package contains object and package with same name

I am having problems compiling some Scala with Maven or Eclipse where I try to import a class from a Java jar which contains both a namespace and class of the same name. 我在使用Maven或Eclipse编译一些Scala时遇到问题,我尝试从包含命名空间和同名类的Java jar导入类。
I can compile with scalac , however. 但是我可以用scalac编译。

Eg the Java project (jar) contains: 例如,Java项目(jar)包含:

src/foo/bar.java

src/foo/bar/some_resource.txt

-> foobar.jar

Scala project references foobar.jar

Foobartest.scala:

import foo.bar

class foobartest {

}

The compiler complains with: 编译器抱怨:

package foo contains object and package with same name: bar 
one of them needs to be removed from classpath

Using Maven 3.0.03/Eclipse 3.7.1 with Scala 2.9.0.1 (and maven-scala-plugin ). 使用Maven 3.0.03 / Eclipse 3.7.1和Scala 2.9.0.1(和maven-scala-plugin )。

The jar which I am having problems with is jenkins-core-1.399.jar - it definitely contains several instances where there is a namespace and object of the same name. 我遇到问题的jar是jenkins-core-1.399.jar - 它肯定包含几个有名称空间和同名对象的实例。
I am attempting to write a Jenkins plugin in Scala (I could do this in Java but would prefer scala since all of our libraries are in scala), which is dependent on using Maven - 我试图在Scala中编写一个Jenkins插件(我可以用Java做这个,但是我更喜欢scala,因为我们所有的库都在scala中),这依赖于使用Maven -
https://wiki.jenkins-ci.org/display/JENKINS/Plugin+tutorial . https://wiki.jenkins-ci.org/display/JENKINS/Plugin+tutorial

That kind of limitation was outlined in SI-4695: package object misbehaves in the presence of classfiles . SI-4695中概述了这种限制:包类对象在类文件存在的情况下行为不端

As suggested in SI-2089 (naming restriction makes some jars unusable) , you could try and use the " resolve-term-conflict ", as implemented in changeset 25145 : 正如SI-2089中所建议的那样(命名限制使得某些jar无法使用) ,您可以尝试使用“ resolve-term-conflict ”,如changeset 25145中所实现的:

Added a -Y option to resolve namespace collisions between package and object. 添加了-Y选项以解决包和对象之间的命名空间冲突。
It's a blunt instrument: if people have lots of these conflicts they need to resolve in individually nuanced fashion, they'll probably remain out of luck. 这是一个钝器:如果人们有很多这些冲突,他们需要以个别细致的方式解决,他们可能仍然没有运气。

val termConflict  = ChoiceSetting     ("-Yresolve-term-conflict", "strategy", "Resolve term conflicts", 113 List("package", "object", "error"), "error")

// Some jars (often, obfuscated ones) include a package and
// object with the same name. Rather than render them unusable,
// offer a setting to resolve the conflict one way or the other.
// This was motivated by the desire to use YourKit probes, which
// require `yjp.jar` at runtime. See SI-2089.

实际的编译器选项是“-Yresolve-term-conflict: strategy ”,其中策略是包,对象,错误。

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

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