2012-01-24 88 views
8

我在編譯一些Scala時遇到了問題,用Maven或Eclipse嘗試從包含名稱空間和同名類的Java jar中導入類。
但是,我可以用scalac進行編譯。包中包含的對象和包的名字相同

E.g. 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 { 

} 

的編譯器抱怨:

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

使用Maven 3.0.03/3.7.1的Eclipse使用Scala 2.9.0.1(和maven-scala-plugin)。

我遇到問題的jar是jenkins-core-1.399.jar - 它肯定包含幾個實例,其中有一個名稱空間和對象名稱相同。
我正在嘗試在Scala中編寫一個Jenkins插件(我可以在Java中執行此操作,但由於我們所有的庫都在scala中,所以更喜歡scala),這取決於使用Maven-
https://wiki.jenkins-ci.org/display/JENKINS/Plugin+tutorial

+0

嘿,你有沒有得到一個用斯卡拉編寫的Jenkins插件?我正在嘗試相同 – KeepCalmAndCarryOn

+0

是的,它在我的github上。我試圖發佈它,但我沒有得到詹金斯人的大力支持。 https://github.com/Chemmo/scala_plugin – David

+0

我也寫了一個,並且確實發佈了它,但不能從插件管理器https://github.com/jenkinsci/hello-world-scala-plugin下載 – KeepCalmAndCarryOn

回答

5

這種限制概述於SI-4695: package object misbehaves in the presence of classfiles

正如SI-2089 (naming restriction makes some jars unusable)建議,你可以嘗試使用「resolve-term-conflict」,如changeset 25145實施:

增加了一個-Y選項來解決包裝和物體之間命名空間衝突。
這是一個直率的工具:如果人們有很多這些衝突,他們需要個別細微的方式來解決,他們可能會保持運氣不佳。

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

實際的編譯器選項是「-Yresolve長期衝突:戰略」,其中的戰略要麼包,對象錯誤。