0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

目的

家で VSCode、Liberty、Java、Maven を使う時も AI を使いたくなり、さくらのAIをためします。

KiloCode と さくらのAI Engine

以下のページを参考にセットアップしました。

APIキー

以下ページの下部にある ”ご利用の流れ” 手順でユーザーIDを登録します。クレジットカードの登録が必要ですが、AI サービスを登録するときに基盤モデル無料プランを選択するように注意します。APIトークンを生成します。

インストール

まず、VS Codeの拡張機能マーケットプレイスからKiloCodeをインストールします:

  1. VS Codeを開く
  2. 拡張機能ビュー(⇧⌘X / Ctrl+Shift+X)を開く
  3. 検索バーに「KiloCode」と入力
  4. 「Kilo Code AI Agent」を見つけてインストール

image.png

API設定

KiloCodeをインストールすると、VS Codeのサイドバーにアイコンが追加されます。設定手順は以下の通りです:

  1. KiloCodeアイコンをクリック
  2. チャット欄に /settings と送信
  3. Providers として「Custom Provider」で Connect をクリック
    image.png
  4. 以下の情報を入力:
    Base URL: https://api.ai.sakura.ad.jp/v1
    API Key: さくらのAI Engineで発行したAPIキー
    image.png

この設定により、さくらのAI EngineがKiloCodeから利用可能になります。以下は sakura-ai の gpt-oss-120b を選択した例です。

image.png

Maven プロジェクト

Maven プロジェクトは AI で作成せず、コマンドを使って自分で実行します。

プロジェクト作成

mvn archetype:generate を実行してメニューから選択します。

<!-- ★ここはコマンド実行 -->
mvn archetype:generate

<!-- ★ここで入力 -->
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 2384: liberty
Choose archetype:
1: remote -> io.openliberty.tools:liberty-archetype-ear (An archetype that generates a Java EE 8 multi-module project that includes an EJB module, a web application module and an EAR module)
2: remote -> io.openliberty.tools:liberty-archetype-webapp (-)
3: remote -> io.openliberty.tools:liberty-plugin-archetype (-)
4: remote -> net.wasdev.maven.tools.archetypes:ejb-jee5-liberty (EJB 3.0 project targeting WebSphere Liberty)
5: remote -> net.wasdev.maven.tools.archetypes:ejb-jee6-liberty (EJB 3.1 project targeting WebSphere Liberty)
6: remote -> net.wasdev.maven.tools.archetypes:ejb-jee7-liberty (EJB 3.2 project targeting WebSphere Liberty)
7: remote -> net.wasdev.maven.tools.archetypes:osgi-liberty (OSGi project targeting WebSphere Liberty)
8: remote -> net.wasdev.maven.tools.archetypes:osgi-web25-liberty (OSGi Web 2.5 project targeting WebSphere Liberty)
9: remote -> net.wasdev.maven.tools.archetypes:osgi-web30-liberty (OSGi Web 3.0 project targeting WebSphere Liberty)
10: remote -> net.wasdev.maven.tools.archetypes:osgi-web31-liberty (OSGi Web 3.1 project targeting WebSphere Liberty)
11: remote -> net.wasdev.maven.tools.archetypes:webapp-jee5-liberty (Web 2.5 project targeting WebSphere Liberty)
12: remote -> net.wasdev.maven.tools.archetypes:webapp-jee6-liberty (Web 3.0 project targeting WebSphere Liberty)
13: remote -> net.wasdev.maven.tools.archetypes:webapp-jee7-liberty (Web 3.1 project targeting WebSphere Liberty)
14: remote -> net.wasdev.wlp.maven:liberty-archetype-ear (An archetype that generates a Java EE 7 multi-module project that includes an EJB module, a web application module and an EAR module)
15: remote -> net.wasdev.wlp.maven:liberty-archetype-webapp (-)
16: remote -> net.wasdev.wlp.maven:liberty-plugin-archetype (-)

<!-- ★ここで入力 -->
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): : 2
Choose io.openliberty.tools:liberty-archetype-webapp version: 
1: 3.0
2: 3.0.1
3: 3.1
4: 3.2
5: 3.2.1
6: 3.2.2
7: 3.2.3
8: 3.2.4
9: 3.3-M1
10: 3.3-M2
11: 3.3-M3
12: 3.3-M4
13: 3.3
14: 3.3.1
15: 3.3.2
16: 3.3.3
17: 3.3.4
18: 3.3.5-M1
19: 3.3.5-M2
20: 3.4
21: 3.5
22: 3.5.1
23: 3.5.2
24: 3.6
25: 3.6.1
26: 3.7
27: 3.7.1

