diff --git a/README.md b/README.md index fefd2ce5..5362eed7 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,35 @@ dependencies { } ``` +### Quickstart Archetypes + +Instead of configuring a project manually, you can generate a ready-to-use +skeleton with Maven Archetypes: + +**BOX API quickstart:** +```bash +mvn archetype:generate \ + -DarchetypeGroupId=io.tarantool \ + -DarchetypeArtifactId=tarantool-box-quickstart \ + -DarchetypeVersion=2.0.0 +``` + +**CRUD API quickstart:** +```bash +mvn archetype:generate \ + -DarchetypeGroupId=io.tarantool \ + -DarchetypeArtifactId=tarantool-crud-quickstart \ + -DarchetypeVersion=2.0.0 +``` + +> On Windows PowerShell, quote the version: DarchetypeVersion="2.0.0" + +The BOX archetype generates a minimal example of client initialization, idempotent space creation, and basic `insert`/`select` operations. + +The CRUD archetype generates a Spring Boot project with a `CrudRepository`, a `@Tuple`-mapped entity, and a `TarantoolCartridgeContainer`-based integration test. Note that the CRUD layer requires a Cartridge cluster with the `crud-router` and `crud-storage` roles enabled — a standalone Tarantool instance is not sufficient. + +> **Note:** Until the first release, build and install the archetypes locally with `mvn clean install -pl tarantool-archetypes -am` and pass `-DarchetypeCatalog=local` to `mvn archetype:generate`. + ## Documentation - [Release](RELEASING.md) diff --git a/pom.xml b/pom.xml index dffce18d..3ed843c2 100644 --- a/pom.xml +++ b/pom.xml @@ -88,6 +88,7 @@ ${project.parent.basedir}/tarantool-shared-resources/ ${project.basedir}/LICENSE_HEADER.txt 3.6.1 + 3.4.1 @@ -532,5 +533,6 @@ testcontainers-autogen documentation tarantool-java-sdk-bom + tarantool-archetypes diff --git a/tarantool-archetypes/pom.xml b/tarantool-archetypes/pom.xml new file mode 100644 index 00000000..6d8b9895 --- /dev/null +++ b/tarantool-archetypes/pom.xml @@ -0,0 +1,54 @@ + + + 4.0.0 + + + io.tarantool + tarantool-java-sdk + 2.0.0-SNAPSHOT + + + tarantool-archetypes + pom + Tarantool Java SDK :: Archetypes + Maven Archetype templates for quick project scaffolding + + + true + true + + + + + + org.apache.maven.archetype + archetype-packaging + ${maven-archetype-plugin.version} + + + + + + org.apache.maven.plugins + maven-archetype-plugin + ${maven-archetype-plugin.version} + + + org.codehaus.mojo + exec-maven-plugin + 3.1.0 + + ${package}.BoxApiApplication + + + + + + + tarantool-box-quickstart + tarantool-crud-quickstart + + diff --git a/tarantool-archetypes/tarantool-box-quickstart/pom.xml b/tarantool-archetypes/tarantool-box-quickstart/pom.xml new file mode 100644 index 00000000..f4d2f9d1 --- /dev/null +++ b/tarantool-archetypes/tarantool-box-quickstart/pom.xml @@ -0,0 +1,28 @@ + + + 4.0.0 + + + io.tarantool + tarantool-archetypes + 2.0.0-SNAPSHOT + + + tarantool-box-quickstart + maven-archetype + Tarantool Java SDK :: Archetypes :: Box Quickstart + Quickstart archetype for Tarantool BOX API + + + + + org.apache.maven.archetype + archetype-packaging + 3.4.1 + + + + diff --git a/tarantool-archetypes/tarantool-box-quickstart/src/main/resources/META-INF/maven/archetype-metadata.xml b/tarantool-archetypes/tarantool-box-quickstart/src/main/resources/META-INF/maven/archetype-metadata.xml new file mode 100644 index 00000000..1d0419ad --- /dev/null +++ b/tarantool-archetypes/tarantool-box-quickstart/src/main/resources/META-INF/maven/archetype-metadata.xml @@ -0,0 +1,31 @@ + + + + + + src/main/java + + **/*.java + + + + + src/main/resources + + **/*.xml + **/*.properties + + + + + + + README.md + + + + diff --git a/tarantool-archetypes/tarantool-box-quickstart/src/main/resources/archetype-resources/README.md b/tarantool-archetypes/tarantool-box-quickstart/src/main/resources/archetype-resources/README.md new file mode 100644 index 00000000..4e97cfe2 --- /dev/null +++ b/tarantool-archetypes/tarantool-box-quickstart/src/main/resources/archetype-resources/README.md @@ -0,0 +1,38 @@ +# ${artifactId} + +Quickstart project for working with the **Tarantool BOX API** via the Tarantool Java SDK. + +## Requirements + +- Java 17+ +- A running Tarantool instance on `localhost:3301` + +## Run + +```bash +mvn clean package +java -jar target/${artifactId}-${version}.jar +``` + +## What the example does + +1. Connects to Tarantool via `TarantoolFactory.box()`. +2. Idempotently creates the person space and the primary pk index. +3. Inserts the tuple `[1, 'Alice']`. +4. Reads the tuple by key 1 and prints it to the log. + +## Important for production +Creating the schema via eval on application startup is an anti-pattern. +In a real project, manage the schema via migrations: + +- Flyway with Java migrations +- tarantool/migrations (Lua) +- or an external schema deployment tool + +`if_not_exists = true` in the example only exists so that re-running the +quickstart does not fail with "Space 'person' already exists". + +## Dependencies + +- `io.tarantool:tarantool-client:1.7.3` — Box API +- Logback + SLF4J — logging diff --git a/tarantool-archetypes/tarantool-box-quickstart/src/main/resources/archetype-resources/pom.xml b/tarantool-archetypes/tarantool-box-quickstart/src/main/resources/archetype-resources/pom.xml new file mode 100644 index 00000000..74b02b84 --- /dev/null +++ b/tarantool-archetypes/tarantool-box-quickstart/src/main/resources/archetype-resources/pom.xml @@ -0,0 +1,50 @@ + + + 4.0.0 + + ${groupId} + ${artifactId} + ${version} + jar + + + 17 + 17 + UTF-8 + 1.7.3 + 2.0.16 + 1.5.12 + + + + + io.tarantool + tarantool-client + ${tarantool-client.version} + + + + org.slf4j + slf4j-api + ${slf4j.version} + + + ch.qos.logback + logback-classic + ${logback.version} + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.13.0 + + + + diff --git a/tarantool-archetypes/tarantool-box-quickstart/src/main/resources/archetype-resources/src/main/java/BoxApiApplication.java b/tarantool-archetypes/tarantool-box-quickstart/src/main/resources/archetype-resources/src/main/java/BoxApiApplication.java new file mode 100644 index 00000000..7a3ee9c9 --- /dev/null +++ b/tarantool-archetypes/tarantool-box-quickstart/src/main/resources/archetype-resources/src/main/java/BoxApiApplication.java @@ -0,0 +1,41 @@ +package ${package}; + +import io.tarantool.driver.api.TarantoolBoxClient; +import io.tarantool.driver.api.TarantoolFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Arrays; + +public class BoxApiApplication { + private static final Logger log = LoggerFactory.getLogger(BoxApiApplication.class); + + public static void main(String[] args) { + // 1. Initialize the Box API client. + // For local development without authentication: + TarantoolBoxClient client = TarantoolFactory.box() + .withHost("localhost") + .withPort(3301) + // .withCredentials("guest", "") // Uncomment if explicit authentication is required + .build(); + + try { + // 2. Prepare the schema (idempotent). + // IMPORTANT: in production, manage the schema via migrations + // (Flyway, Tarantool migrations), not via eval on application startup. + client.eval("return box.schema.create_space('person', {if_not_exists = true})").join(); + client.eval("return box.space.person:create_index('pk', " + + "{parts = {1, 'unsigned'}, if_not_exists = true})").join(); + + // 3. Work with data through the low-level Box API + var space = client.space("person"); + space.insert(Arrays.asList(1L, "Alice")).join(); + + var result = space.select(1L).join(); + log.info("Retrieved tuple: {}", result.get(0)); + + } finally { + client.close(); + } + } +} diff --git a/tarantool-archetypes/tarantool-box-quickstart/src/main/resources/archetype-resources/src/main/resources/logback.xml b/tarantool-archetypes/tarantool-box-quickstart/src/main/resources/archetype-resources/src/main/resources/logback.xml new file mode 100644 index 00000000..f2f696e6 --- /dev/null +++ b/tarantool-archetypes/tarantool-box-quickstart/src/main/resources/archetype-resources/src/main/resources/logback.xml @@ -0,0 +1,11 @@ + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + diff --git a/tarantool-archetypes/tarantool-box-quickstart/src/test/resources/projects/basic/archetype.properties b/tarantool-archetypes/tarantool-box-quickstart/src/test/resources/projects/basic/archetype.properties new file mode 100644 index 00000000..84c3a30d --- /dev/null +++ b/tarantool-archetypes/tarantool-box-quickstart/src/test/resources/projects/basic/archetype.properties @@ -0,0 +1,4 @@ +groupId=com.example.test +artifactId=my-box-app +version=0.0.1-TEST +package=com.example.test.box diff --git a/tarantool-archetypes/tarantool-box-quickstart/src/test/resources/projects/basic/goal.txt b/tarantool-archetypes/tarantool-box-quickstart/src/test/resources/projects/basic/goal.txt new file mode 100644 index 00000000..1d8e0fd8 --- /dev/null +++ b/tarantool-archetypes/tarantool-box-quickstart/src/test/resources/projects/basic/goal.txt @@ -0,0 +1 @@ +validate \ No newline at end of file diff --git a/tarantool-archetypes/tarantool-box-quickstart/src/test/resources/projects/basic/verify.groovy b/tarantool-archetypes/tarantool-box-quickstart/src/test/resources/projects/basic/verify.groovy new file mode 100644 index 00000000..7cefe4bd --- /dev/null +++ b/tarantool-archetypes/tarantool-box-quickstart/src/test/resources/projects/basic/verify.groovy @@ -0,0 +1,5 @@ +def readme = new File(context.projectDir, "README.md") + +assert readme.exists() : "README.md not generated at ${readme}" +assert readme.text.contains("# my-box-app") : "artifactId not substituted" +assert !readme.text.contains('${') : "Unresolved variables found" diff --git a/tarantool-archetypes/tarantool-crud-quickstart/pom.xml b/tarantool-archetypes/tarantool-crud-quickstart/pom.xml new file mode 100644 index 00000000..733de020 --- /dev/null +++ b/tarantool-archetypes/tarantool-crud-quickstart/pom.xml @@ -0,0 +1,28 @@ + + + 4.0.0 + + + io.tarantool + tarantool-archetypes + 2.0.0-SNAPSHOT + + + tarantool-crud-quickstart + maven-archetype + Tarantool Java SDK :: Archetypes :: CRUD Quickstart + Quickstart archetype for Tarantool CRUD API + + + + + org.apache.maven.archetype + archetype-packaging + 3.4.1 + + + + diff --git a/tarantool-archetypes/tarantool-crud-quickstart/src/main/resources/META-INF/maven/archetype-metadata.xml b/tarantool-archetypes/tarantool-crud-quickstart/src/main/resources/META-INF/maven/archetype-metadata.xml new file mode 100644 index 00000000..7aa7c55e --- /dev/null +++ b/tarantool-archetypes/tarantool-crud-quickstart/src/main/resources/META-INF/maven/archetype-metadata.xml @@ -0,0 +1,44 @@ + + + + + + src/main/java + + **/*.java + + + + src/test/java + + **/*.java + + + + src/main/resources + + **/*.yml + **/*.yaml + **/*.properties + + + + src/test/resources + + **/*.yml + **/*.lua + + + + + + pom.xml + README.md + + + + diff --git a/tarantool-archetypes/tarantool-crud-quickstart/src/main/resources/archetype-resources/README.md b/tarantool-archetypes/tarantool-crud-quickstart/src/main/resources/archetype-resources/README.md new file mode 100644 index 00000000..22dde32f --- /dev/null +++ b/tarantool-archetypes/tarantool-crud-quickstart/src/main/resources/archetype-resources/README.md @@ -0,0 +1,35 @@ +# ${artifactId} + +Quickstart project for working with the **Tarantool CRUD API** via Spring Data Tarantool. + +## Requirements + +- Java 17+ +- Docker (for integration tests) + +## Run the application + +```bash +mvn clean package +java -jar target/${artifactId}-${version}.jar +``` + +The application connects to Tarantool at localhost:3301 (see src/main/resources/application.yml). + +## Run the tests + +```bash +mvn test +``` + +The tests use TarantoolCartridgeContainer, which starts a Tarantool Cartridge cluster with the crud and vshard modules preinstalled. + +## Important for production + +- The CrudRepository layer is backed by Lua stored procedures from the crud module (crud.insert, crud.select, etc.). Therefore the target Tarantool must be a Cartridge cluster with the crud-router and crud-storage roles enabled, not a standalone instance. +- Manage the schema via migrations, not by creating spaces in application code. + +## Dependencies + +- `io.tarantool:tarantool-spring-data-33` — CRUD via tarantool/crud +- `io.tarantool:testcontainers-java-tarantool` — test cluster diff --git a/tarantool-archetypes/tarantool-crud-quickstart/src/main/resources/archetype-resources/pom.xml b/tarantool-archetypes/tarantool-crud-quickstart/src/main/resources/archetype-resources/pom.xml new file mode 100644 index 00000000..5c346fbc --- /dev/null +++ b/tarantool-archetypes/tarantool-crud-quickstart/src/main/resources/archetype-resources/pom.xml @@ -0,0 +1,64 @@ + + + 4.0.0 + + ${groupId} + ${artifactId} + ${version} + jar + + + org.springframework.boot + spring-boot-starter-parent + 3.3.13 + + + + + 17 + 1.7.2 + 1.5.0 + + + + + org.springframework.boot + spring-boot-starter + + + + io.tarantool + tarantool-spring-data-33 + ${tarantool-spring-data.version} + + + + org.springframework.boot + spring-boot-starter-test + test + + + io.tarantool + testcontainers-java-tarantool + ${testcontainers-tarantool.version} + test + + + org.testcontainers + junit-jupiter + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + diff --git a/tarantool-archetypes/tarantool-crud-quickstart/src/main/resources/archetype-resources/src/main/java/CrudApplication.java b/tarantool-archetypes/tarantool-crud-quickstart/src/main/resources/archetype-resources/src/main/java/CrudApplication.java new file mode 100644 index 00000000..fb59b485 --- /dev/null +++ b/tarantool-archetypes/tarantool-crud-quickstart/src/main/resources/archetype-resources/src/main/java/CrudApplication.java @@ -0,0 +1,27 @@ +package ${package}; + +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.data.tarantool.repository.config.EnableTarantoolRepositories; + +@SpringBootApplication +@EnableTarantoolRepositories +public class CrudApplication { + + public static void main(String[] args) { + SpringApplication.run(CrudApplication.class, args); + } + + @Bean + CommandLineRunner demo(PersonRepository repository) { + return args -> { + Person saved = repository.save(new Person(1L, "Bob")); + System.out.println("Saved: " + saved); + + repository.findById(1L).ifPresent(p -> + System.out.println("Found: " + p)); + }; + } +} diff --git a/tarantool-archetypes/tarantool-crud-quickstart/src/main/resources/archetype-resources/src/main/java/Person.java b/tarantool-archetypes/tarantool-crud-quickstart/src/main/resources/archetype-resources/src/main/java/Person.java new file mode 100644 index 00000000..89e162da --- /dev/null +++ b/tarantool-archetypes/tarantool-crud-quickstart/src/main/resources/archetype-resources/src/main/java/Person.java @@ -0,0 +1,51 @@ +package ${package}; + +import org.springframework.data.annotation.Id; +import org.springframework.data.tarantool.core.mapping.Field; +import org.springframework.data.tarantool.core.mapping.Tuple; + +/** + * Entity mapped to the Tarantool {@code person} space. + * {@link Tuple} binds the class to the space name, + * {@link Field} maps Java fields to tuple fields, + * and {@link Id} marks the primary key field used by findById/save. + */ +@Tuple("person") +public class Person { + + @Id + @Field(name = "id") + private Long id; + + @Field(name = "name") + private String name; + + public Person() { + } + + public Person(Long id, String name) { + this.id = id; + this.name = name; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public String toString() { + return "Person{id=" + id + ", name='" + name + "'}"; + } +} diff --git a/tarantool-archetypes/tarantool-crud-quickstart/src/main/resources/archetype-resources/src/main/java/PersonRepository.java b/tarantool-archetypes/tarantool-crud-quickstart/src/main/resources/archetype-resources/src/main/java/PersonRepository.java new file mode 100644 index 00000000..b1be86f9 --- /dev/null +++ b/tarantool-archetypes/tarantool-crud-quickstart/src/main/resources/archetype-resources/src/main/java/PersonRepository.java @@ -0,0 +1,12 @@ +package ${package}; + +import org.springframework.data.repository.CrudRepository; + +/** + * Repository interface for the {@link Person} entity. + * By extending {@link CrudRepository}, it automatically inherits implementations + * of the standard CRUD operations: save(), findById(), delete(), findAll(), etc. + * No implementation class is required — Spring Data generates a proxy at runtime. + */ +public interface PersonRepository extends CrudRepository { +} diff --git a/tarantool-archetypes/tarantool-crud-quickstart/src/main/resources/archetype-resources/src/main/resources/application.yml b/tarantool-archetypes/tarantool-crud-quickstart/src/main/resources/archetype-resources/src/main/resources/application.yml new file mode 100644 index 00000000..fb8a33ed --- /dev/null +++ b/tarantool-archetypes/tarantool-crud-quickstart/src/main/resources/archetype-resources/src/main/resources/application.yml @@ -0,0 +1,5 @@ +tarantool: + host: localhost + port: 3301 + username: admin + password: secret-cluster-cookie diff --git a/tarantool-archetypes/tarantool-crud-quickstart/src/main/resources/archetype-resources/src/test/java/PersonRepositoryTest.java b/tarantool-archetypes/tarantool-crud-quickstart/src/main/resources/archetype-resources/src/test/java/PersonRepositoryTest.java new file mode 100644 index 00000000..77d847b4 --- /dev/null +++ b/tarantool-archetypes/tarantool-crud-quickstart/src/main/resources/archetype-resources/src/test/java/PersonRepositoryTest.java @@ -0,0 +1,37 @@ +package ${package}; + +import io.tarantool.testcontainers.TarantoolCartridgeContainer; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; + +import static org.junit.jupiter.api.Assertions.*; + +@SpringBootTest +@Testcontainers +class PersonRepositoryTest { + + @Container + // TarantoolCartridgeContainer starts a Cartridge cluster with the + // vshard and crud roles enabled, which is required for CrudRepository + static final TarantoolCartridgeContainer tarantool = + new TarantoolCartridgeContainer("instances.yml", "topology.lua") + .withRouterHost("localhost") + .withRouterPort(3301) + .withRouterUsername("admin") + .withRouterPassword("secret-cluster-cookie"); + + @Autowired + private PersonRepository personRepository; + + @Test + void shouldSaveAndFindPerson() { + Person person = new Person(1L, "Alice"); + Person saved = personRepository.save(person); + assertNotNull(saved); + + assertTrue(personRepository.findById(1L).isPresent()); + } +} diff --git a/tarantool-archetypes/tarantool-crud-quickstart/src/main/resources/archetype-resources/src/test/resources/cartridge/instances.yml b/tarantool-archetypes/tarantool-crud-quickstart/src/main/resources/archetype-resources/src/test/resources/cartridge/instances.yml new file mode 100644 index 00000000..94f0c14b --- /dev/null +++ b/tarantool-archetypes/tarantool-crud-quickstart/src/main/resources/archetype-resources/src/test/resources/cartridge/instances.yml @@ -0,0 +1,7 @@ +quickstart.router: + advertise_uri: localhost:3301 + http_port: 8081 + +quickstart.s1-master: + advertise_uri: localhost:3302 + http_port: 8082 diff --git a/tarantool-archetypes/tarantool-crud-quickstart/src/main/resources/archetype-resources/src/test/resources/cartridge/topology.lua b/tarantool-archetypes/tarantool-crud-quickstart/src/main/resources/archetype-resources/src/test/resources/cartridge/topology.lua new file mode 100644 index 00000000..b0ff21c2 --- /dev/null +++ b/tarantool-archetypes/tarantool-crud-quickstart/src/main/resources/archetype-resources/src/test/resources/cartridge/topology.lua @@ -0,0 +1,19 @@ +-- Tarantool Cartridge cluster topology configuration. +-- We explicitly assign the vshard and crud roles so that Spring Data Repository +-- can invoke stored procedures such as crud.insert and crud.select. +local cartridge = require('cartridge') + +local replicasets = { + { + alias = 'router', + roles = {'vshard-router', 'crud-router'}, + join_servers = {{uri = 'localhost:3301'}} + }, + { + alias = 's1-storage', + roles = {'vshard-storage', 'crud-storage'}, + join_servers = {{uri = 'localhost:3302'}} + } +} + +return cartridge.admin_edit_topology({replicasets = replicasets})