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 example/javaweb/4-hello-micronaut and 5-todo-micronaut #3292

Merged
merged 9 commits into from
Jul 25, 2024
Merged
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
3 changes: 2 additions & 1 deletion docs/modules/ROOT/pages/Intro_to_Mill_for_Java.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ your projects in a way that's simple, fast, and predictable.
Mill automates dealing with a lot of common build-tool concerns such as caching,
incremental re-computation, and parallelism. This allows you
to focus your effort on the business logic unique to your build, while letting
Mill take care of all the rest.
Mill take care of all the rest. Mill's caching and parallelism mean it scales well
from small projects to large codebases with hundreds of modules.

Compared to Maven and Gradle, Mill builds are far more intuitive and easy to read,
write, and debug:
Expand Down
3 changes: 2 additions & 1 deletion docs/modules/ROOT/pages/Intro_to_Mill_for_Scala.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ and customize your projects in a way that's simple, fast, and predictable.
Mill automates dealing with a lot of common build-tool concerns such as caching,
incremental re-computation, and parallelism. This allows you
to focus your effort on the business logic unique to your build, while letting
Mill take care of all the rest.
Mill take care of all the rest. Mill's caching and parallelism mean it scales well
from small projects to large codebases with hundreds of modules.

Compared to SBT and other tools, Mill builds are far more intuitive and easy to read,
write, and debug:
Expand Down
7 changes: 7 additions & 0 deletions docs/modules/ROOT/pages/Java_Web_Build_Examples.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,10 @@ include::example/javaweb/2-hello-spring-boot.adoc[]

include::example/javaweb/3-todo-spring-boot.adoc[]

== Micronaut Hello World App

include::example/javaweb/4-hello-micronaut.adoc[]

== Micronaut TodoMvc App

include::example/javaweb/5-todo-micronaut.adoc[]
76 changes: 76 additions & 0 deletions example/javaweb/4-hello-micronaut/build.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import mill._, javalib._

object hello extends RootModule with MicronautModule {
def micronautVersion = "4.5.3"
def ivyDeps = Agg(
ivy"io.micronaut:micronaut-http-server-netty:$micronautVersion",
ivy"io.micronaut.serde:micronaut-serde-jackson:2.10.1",
ivy"ch.qos.logback:logback-classic:1.5.3",
)


object test extends MavenTests with TestModule.Junit5{
def ivyDeps = super.ivyDeps() ++ Agg(
ivy"io.micronaut:micronaut-http-client:$micronautVersion",
ivy"io.micronaut.test:micronaut-test-junit5:4.4.0",
ivy"org.junit.jupiter:junit-jupiter-api:5.8.1",
ivy"org.junit.jupiter:junit-jupiter-engine:5.8.1"
)
}
}

trait MicronautModule extends MavenModule{
def micronautVersion: String

object processors extends JavaModule{
def ivyDeps = Agg(
ivy"io.micronaut.data:micronaut-data-processor:4.7.0",
ivy"io.micronaut:micronaut-http-validation:$micronautVersion",
ivy"io.micronaut.serde:micronaut-serde-processor:2.9.0",
ivy"io.micronaut.validation:micronaut-validation-processor:4.5.0",
ivy"io.micronaut:micronaut-inject-java:$micronautVersion"
)
}

def javacOptions = Seq(
"-processorpath",
processors.runClasspath().map(_.path).mkString(":"),
"-parameters",
"-Amicronaut.processing.incremental=true",
"-Amicronaut.processing.group=example.micronaut",
"-Amicronaut.processing.module=todo",
"-Amicronaut.processing.annotations=example.micronaut.*",
)
}


// This example demonstrates how to set up a simple Micronaut example service,
// using the code from the
// https://guides.micronaut.io/latest/creating-your-first-micronaut-app.html[Micronaut Tutorial].
//
// To preserve compatibility with the file layout from the example project, we use `MavenModule`,
// which follows the `src/main/java` and `src/test/java` folder convention.
//
// Although Mill does not have a built in `MicronautModule`, this example shows how easy
// it is to define it yourself as `trait MicronautModule`: setting up the annotation processor
// classpath as a `JavaModule` and setting up the annotation via `javacOptions`. Once defined,
// you can then use `MicronautModule in your build just like you.
//
// The `MicronautModule` shown here does not implement the full functionality of the micronaut
// CLI; in particular, support for Micronaut AOT compilation is missing. But it easily can be
// extended with more features as necessary.


