Top Banner
仕事でも Groovy を使おう! Groovy を堂々と使える環境を作ろ
18

仕事でも Groovy を使おう!

Jul 12, 2015

Download

Technology

Oda Shinsuke
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 仕事でも Groovy を使おう!

仕事でも Groovy を使おう!

Groovy を堂々と使える環境を作ろう

Page 2: 仕事でも Groovy を使おう!

アジェンダ

• 自己紹介

• Groovy って認知度低いよね?

• どうやって認めてもらうか?

• 体験談:私の場合

• まとめ

Page 3: 仕事でも Groovy を使おう!

自己紹介

• 名前:織田 信亮(おだ しんすけ)

• 勤務地:西中島で働いてます

• .NET (C#) がメイン (Java はよく知りません)

• オンラインの活動

Blog:おだのスペース

http://d.hatena.ne.jp/odashinsuke/

twitter:shinsukeoda

http://twitter.com/shinsukeoda

Page 4: 仕事でも Groovy を使おう!

Groovy って認知度低いよね?

• この勉強会に参加するようになるまで知らなかった

あんまり Java に興味無かったし、アンテナ張ってなかったのかな?

Page 5: 仕事でも Groovy を使おう!

会社でも…

• 隣に座ってる人も知らなかった

• Java をメインでやってる人も知らなかった

みんな、知らないじゃね?

Page 6: 仕事でも Groovy を使おう!

どうやって認めてもらうか

• 便利さを見せつける!!

いきなり「アプリケーションを Groovy で

作りましょう」と言っても受け入れてもらえません。

ひとまず、開発ツールやビルドプロセス

にちゃちゃっと取り入れて、見せびらかしましょう。

Page 7: 仕事でも Groovy を使おう!

でも、環境作るの大変じゃね?

• セットアップ簡単だよ

Windows なら、インストーラー叩くだけ

または、解凍して環境パス通すだけ

Page 8: 仕事でも Groovy を使おう!

新しい言語覚えるのダルイし

• Java の文法とほぼ同じで書けるよ

微妙に違ったりするので、全くそのまま移植は出来ないかも。

配列の初期化子とかダメじゃね?

Page 9: 仕事でも Groovy を使おう!

体験談:私の場合

• 当時取りかかっていたプロジェクトに、Excel から .properties ファイルを生成している物があった。

多言語対応用のメッセージ管理に Excel を使用し、properties ファイルを作成していた。

Page 10: 仕事でも Groovy を使おう!

こんな感じ

Page 11: 仕事でも Groovy を使おう!

VBA から Groovy へ

• 元々 Excel マクロ(VBA)で properties ファイルを作成していた。

• ある時、ビルドプロセスに取り込む事に・・・。

• Excel マクロだと、セキュリティを緩めないと自動で流せない!?

「なんか良い方法ない?」と相談を受ける。

そこで・・・

Page 12: 仕事でも Groovy を使おう!

Groovy なら簡単に出来ますよ!

____/\ /\ キリッ

/ (ー) (ー)\/ ⌒(__人__)⌒\| |r┬-| |\ `ー'´ /ノ \

/´ ヽ| l

\ヽ -一''''''"~~``'ー--、 -一'''''''ー-

、.ヽ____(⌒)(⌒)⌒) ) (⌒_

(⌒)⌒)⌒))

Page 13: 仕事でも Groovy を使おう!

どうやったか

• Scriptom を使いました。

Page 14: 仕事でも Groovy を使おう!

import org.codehaus.groovy.scriptom.*

def messages = [ ja : new Properties(), en : new Properties() ]

Scriptom.inApartment{def excelAppdef workBooktry { excelApp = new ActiveXObject('Excel.Application')def fileName = "~\\Message.xls"// ファイルを開くworkBook = excelApp.workbooks.open(fileName)

// セルに対しての操作workBook.sheets(1).cells.with {def rowIndex = 2def messageKey = cells.item(rowIndex, 1).valuewhile (messageKey) {

def japanese = cells.item(rowIndex, 2).valuedef english = cells.item(rowIndex, 3).valuemessages.ja.setProperty(messageKey, japanese)messages.en.setProperty(messageKey, english)rowIndex++messageKey = cells.item(rowIndex, 1).value

} }

} finally { workBook?.close()excelApp?.quit()

}}

messages.each { entry -> entry.value.store(new File("~\\Message_${entry.key}.properties").newOutputStream(), "$entry.key")

}

Page 15: 仕事でも Groovy を使おう!

Scriptom は遅かったので POI で

• Scriptom では結構時間が掛かったので、コメントにて教えてもらった POI で。

Page 16: 仕事でも Groovy を使おう!

import org.apache.poi.hssf.usermodel.HSSFWorkbook

def messages = [ ja : new Properties(), en : new Properties() ]

def fileName = /~\Message.xls/// ファイルを開くHSSFWorkbook workBook = new HSSFWorkbook(new File(fileName).newInputStream())

// セルに対しての操作def sheet = workBook.sheets[0]def rowIndex = 1def row = sheet.getRow(rowIndex)def messageKey = row.getCell((short) 0).getRichStringCellValue().getString()while (messageKey) {

def japanese = row.getCell((short) 1).getRichStringCellValue().getString()def english = row.getCell((short) 2).getRichStringCellValue().getString()messages.ja.setProperty(messageKey, japanese)messages.en.setProperty(messageKey, english)rowIndex++row = sheet.getRow(rowIndex)messageKey = row?.getCell((short) 0)?.getRichStringCellValue()?.getString()

}

messages.each { entry -> entry.value.store(new File(/~\Message_${entry.key}.properties/).newOutputStream(), "$entry.key")

}

Page 17: 仕事でも Groovy を使おう!

結構あっさり出来たので

• Groovy 良くね?と評価してもらい、正式に採用されました。

Page 18: 仕事でも Groovy を使おう!

まとめ

• いきなり開発に使用するのは難しい。

• 良いところを見せつける。

簡単なツールとか、GAnt とかが良いのかも。

※Ant は詳しくないので、紹介しません