LoginSignup
0
1

More than 5 years have passed since last update.

linebot-serverless-blueprint-javaの作り方(その1)

Posted at

linebot-serverless-blueprint-javaを作った!で紹介したLINE BOTの雛形の作り方を何回かに分けて紹介します。

環境

  • Windows 10
  • Node: v6.9.1
  • npm: 3.10.8
  • Git for Windows: 2.9.3.windows.2
  • Java 8
  • AWSアカウント(AdministratorAccess権限)
    ※rootアカウントはやめましょう

前準備~serverlessの導入

プロジェクト用フォルダーの作成

mkdir linebot-serverless-blueprint-java
cd linebot-serverless-blueprint-java

package.jsonの作成

npm init -y
package.json
{
  "name": "linebot-serverless-blueprint-java",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

serverlessのインストール

npm install serverless --save-dev

"devDependencies"が追加されます。

package.json
{
  "name": "linebot-serverless-blueprint-java",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "serverless": "^1.8.0"
  }
}

serverlessプロジェクトの作成

node_modules\.bin\serverless create -t aws-java-gradle

なぜかgradle\wrapper\gradle-wrapper.jarが作成されませんので、個別にダウンロードします。
https://github.com/serverless/serverless/blob/master/lib/plugins/create/templates/aws-java-gradle/gradle/wrapper/gradle-wrapper.jar
こんな感じになります。

C:.
│  .npmignore
│  build.gradle
│  gradlew
│  gradlew.bat
│  package.json
│  serverless.yml
│  
├─gradle
│  └─wrapper
│          gradle-wrapper.jar
│          gradle-wrapper.properties
│          
├─node_modules
~~~省略~~~
│              
└─src
    └─main
        ├─java
        │  └─com
        │      └─serverless
        │              ApiGatewayResponse.java
        │              Handler.java
        │              Response.java
        │              
        └─resources
                log4j.properties

Javaプロジェクトのフォルダー構成の変更

今回はJavaプロジェクトを2つ用意しますので、フォルダー構成を変更します。

srcフォルダーとbuild.gradleをサブフォルダー内に移動

mkdir webhook
move src webhook\
        1 個のディレクトリを移動しました。
move build.gradle webhook\
        1 個のファイルを移動しました。

もう一つのJavaプロジェクト用ファイルもコピーして作成

xcopy /E /I webhook reply
webhook\build.gradle
webhook\src\main\java\com\serverless\ApiGatewayResponse.java
webhook\src\main\java\com\serverless\Handler.java
webhook\src\main\java\com\serverless\Response.java
webhook\src\main\resources\log4j.properties
5 個のファイルをコピーしました

settings.gradleファイルの作成

type nul > settings.gradle
settings.gradle
include 'webhook','reply'

Gradle動作確認

gradlew projects
------------------------------------------------------------
Root project
------------------------------------------------------------

Root project 'linebot-serverless-blueprint-java'
+--- Project ':reply'
\--- Project ':webhook'

To see a list of the tasks of a project, run gradlew <project-path>:tasks
For example, try running gradlew :reply:tasks

BUILD SUCCESSFUL

Total time: 7.01 secs

build.gradleファイルの修正

webhook/build.gradleを少し修正します。reply/build.gradleも同様(baseNameはreply)に修正します。

webhook/build.gradle
apply plugin: 'java'

repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

+tasks.withType(AbstractCompile)*.options*.encoding = tasks.withType(GroovyCompile)*.groovyOptions*.encoding = 'UTF-8'

dependencies {
    compile (
        'com.amazonaws:aws-lambda-java-core:1.1.0',
        'com.amazonaws:aws-lambda-java-log4j:1.0.0',
+        'com.amazonaws:aws-java-sdk-dynamodb:1.11.98',
+        'com.linecorp.bot:line-bot-api-client:1.6.0',
    )
    testCompile 'junit:junit:4.12'
}

// Task for building the zip file for upload
task buildZip(type: Zip) {
    // Using the Zip API from gradle to build a zip file of all the dependencies
    //
    // The path to this zip file can be set in the serverless.yml file for the
    // package/artifact setting for deployment to the S3 bucket
    //
    // Link: https://docs.gradle.org/current/dsl/org.gradle.api.tasks.bundling.Zip.html

    // set the base name of the zip file
-    baseName = "hello"
+    baseName = "webhook"
    from compileJava
    from processResources
    into('lib') {
        from configurations.runtime
    }
}

build.dependsOn buildZip

以上で下準備は完了です。
次回はServerlessの設定やJavaプログラムについての解説を行いたいと思います。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1