<!-- ★ここで入力 -->
Choose a number: 27: 27
Define value for property 'groupId': sakuranoai
Define value for property 'artifactId': sakuranoai
[INFO] Using property: runtimeGroupId = io.openliberty
[INFO] Using property: runtimeArtifactId = openliberty-runtime
[INFO] Using property: runtimeVersion = [21.0.0.3,)
[INFO] Using property: libertyPluginVersion = 3.7.1
[INFO] Using property: groupId = sakuranoai
[INFO] Using property: artifactId = sakuranoai
Define value for property 'version' 1.0-SNAPSHOT: 
Define value for property 'package' sakuranoai: 
Confirm properties configuration:
groupId: sakuranoai
artifactId: sakuranoai
runtimeGroupId: io.openliberty
runtimeArtifactId: openliberty-runtime
runtimeVersion: [21.0.0.3,)
libertyPluginVersion: 3.7.1
groupId: sakuranoai
artifactId: sakuranoai
version: 1.0-SNAPSHOT
package: sakuranoai

<!-- ★ここで入力 -->
 Y: Y
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: liberty-archetype-webapp:3.7.1
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: sakuranoai
[INFO] Parameter: artifactId, Value: sakuranoai
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value: sakuranoai
[INFO] Parameter: packageInPathFormat, Value: sakuranoai
[INFO] Parameter: runtimeVersion, Value: [21.0.0.3,)
[INFO] Parameter: package, Value: sakuranoai
[INFO] Parameter: groupId, Value: sakuranoai
[INFO] Parameter: runtimeGroupId, Value: io.openliberty
[INFO] Parameter: artifactId, Value: sakuranoai
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: runtimeArtifactId, Value: openliberty-runtime
[INFO] Parameter: libertyPluginVersion, Value: 3.7.1
[INFO] Project created from Archetype in dir: ${user.home}/git/sakurano-ai/sakuranoai
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  01:43 min
[INFO] Finished at: 2026-08-24T00:38:36+09:00
[INFO] ------------------------------------------------------------------------

pom.xml

自分用のテンプレートを作成したらいいのだけど、プロジェクトをテンプレートから作成したら pom.xml の以下を変更します。

maven.compiler.source と maven.compiler.target を 1.8 に変更する。前提条件などの理由がなければ tWAS でも動作するように 1.8 を使いがちです。

pom.xml
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <packaging.type>minify,runnable</packaging.type>
    </properties>

Liberty の installDirectory の指定を変更する。

pom.xml
            <plugin>
                <groupId>io.openliberty.tools</groupId>
                <artifactId>liberty-maven-plugin</artifactId>
                <version>3.11.4</version>
                <extensions>true</extensions>
                <configuration>
                    <installDirectory>${user.home}/wlp/26.0.0.5</installDirectory>
                    <serverName>sakurano-ai-svr</serverName>
                    <looseApplication>true</looseApplication>
                    <deployPackages>all</deployPackages>
                </configuration>
            </plugin>

Liberty サーバー

pom.xml の serverName で指定したサーバーを作成します。

cd ~/wlp/26.0.0.5/bin/
./server create sakurano-ai-svr

Liberty サーバーの起動

VS Code のメニューからサーバーを起動して、アプリケーションにアクセスして動作状況を確認します。以下のページが表示されれば問題ありません。

image.png

JAX-RS で Hello World

KiloCode に ”今ある sakuranoai MAVEN プロジェクト に JAX-RS 2.1 で HelloWorldをつくってほしい、Libertyで動くかもテストして” と依頼して作ります。

image.png

今後も AI に依頼するのであれば、知らなくても良いのですが、勉強のために更新後のファイルを確認します。次からはコピーして作成するだけでもできるようになります。

pom.xml

Maven の pom.xml の dependency に javax.ws.rs が追加されました。

pom.xml
        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>javax.ws.rs-api</artifactId>
            <version>2.1.1</version>
            <scope>provided</scope>
        </dependency>

xml:server.xml

Liberty の server.xml に jaxrs-2.1 feature が追加されました。

server.xml
        <feature>jaxrs-2.1</feature>

ApplicationConfig.java

JAX-RS Application クラスが作成されました。

ApplicationConfig.java
package com.example.rest;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("/api")
public class ApplicationConfig extends Application {
    // No additional configuration required
}

HelloWorld.java

JAX-RS の @Produces を含むクラスが作成されました。

HelloWorld.java
package com.example.rest;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/hello")
public class HelloWorld {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String greet() {
        return "Hello World";
    }
}

HelloWorldIT.java

テスト用のクラスが作成されました。

HelloWorldIT.java
package com.example.rest;

import static org.junit.Assert.*;
import org.junit.BeforeClass;
import org.junit.Test;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;

public class HelloWorldIT {
    private static String URL;

    @BeforeClass
    public static void init() {
        URL = "http://localhost:9080/sakuranoai/api/hello";
    }

    @Test
    public void testHelloWorld() throws Exception {
        HttpClient client = new HttpClient();
        GetMethod method = new GetMethod(URL);
        try {
            int statusCode = client.executeMethod(method);
            assertEquals("HTTP GET failed", HttpStatus.SC_OK, statusCode);
            String response = method.getResponseBodyAsString(1000);
            assertTrue("Unexpected response body", response.contains("Hello World"));
        } finally {
            method.releaseConnection();
        }
    }
}

まとめ

さくらのAI を使って Liberty、Java、Maven の構成を自動でしてくれることを確認しました。Eclipse でクラス作成時にダイアログを使っていましたが、AI で置き換えることができそうです。

0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?