/** Usage

> mill test
...example.micronaut.HelloControllerTest#testHello()...

> mill runBackground

> curl http://localhost:8087/hello
...Hello World...

> mill clean runBackground

*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2017-2024 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.micronaut;

import io.micronaut.runtime.Micronaut;

public class Application {

public static void main(String[] args) {
Micronaut.run(Application.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2017-2024 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.micronaut;

import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Produces;

@Controller("/hello") // <1>
public class HelloController {
@Get // <2>
@Produces(MediaType.TEXT_PLAIN) // <3>
public String index() {
return "Hello World"; // <4>
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#Tue Jun 18 12:49:41 UTC 2024
micronaut.application.name=default
micronaut.server.port=8087
14 changes: 14 additions & 0 deletions example/javaweb/4-hello-micronaut/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<configuration>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%cyan(%d{HH:mm:ss.SSS}) %gray([%thread]) %highlight(%-5level) %magenta(%logger{36}) - %msg%n</pattern>
</encoder>
</appender>

<root level="info">
<appender-ref ref="STDOUT" />
</root>
</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2017-2024 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.micronaut;

import io.micronaut.runtime.EmbeddedApplication;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;

import jakarta.inject.Inject;

@MicronautTest
class DefaultTest {

@Inject
EmbeddedApplication<?> application;

@Test
void testItWorks() {
Assertions.assertTrue(application.isRunning());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2017-2024 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.micronaut;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import io.micronaut.http.HttpRequest;
import io.micronaut.http.MediaType;
import io.micronaut.http.client.HttpClient;
import io.micronaut.http.client.annotation.Client;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import org.junit.jupiter.api.Test;

import jakarta.inject.Inject;

@MicronautTest // <1>
public class HelloControllerTest {

@Inject
@Client("/") // <2>
HttpClient client;

@Test
public void testHello() {
HttpRequest<?> request = HttpRequest.GET("/hello").accept(MediaType.TEXT_PLAIN); // <3>
String body = client.toBlocking().retrieve(request);

assertNotNull(body);
assertEquals("Hello World", body);
}
}
97 changes: 97 additions & 0 deletions example/javaweb/5-todo-micronaut/build.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import mill._, javalib._

object hello extends RootModule with MicronautModule {
def micronautVersion = "4.4.3"
def runIvyDeps = Agg(
ivy"ch.qos.logback:logback-classic:1.5.3",
ivy"com.h2database:h2:2.2.224",
)

def ivyDeps = Agg(
ivy"io.micronaut:micronaut-http-server-netty:$micronautVersion",
ivy"io.micronaut.serde:micronaut-serde-jackson:2.9.0",
ivy"io.micronaut.data:micronaut-data-jdbc:4.7.0",
ivy"io.micronaut.sql:micronaut-jdbc-hikari:5.6.0",
ivy"io.micronaut.validation:micronaut-validation:4.5.0",

ivy"io.micronaut.views:micronaut-views-htmx:5.2.0",
ivy"io.micronaut.views:micronaut-views-thymeleaf:5.2.0",

ivy"org.webjars.npm:todomvc-common:1.0.5",
ivy"org.webjars.npm:todomvc-app-css:2.4.1",
ivy"org.webjars.npm:github-com-bigskysoftware-htmx:1.9.10",
)


object test extends MavenTests with TestModule.Junit5{
def ivyDeps = super.ivyDeps() ++ Agg(
ivy"com.h2database:h2:2.2.224",

ivy"io.micronaut:micronaut-http-client:$micronautVersion",
ivy"io.micronaut.test:micronaut-test-junit5:4.4.0",

ivy"org.junit.jupiter:junit-jupiter-api:5.8.1",
ivy"org.junit.jupiter:junit-jupiter-engine:5.8.1"
)
}
}

trait MicronautModule extends MavenModule{
def micronautVersion: String

object processors extends JavaModule{
def ivyDeps = Agg(
ivy"io.micronaut.data:micronaut-data-processor:4.7.0",
ivy"io.micronaut:micronaut-http-validation:$micronautVersion",
ivy"io.micronaut.serde:micronaut-serde-processor:2.9.0",
ivy"io.micronaut.validation:micronaut-validation-processor:4.5.0",
ivy"io.micronaut:micronaut-inject-java:$micronautVersion"
)
}

def javacOptions = Seq(
"-processorpath",
processors.runClasspath().map(_.path).mkString(":"),
"-parameters",
"-Amicronaut.processing.incremental=true",
"-Amicronaut.processing.group=example.micronaut",
"-Amicronaut.processing.module=todo",
"-Amicronaut.processing.annotations=example.micronaut.*",
)
}


// This example is a more complete example using Micronaut, adapted from
// https://github.com/sdelamo/todomvc. On top of the `MicronautModule` and
// annotation processing demonstrated by the previous example, this example
// shows how a "full stack" web application using Micronaut looks like:
//
// * Thymeleaf for HTML templating
// * Webjars for Javascript and CSS
// * HTMX for interactivity
// * Database interactions using JDBC and H2
// * Controllers, Repositories, Entities, Forms
// * A more detailed test suite
//
// Again, the example `MicronautModule` is by no means complete, but it demonstrates
// how Mill can be integrated with Micronaut's annotation processors and configuration,
// and can be extended to cover additional functionality in future


/** Usage

> mill test
...example.micronaut.LearnJsonTest...
...example.micronaut.TodoTest...
...example.micronaut.TodoItemMapperTest...
...example.micronaut.TodoItemControllerTest...
...example.micronaut.HtmxWebJarsTest...

> mill runBackground

> curl http://localhost:8088
...<h1>todos</h1>...

> mill clean runBackground

*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package example.micronaut;

import io.micronaut.runtime.Micronaut;

public class Application {

public static void main(String[] args) {
Micronaut.run(Application.class, args);
}
}
Loading
Loading