Package contains object and package with same name

心已入冬 提交于 2019-12-19 00:17:04

问题


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.
I can compile with scalac, however.

E.g. the Java project (jar) contains:

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).

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.
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 -
https://wiki.jenkins-ci.org/display/JENKINS/Plugin+tutorial.


回答1:


That kind of limitation was outlined in SI-4695: package object misbehaves in the presence of classfiles.

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:

Added a -Y option to resolve namespace collisions between package and object.
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.



回答2:


The actual compiler option is "-Yresolve-term-conflict:strategy" where strategy is either package, object, error.



来源:https://stackoverflow.com/questions/8984730/package-contains-object-and-package-with-same-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!