Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Scala to start.vertx.io #626

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/main/java/io/vertx/starter/model/Language.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ public enum Language {
JAVA("java", ".java"),

@JsonProperty("kotlin")
KOTLIN("kotlin", ".kt", "vertx-lang-kotlin");
KOTLIN("kotlin", ".kt", "vertx-lang-kotlin"),

@JsonProperty("scala")
SCALA("scala", ".scala");

private final String name;
private final String extension;
Expand Down
15 changes: 13 additions & 2 deletions src/main/resources/templates/build.gradle.kts.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ plugins {
<#else>
kotlin ("jvm") version "1.7.21"
</#if>
<#elseif language == "scala">
scala
<#else>
java
</#if>
Expand Down Expand Up @@ -71,11 +73,18 @@ dependencies {
</#list>
<#if language == "kotlin" && vertxVersion?starts_with("4.")>
implementation(kotlin("stdlib-jdk8"))
<#elseif language == "scala">
implementation("org.scala-lang:scala3-library_3:3.5.2")
implementation("io.vertx:vertx-lang-scala_3:${vertxVersion}")
</#if>
<#if hasPgClient>
implementation("com.ongres.scram:client:2.1")
</#if>
<#if hasVertxJUnit5>
<#if language == "scala">
testImplementation("io.vertx:vertx-lang-scala-test_3:${vertxVersion}")
testImplementation("org.scalatest:scalatest_3:3.2.19")
testRuntimeOnly("org.scalatestplus:junit-5-11_3:3.2.19.0")
<#elseif hasVertxJUnit5>
testImplementation("io.vertx:vertx-junit5")
testImplementation("org.junit.jupiter:junit-jupiter:$junitJupiterVersion")
<#elseif hasVertxUnit>
Expand Down Expand Up @@ -111,7 +120,9 @@ tasks.withType<ShadowJar> {
}

tasks.withType<Test> {
<#if hasVertxJUnit5>
<#if language == "scala">
useJUnitPlatform()
<#elseif hasVertxJUnit5>
useJUnitPlatform()
<#elseif hasVertxUnit>
useJUnit()
Expand Down
90 changes: 89 additions & 1 deletion src/main/resources/templates/pom.xml.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
<kotlin.version>1.7.21</kotlin.version>
</#if>

<#elseif language == "scala">
<scala.version>3.5.2</scala.version>
<scalatest.version>3.2.19</scalatest.version>
<#else>
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
</#if>
Expand Down Expand Up @@ -89,6 +92,20 @@
</dependency>
</#noparse>
</#if>

<#elseif language == "scala">
<#noparse>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala3-library_3</artifactId>
<version>${scala.version}</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-lang-scala_3</artifactId>
<version>${vertx.version}</version>
</dependency>
</#noparse>
</#if>
<#if hasPgClient>
<dependency>
Expand All @@ -98,7 +115,22 @@
</dependency>
</#if>

<#if hasVertxJUnit5>
<#if language == "scala">
<#noparse>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-lang-scala-test_3</artifactId>
<version>${vertx.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.scalatest</groupId>
<artifactId>scalatest_3</artifactId>
<version>${scalatest.version}</version>
<scope>test</scope>
</dependency>
</#noparse>
<#elseif hasVertxJUnit5>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-junit5</artifactId>
Expand Down Expand Up @@ -141,6 +173,11 @@
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
</#noparse>
<#elseif language == "scala">
<#noparse>
<sourceDirectory>${project.basedir}/src/main/scala</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/scala</testSourceDirectory>
</#noparse>
</#if>
<plugins>
<#if language == "kotlin">
Expand Down Expand Up @@ -172,6 +209,52 @@
</execution>
</executions>
</plugin>
<#elseif language == "scala">
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>4.9.2</version>
<configuration>
<args>
<arg>-feature</arg>
<arg>-deprecation</arg>
</args>
</configuration>
<executions>
<execution>
<id>compile</id>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.scalatest</groupId>
<artifactId>scalatest-maven-plugin</artifactId>
<version>2.2.0</version>
<configuration>
<#noparse>
<reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
</#noparse>
<junitxml>.</junitxml>
<filereports>WDF TestSuite.txt</filereports>
</configuration>
<executions>
<execution>
<id>test</id>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
<#else>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
Expand Down Expand Up @@ -220,6 +303,11 @@
<#noparse>
<version>${maven-surefire-plugin.version}</version>
</#noparse>
<#if language == "scala">
<configuration>
<skipTests>true</skipTests>
</configuration>
</#if>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
Expand Down
29 changes: 29 additions & 0 deletions src/main/resources/templates/src/main/scala/MainVerticle.scala.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package ${packageName}

import io.vertx.core.{AbstractVerticle, Promise}
import io.vertx.lang.scala.ScalaVerticle
import io.vertx.lang.scala.ImplicitConversions.vertxFutureToScalaFuture

import scala.concurrent.Future
import scala.language.implicitConversions

class MainVerticle extends AbstractVerticle:

override def start(startPromise: Promise[Void]): Unit =
vertx
.deployVerticle(HttpVerticle().asJava)
.onSuccess(_ => startPromise.complete)
.onFailure(e => startPromise.fail(e))


class HttpVerticle extends ScalaVerticle:

override def asyncStart: Future[Unit] =
vertx
.createHttpServer()
.requestHandler(_.response
.putHeader("content-type", "text/plain")
.end("Hello from Vert.x!"))
.listen(8888)
.onSuccess(_ => println("HTTP server started on port 8888"))
.mapEmpty[Unit]()
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ${packageName}

import io.vertx.core.http.HttpMethod

import io.vertx.lang.scala.ImplicitConversions.vertxFutureToScalaFuture
import io.vertx.lang.scala.testing.VerticleTesting

import org.scalatest.matchers.should.Matchers

import scala.language.implicitConversions


class TestMainVerticle extends VerticleTesting[HttpVerticle], Matchers:

"HttpVerticle" should "bind to 8888 and answer with 'Hello from Vert.x!'" in {
val httpClient = vertx.createHttpClient()

for {
req <- httpClient.request(HttpMethod.GET, 8888, "127.0.0.1", "/")
res <- req.send()
body <- res.body.map(_.toString)
assertion = body should equal("Hello from Vert.x!")
} yield assertion
}
2 changes: 1 addition & 1 deletion src/test/java/io/vertx/starter/service/GeneratorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ private void verifyMavenFiles() {

private void verifyMavenOutputFiles() throws IOException {
try (Stream<Path> pathStream = Files.walk(workdir)) {
Optional<Path> testResult = pathStream.filter(p -> p.toString().endsWith("TestMainVerticle.txt")).findFirst();
Optional<Path> testResult = pathStream.filter(p -> p.toString().endsWith("TestMainVerticle.xml")).findFirst();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tsegismont ScalaTest does not generate *.txt files, except for the whole suite. To fix this, I had to change this predicate here, so it looks for the xml test report instead. I think this is also more consistent with verifyGradleOutputFiles().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tsegismont just in case you missed it... This PR is ready to be reviewed now. 🤞 😄

assertThat(testResult).isPresent().hasValueSatisfying(p -> assertThat(p).isRegularFile());
assertThat(workdir.resolve("target/demo-1.0.0-SNAPSHOT-fat.jar")).isRegularFile();
}
Expand Down
Loading