sbtで実行可能なFatJarを生成後、confファイルとZip化してNexusに上げるメモ


Nexusサーバーに上げる方法に苦労したので、メモしておきます。

## やりたいこと

- FatJarを作りたい (<- sbt-assembly)
- confファイルを加えてZipにしたい (<- sbt-native-packager)
- ZipをNexusに登録したい (<- sbt-release)

## 概略

- `sbt-assembly`, `sbt-native_packager`, `sbt-release`を使う
- `sbtAssemblyOutput`でjarの名前を変更した (option)
- `sharedSettings`中にNexusへのpublishに必要な情報を記述　
- `publishZipSettings`にzip化に必要な情報を記述


## はまった点
- Build.scalaではsbt-native-packagerの設定がうまく読み込めなかった.. (`dist` が見つからない..)
- root projectに対しても `enablePlugins(JavaAppPackaging)` しないとだめ

## Files

```project/plugins.sbt
resolvers += "sonatype" at "https://oss.sonatype.org/content/groups/public"

addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.1")

addSbtPlugin("com.github.gseitz" % "sbt-release" % "1.0.0")

addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.0.4")
```

```build.sbt
val libOrganization = "com.anonymous"
// use version.sbt
// val libVersion = ""

val sharedSettings = Seq(
  // version := libVersion,
  organization := libOrganization,
  crossScalaVersions := Seq("2.11.7"),
  scalacOptions := Seq("-deprecation", "-encoding", "utf8"),
  scalaVersion := "2.11.7",
  shellPrompt := { s => Project.extract(s).currentProject.id + " > " },
  skip in update := false,
  crossPaths := false,
  publishMavenStyle := true,
  publishArtifact in Test := false,
  pomIncludeRepository := { _ => false },
  publishArtifact in (Compile, packageDoc) := false,
  publishArtifact in (Compile, packageSrc) := false,
  publishTo <<= (version) apply { v: String =>
    val anonymous_base = "http://nexus.anonymous/nexus/content/repositories/"
    if (v.trim.endsWith("SNAPSHOT"))
      Some("snapshots" at anonymous_base + "snapshots")
    else
      Some("releases"  at anonymous_base + "anonymous")
  },
  credentials += Credentials(Path.userHome / ".ivy2" / ".credentials"),
  resolvers ++= Seq(
    Resolver.mavenLocal,
    // without this Resolvers, it will conflict with anonymous repos.
    Resolver.sonatypeRepo("releases"),
    Resolver.sonatypeRepo("snapshots"),
    "anonymous" at "http://nexus.anonymous/nexus/content/repositories/anonymous",
    "Twitter Repository" at "https://maven.twttr.com"),
  libraryDependencies ++= Seq(),
  testOptions in Test += Tests.Argument(TestFrameworks.ScalaTest, "-oDS")
)

lazy val sbtAssemblyOutput = Seq(
  assemblyJarName in assembly <<= (name, version) map { (name, version) =>
    name + ".jar"
  }
)

// aggregate fatjar by sbt-assembly and conf files to zip.
val publishDist = TaskKey[sbt.File]("publish-dist", "publish the dist artifact")
lazy val publishZipSettings = Seq(
  publish <<= (publish) dependsOn dist,
  publishLocal <<= (publishLocal) dependsOn dist,
  artifact in publishDist ~= {
    (art: Artifact) => art.copy(`type` = "zip", extension = "zip")
  },
  // first, removes all in mappings and adds fat jar.
  mappings in Universal := {
    val universalMappings = (mappings in Universal).value
    val fatJar = (assembly in Compile).value
    val filtered = universalMappings filter {
        case (file, name) =>  ! name.endsWith(".jar")
    }
    filtered :+ (fatJar -> ("lib/" + fatJar.getName))
  },
  mappings in Universal += file("some.conf") -> "conf/some.conf",
  publishDist <<= (target in Universal, normalizedName, version) map { (targetDir, id, version) =>
    val packageName = "%s-%s" format(id, version)
    targetDir / (packageName + ".zip")
  }
)

lazy val hello = (project in file("hello"))
.settings( name := "hello")
.enablePlugins(JavaAppPackaging)
.settings( sharedSettings: _*)
.settings( publishZipSettings: _*)
.settings( sbtAssemblyOutput: _*)
.settings( mainClass in assembly := Some("Hello"))
.settings( addArtifact(artifact in publishDist, publishDist) )

lazy val root = (project in file("."))
.settings( name := "test")
.enablePlugins(JavaAppPackaging)
.settings(sharedSettings: _*)
.settings( aggregate in update := false)
.settings( isSnapshot := true)
.aggregate(hello)
```

```version.sbt
version in ThisBuild := "0.0.1-SNAPSHOT" 
```

```shell
$ sbt release
```


## 参考にしたサイト
- <a href='http://stackoverflow.com/questions/23070469/how-do-i-publish-a-fat-jar-jar-with-dependencies-using-sbt-and-sbt-release'>deployment - How do I publish a fat JAR (JAR with dependencies) using sbt and sbt-release? - Stack Overflow</a>
- <a href='http://stackoverflow.com/questions/26452140/how-to-publish-plays-dist-zip-file-to-remote-maven-repository'>playframework - How to publish Play's "dist" zip file to remote Maven repository? - Stack Overflow</a>
- <a href='http://www.scala-sbt.org/sbt-native-packager/formats/universal.html'>Universal Plugin — sbt-native-packager 1.1.0-SNAPSHOT documentation</a>