%%ほのかちゃん%%

#contentsx(depth=1:2);

*参考あれこれ [#pe6519e1]

-[[Kotlin Programming Language>https://kotlinlang.org/]]
--[[Working with the Command Line Compiler>https://kotlinlang.org/docs/tutorials/command-line.html]]

-[[サーバーサイドKotlinの導入から一年が経ちました>https://blog.applibot.co.jp/2019/02/06/kotlin-one-year-anniversary/]] - てっくぼっと!
-[[なぜサーバーサイドKotlinを導入するのか?>https://blog.applibot.co.jp/2018/01/18/why-introducing-kotlin/]] - てっくぼっと!
-[[JavaプログラマのためのKotlin入門>https://qiita.com/koher/items/bcc58c01c6ff2ece658f]]
-[[JavaプログラマがKotlinでつまづきがちなところ>https://qiita.com/koher/items/d9411a00986f14683a3f]]
-[[JavaプログラマがKotlinで便利だと感じること>https://qiita.com/koher/items/cb91dbbff9b14575f498]]

-[[ラブライブ!で学ぶソフトウェア開発入門>http://learn-with-muse.sato-t.net/]] -> [[南ことりと星空凛のKotlin入門>http://learn-with-muse.sato-t.net/?page_id=5080]]

*てっとりばやくWindowsのコマンドラインで [#q65e1960]
**インストール [#o0216bf0]
+Kotlinのコンパイラをインストール(→ [[Working with the Command Line Compiler>https://kotlinlang.org/docs/tutorials/command-line.html]])
+未インストールなら [[Java実行環境>https://java.com/ja/download/]] も用意(Java 8でOK)

 PS W:\> kotlinc -version
 info: kotlinc-jvm 1.3.21 (JRE 1.8.0_201-b09)

わざわざ手元にインストールしないで [[Kotlin Playground>https://play.kotlinlang.org/]] を使わせてもらうのでもいいかも
**Hello [#y6421dfc]
#highlight(kt){{
fun main(args: Array<String>) {
  println("Hello, again my old dear place")
}
}}

 PS W:\> kotlinc .\hello.kt -include-runtime -d hello.jar
 PS W:\> java -jar hello.jar
 Hello, again my old dear place

**Nullable Fantasy [#h58446bb]
#highlight(kt){{
import java.util.Date
import java.text.SimpleDateFormat;

class Person {
  val firstName: String
  val lastName: String
  val birthday: Date?

  // "?"がついていなければ null 入れるコードはコンパイルすらできない
  constructor(firstName: String, lastName: String, birthday: Date?) {
    this.firstName = firstName
    this.lastName = lastName
    this.birthday = birthday
  }

  fun fullName(): String {
    return "%s %s".format(this.firstName, this.lastName)
  }

  // 誕生日が null なら何も計算せず(letの中に入らず)に null だけ返す
  // 整数型もオブジェクトのように扱えて、null にもできる
  fun age(): Int? {
    return this.birthday?.let {
      val sdf = SimpleDateFormat("yyyyMMdd")
      (sdf.format(Date()).toInt() - sdf.format(it).toInt()) / 10000
    }
  }
}

fun main(args: Array<String>) {
  val person1 = Person("Kanata", "Tanaka", SimpleDateFormat("yyyy/MM/dd").parse("2013/12/02"))
  println("%s (%s)".format(person1.fullName(), person1.age()?.toString()))

  val person2 = Person("Kanata", "Tanaka", null)
  // SQLのCOALESCE関数みたいなもの
  println("%s (%s)".format(person2.fullName(), person2.age()?.toString() ?: "?"))
}
}}

 PS W:\> kotlinc .\age.kt -include-runtime -d age.jar
 PS W:\> java -jar age.jar
 Kanata Tanaka (5)
 Kanata Tanaka (?)

**外部JARをコマンドラインから [#e7cb68c1]
実用性不明。試しに [[jsoup>https://jsoup.org/]] を手元に置いてから

#highlight(kt){{
import org.jsoup.Jsoup

fun main(args: Array<String>) {
  val doc = Jsoup.connect("http://www.capcom.co.jp/arcade/rev/PC/music.html").get()
  val elms = doc.select("p.n-mTitle")

  for (elm in elms) {
    println(elm.text())
  }
}
}}

kotlincで生成したJARにGetWebKt.classが含まれるので、これをメインクラスにして起動する(→[[参考>https://irof.hateblo.jp/entry/2016/04/02/164954]])

 PS W:\> kotlinc .\getWeb.kt -classpath .\jsoup-1.11.3.jar -include-runtime -d getWeb.jar
 PS W:\> java -classpath ".\jsoup-1.11.3.jar;getWeb.jar" GetWebKt
 Wanna Be Your Special
 Touch Of Gold
  :

*Spring BootをKotlinで使って、warを作ってTomcatに突っ込む [#ef91aef0]
ベースはこちら → [[Spring Boot(Kotlin) + GradleでAPIを書いてAzure Web Appにデプロイする。>https://qiita.com/hako1912/items/bb81f969ee3b0edd6b68]]

**プロジェクト作成 [#ff72011c]
[[Spring Initializr>https://start.spring.io/]]に作ってもらう

**コード実装 [#w1eea7c9]
***XxxxApplication.kt [#e138d6b9]
クラス名定義の後ろに SpringBootServletInitializer() を追加
#highlight(kt){{
package jp.maplia.xxxx

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer

@SpringBootApplication
class XxxxApplication: SpringBootServletInitializer()

fun main(args: Array<String>) {
  runApplication<XxxxApplication>(*args)
}
}}

***XxxxController.kt [#aede2696]
すべて新規
#highlight(kt){{
package jp.maplia.xxxx

import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
class XxxxController {
  @RequestMapping("/")
  fun index(): String {
    return "To be continued.."
    return "To be continued..."
  }
}
}}

***build.gradle [#zac40539]
追加する分だけ記載

warタスクを実行すると、build/libsに生成されたwarが格納される
#highlight(kt){{
apply plugin: 'war'
war {
  enabled = true
}
}}

**Tomcatにデプロイ [#n0813ca2]
$CATALINA_HOME/webappsに放り込む。環境に手を加えていなければこれだけでおしまい

#highlight(end)

IP:111.107.6.50 TIME:"2019-03-28 (木) 21:46:57" REFERER:"http://wiki.maplia.jp/?cmd=secedit&amp;page=Kotlin&amp;id=11" USER_AGENT:"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0"

トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS