CLOVER🍀

That was when it all began.

sbtでJavaのプロジェクトを作る場合に、アーティファクト名からScalaのバージョンを省く

Twitterで流れているのを見て。

自分は、このブログを書いている時はJavaでもsbtを使うことが多いですが(たまにGradle)、そういえばアーティファクト名についてはあんまり気にしてなかったですね…。

だいたい、runして終了!ってパターンが多いからですか。

元ネタは、こちら。
http://stackoverflow.com/questions/8288859/how-do-you-remove-the-scala-version-postfix-from-artifacts-builtpublished-wi/8291387#8291387

何の話をしてるかって、例えば
build.sbt

name := "hello-world"

version := "0.0.1"

organization := "littlewings"

src/main/java/HelloWorld.java

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}

というJavaプロジェクトがあった時に、sbtでpackageを実行すると

> package
[info] Updating {file:/xxxxx/hello-world/}default-d3c92e...
[info] Resolving org.scala-lang#scala-library;2.9.2 ...
[info] Done updating.
[info] Compiling 1 Java source to /xxxxx/hello-world/target/scala-2.9.2/classes...
[info] Packaging /xxxxx/hello-world/target/scala-2.9.2/hello-world_2.9.2-0.0.1.jar ...
[info] Done packaging.
[success] Total time: 2 s, completed 2012/12/13 23:08:58

というように、

hello-world_2.9.2-0.0.1.jar

アーティファクト名にScalaバージョンが入ってしまうという話。

Scalaを使ってない時は、確かに省きたくなりますね…。

これ、artifactNameで指定するものだと思っていたのですが、crossPathsをfalseにすると同じ結果になるようです。

build.sbtをこう変えて…

name := "hello-world"

version := "0.0.1"

organization := "littlewings"

crossPaths := false

reloadして

> reload
[info] Set current project to hello-world (in build file:/xxxxx/hello-world/)

cleanしてpackage

> clean
[success] Total time: 0 s, completed 2012/12/13 23:11:10
> package
[info] Updating {file:/xxxxx/hello-world/}default-d3c92e...
[info] Resolving org.scala-lang#scala-library;2.9.2 ...
[info] Done updating.
[info] Compiling 1 Java source to /xxxxx/hello-world/target/classes...
[info] Packaging /home/ippei/study/xxxxx/hello-world/target/hello-world-0.0.1.jar ...
[info] Done packaging.
[success] Total time: 1 s, completed 2012/12/13 23:11:12

確かに、アーティファクト名からScalaバージョンが無くなっています…。

hello-world-0.0.1.jar

ここにも書いてましたね。全然気付いてませんでした。
http://www.scala-sbt.org/release/docs/Examples/Quick-Configuration-Examples.html

自分が思っていた設定は、こんな感じです。

name := "hello-world"

version := "0.0.1"

organization := "littlewings"

artifactName := { (sv, module, artifact) =>
  artifact.name + "-" + module.revision + "." + artifact.extension
}

これでも同じ結果になりますよ〜。でも、crossPathsの方が簡単に設定できるので、知っておくと便利かもですね。