Update to remove mvn exec and use java -jar fatjar for examples (#227)

* 1. Added Spring boot maven plugin to create fat jar.
2. Created new main class as entry point for the fat jar.
3. Updated Readme and comments for all the examples

* 1) Configured spring boot maven plugin to not include version in the generated fat jar name and updated readme and comments
2) Removed SpringBootApplication annotation from DaprMainApplication.
3) Minor formating changes

* Update DaprMainApplication.java

Co-authored-by: Artur Souza <artursouza.ms@outlook.com>
This commit is contained in:
Rinita De 2020-02-11 21:35:12 -05:00 committed by GitHub
parent 4e6c55f94e
commit dad47d251f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 80 additions and 25 deletions

View File

@ -143,6 +143,22 @@
<skip>true</skip> <skip>true</skip>
</configuration> </configuration>
</plugin> </plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<mainClass>io.dapr.springboot.DaprMainApplication</mainClass>
<classifier>exec</classifier>
<finalName>dapr-java-sdk-examples</finalName>
</configuration>
</execution>
</executions>
</plugin>
</plugins> </plugins>
</build> </build>
</project> </project>

View File

@ -17,8 +17,8 @@ import java.util.List;
* 1. Build and install jars: * 1. Build and install jars:
* mvn clean install * mvn clean install
* 2. Run the client: * 2. Run the client:
* dapr run --app-id demoactorclient --port 3006 -- mvn exec:java \ * dapr run --app-id demoactorclient --port 3006 -- java -jar \
* -pl=examples -Dexec.mainClass=io.dapr.examples.actors.http.DemoActorClient * examples/target/dapr-java-sdk-examples-exec.jar io.dapr.examples.actors.http.DemoActorClient
*/ */
public class DemoActorClient { public class DemoActorClient {

View File

@ -18,7 +18,8 @@ import org.apache.commons.cli.Options;
* mvn clean install * mvn clean install
* 2. Run the server: * 2. Run the server:
* dapr run --app-id demoactorservice --app-port 3000 --port 3005 \ * dapr run --app-id demoactorservice --app-port 3000 --port 3005 \
* -- mvn exec:java -pl=examples -Dexec.mainClass=io.dapr.examples.actors.http.DemoActorService -Dexec.args="-p 3000" * -- java -jar examples/target/dapr-java-sdk-examples-exec.jar \
* io.dapr.examples.actors.http.DemoActorService -p 3000
*/ */
public class DemoActorService { public class DemoActorService {

View File

@ -105,7 +105,7 @@ An actor inherits from `AbstractActor` and implements the constructor to pass th
Now, execute the following script in order to run DemoActorService: Now, execute the following script in order to run DemoActorService:
```sh ```sh
cd to [repo-root] cd to [repo-root]
dapr run --app-id demoactorservice --app-port 3000 --port 3005 -- mvn exec:java -pl=examples -D exec.mainClass=io.dapr.examples.actors.http.DemoActorService -D exec.args="-p 3000" dapr run --app-id demoactorservice --app-port 3000 --port 3005 -- java -jar examples/target/dapr-java-sdk-examples-exec.jar io.dapr.examples.actors.http.DemoActorService -p 3000
``` ```
### Running the Actor client ### Running the Actor client
@ -164,7 +164,7 @@ Use the follow command to execute the DemoActorClient:
```sh ```sh
cd to [repo-root] cd to [repo-root]
dapr run --app-id demoactorclient --port 3006 -- mvn exec:java -pl=examples -D exec.mainClass=io.dapr.examples.actors.http.DemoActorClient dapr run --app-id demoactorclient --port 3006 -- java -jar examples/target/dapr-java-sdk-examples-exec.jar io.dapr.examples.actors.http.DemoActorClient
``` ```
Once running, the `DemoActorClient` logs will start displaying the different steps: Once running, the `DemoActorClient` logs will start displaying the different steps:

View File

@ -18,7 +18,8 @@ import org.apache.commons.cli.Options;
* 2. cd to [repo-root]/examples * 2. cd to [repo-root]/examples
* 3. Run : * 3. Run :
* dapr run --app-id inputbinding --app-port 3000 --port 3005 \ * dapr run --app-id inputbinding --app-port 3000 --port 3005 \
* -- mvn exec:java -D exec.mainClass=io.dapr.examples.bindings.http.InputBindingExample -D exec.args="-p 3000" * -- java -jar target/dapr-java-sdk-examples-exec.jar \
* io.dapr.examples.bindings.http.InputBindingExample -p 3000
*/ */
public class InputBindingExample { public class InputBindingExample {

View File

@ -15,7 +15,7 @@ import io.dapr.client.DaprClientBuilder;
* 2. cd to [repo-root]/examples * 2. cd to [repo-root]/examples
* 3. Run the program: * 3. Run the program:
* dapr run --app-id outputbinding --port 3006 \ * dapr run --app-id outputbinding --port 3006 \
* -- mvn exec:java -D exec.mainClass=io.dapr.examples.bindings.http.OutputBindingExample * -- java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.bindings.http.OutputBindingExample
*/ */
public class OutputBindingExample { public class OutputBindingExample {

View File

@ -89,7 +89,7 @@ public class InputBindingController {
Execute the follow script in order to run the Input Binding example: Execute the follow script in order to run the Input Binding example:
```sh ```sh
cd to [repo-root]/examples cd to [repo-root]/examples
dapr run --app-id inputbinding --app-port 3000 --port 3005 -- mvn exec:java -D exec.mainClass=io.dapr.examples.bindings.http.InputBindingExample -D exec.args="-p 3000" dapr run --app-id inputbinding --app-port 3000 --port 3005 -- java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.bindings.http.InputBindingExample -p 3000
``` ```
### Running the Output binding sample ### Running the Output binding sample
@ -123,7 +123,7 @@ Use the follow command to execute the Output Binding example:
```sh ```sh
cd to [repo-root]/examples cd to [repo-root]/examples
dapr run --app-id outputbinding --port 3006 -- mvn exec:java -D exec.mainClass=io.dapr.examples.bindings.http.OutputBindingExample dapr run --app-id outputbinding --port 3006 -- java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.bindings.http.OutputBindingExample
``` ```
Once running, the OutputBindingExample should print the output as follows: Once running, the OutputBindingExample should print the output as follows:

View File

@ -13,7 +13,8 @@ import io.dapr.client.domain.Verb;
* 1. Build and install jars: * 1. Build and install jars:
* mvn clean install * mvn clean install
* 2. Send messages to the server: * 2. Send messages to the server:
* dapr run -- mvn exec:java -pl=examples -D exec.mainClass=io.dapr.examples.invoke.grpc.HelloWorldClient * dapr run -- java -jar examples/target/dapr-java-sdk-examples-exec.jar \
* io.dapr.examples.invoke.grpc.HelloWorldClient
*/ */
public class HelloWorldClient { public class HelloWorldClient {

View File

@ -30,8 +30,9 @@ import static io.dapr.examples.DaprExamplesProtos.SayResponse;
* mvn clean install * mvn clean install
* 2. Run in server mode: * 2. Run in server mode:
* dapr run --app-id hellogrpc --app-port 5000 --protocol grpc \ * dapr run --app-id hellogrpc --app-port 5000 --protocol grpc \
* -- mvn exec:java -pl=examples -D exec.mainClass=io.dapr.examples.invoke.grpc.HelloWorldService \ * -- java -jar examples/target/dapr-java-sdk-examples-exec.jar \
* -D exec.args="-p 5000" * io.dapr.examples.invoke.grpc.HelloWorldService \
* -p 5000
*/ */
public class HelloWorldService { public class HelloWorldService {

View File

@ -70,7 +70,7 @@ In the `GrpcHelloWorldDaprService` class, the `onInvoke` method is the most impo
Now run the service code: Now run the service code:
```sh ```sh
dapr run --app-id hellogrpc --app-port 5000 --protocol grpc -- mvn exec:java -pl=examples -D exec.mainClass=io.dapr.examples.invoke.grpc.HelloWorldService -D exec.args="-p 5000" dapr run --app-id hellogrpc --app-port 5000 --protocol grpc -- java -jar examples/target/dapr-java-sdk-examples-exec.jar io.dapr.examples.invoke.grpc.HelloWorldService -p 5000
``` ```
The `app-id` argument is used to identify this service in Dapr's runtime. The `app-port` determines which port Dapr's runtime should call into this service. The `protocol` argument informs Dapr which protocol it should use to invoke the application: `grpc` or `http`(default). The `app-id` argument is used to identify this service in Dapr's runtime. The `app-port` determines which port Dapr's runtime should call into this service. The `protocol` argument informs Dapr which protocol it should use to invoke the application: `grpc` or `http`(default).
@ -110,7 +110,7 @@ Finally, it will go through in an infinite loop and invoke the `say` method ever
Finally, open a new command line terminal and run the client code to send some messages. Finally, open a new command line terminal and run the client code to send some messages.
```sh ```sh
dapr run -- mvn exec:java -pl=examples -D exec.mainClass=io.dapr.examples.invoke.grpc.HelloWorldClient dapr run -- java -jar examples/target/dapr-java-sdk-examples-exec.jar io.dapr.examples.invoke.grpc.HelloWorldClient
``` ```
Once the messages are sent, use `CTRL+C` to exit Dapr. Once the messages are sent, use `CTRL+C` to exit Dapr.

View File

@ -16,7 +16,8 @@ import org.apache.commons.cli.Options;
* mvn clean install * mvn clean install
* 2. Run in server mode: * 2. Run in server mode:
* dapr run --app-id invokedemo --app-port 3000 --port 3005 \ * dapr run --app-id invokedemo --app-port 3000 --port 3005 \
* -- mvn exec:java -pl=examples -D exec.mainClass=io.dapr.examples.invoke.http.DemoService -D exec.args="-p 3000" * -- java -jar examples/target/dapr-java-sdk-examples-exec.jar \
* io.dapr.examples.invoke.http.DemoService -p 3000
*/ */
public class DemoService { public class DemoService {

View File

@ -13,8 +13,8 @@ import io.dapr.client.domain.Verb;
* 1. Build and install jars: * 1. Build and install jars:
* mvn clean install * mvn clean install
* 2. Send messages to the server: * 2. Send messages to the server:
* dapr run --port 3006 -- mvn exec:java -pl=examples \ * dapr run --port 3006 -- java -jar examples/target/dapr-java-sdk-examples-exec.jar \
* -Dexec.mainClass=io.dapr.examples.invoke.http.InvokeClient -Dexec.args="'message one' 'message two'" * 'message one' 'message two'
*/ */
public class InvokeClient { public class InvokeClient {

View File

@ -89,7 +89,7 @@ public class DemoServiceController {
Use the follow command to execute the demo service example: Use the follow command to execute the demo service example:
```sh ```sh
dapr run --app-id invokedemo --app-port 3000 --port 3005 -- mvn exec:java -pl=examples -D exec.mainClass=io.dapr.examples.invoke.http.DemoService -D exec.args="-p 3000" dapr run --app-id invokedemo --app-port 3000 --port 3005 -- java -jar examples/target/dapr-java-sdk-examples-exec.jar io.dapr.examples.invoke.http.DemoService -p 3000
``` ```
Once running, the ExposerService is now ready to be invoked by Dapr. Once running, the ExposerService is now ready to be invoked by Dapr.
@ -118,7 +118,7 @@ The class knows the app id for the remote application. It uses the the static `D
Execute the follow script in order to run the InvokeClient example, passing two messages for the remote method: Execute the follow script in order to run the InvokeClient example, passing two messages for the remote method:
```sh ```sh
dapr run --port 3006 -- mvn exec:java -pl=examples -D exec.mainClass=io.dapr.examples.invoke.http.InvokeClient -D exec.args="'message one' 'message two'" dapr run --port 3006 -- java -jar examples/target/dapr-java-sdk-examples-exec.jar io.dapr.examples.invoke.http.InvokeClient 'message one' 'message two'
``` ```
Once running, the output should display the messages sent from invoker in the demo service output as follows: Once running, the output should display the messages sent from invoker in the demo service output as follows:

View File

@ -16,7 +16,7 @@ import java.util.Collections;
* mvn clean install * mvn clean install
* 2. Run the program: * 2. Run the program:
* dapr run --app-id publisher --port 3006 -- \ * dapr run --app-id publisher --port 3006 -- \
* mvn exec:java -pl=examples -D exec.mainClass=io.dapr.examples.pubsub.http.Publisher * java -jar examples/target/dapr-java-sdk-examples-exec.jar io.dapr.examples.pubsub.http.Publisher
*/ */
public class Publisher { public class Publisher {

View File

@ -74,7 +74,7 @@ public class SubscriberController {
``` ```
Execute the follow script in order to run the Subscriber example: Execute the follow script in order to run the Subscriber example:
```sh ```sh
dapr run --app-id subscriber --app-port 3000 --port 3005 -- mvn exec:java -pl=examples -D exec.mainClass=io.dapr.examples.pubsub.http.Subscriber -D exec.args="-p 3000" dapr run --app-id subscriber --app-port 3000 --port 3005 -- java -jar examples/target/dapr-java-sdk-examples-exec.jar io.dapr.examples.pubsub.http.Subscriber -p 3000
``` ```
### Running the publisher ### Running the publisher
@ -122,7 +122,7 @@ public class Publisher {
Use the follow command to execute the Publisher example: Use the follow command to execute the Publisher example:
```sh ```sh
dapr run --app-id publisher --port 3006 -- mvn exec:java -pl=examples -D exec.mainClass=io.dapr.examples.pubsub.http.Publisher dapr run --app-id publisher --port 3006 -- java -jar examples/target/dapr-java-sdk-examples-exec.jar io.dapr.examples.pubsub.http.Publisher
``` ```
Once running, the Publisher should print the output as follows: Once running, the Publisher should print the output as follows:

View File

@ -17,7 +17,7 @@ import org.apache.commons.cli.Options;
* mvn clean install * mvn clean install
* 2. Run the server: * 2. Run the server:
* dapr run --app-id subscriber --app-port 3000 --port 3005 -- \ * dapr run --app-id subscriber --app-port 3000 --port 3005 -- \
* mvn exec:java -pl=examples -Dexec.mainClass=io.dapr.examples.pubsub.http.Subscriber -Dexec.args="-p 3000" * java -jar examples/target/dapr-java-sdk-examples-exec.jar io.dapr.examples.pubsub.http.Subscriber -p 3000
*/ */
public class Subscriber { public class Subscriber {

View File

@ -62,7 +62,7 @@ The code uses the `DaprClient` created by the `DaprClientBuilder`. Notice that t
Run this example with the following command: Run this example with the following command:
```sh ```sh
dapr run --port 3006 -- mvn exec:java -pl=examples -D exec.mainClass=io.dapr.examples.state.StateClient -D exec.args="'my message'" dapr run --port 3006 -- java -jar examples/target/dapr-java-sdk-examples-exec.jar io.dapr.examples.state.StateClient 'my message'
``` ```
Once running, the OutputBindingExample should print the output as follows: Once running, the OutputBindingExample should print the output as follows:

View File

@ -15,7 +15,8 @@ import reactor.core.publisher.Mono;
* mvn clean install * mvn clean install
* 2. send a message to be saved as state: * 2. send a message to be saved as state:
* dapr run --port 3006 -- \ * dapr run --port 3006 -- \
* mvn exec:java -pl=examples -D exec.mainClass=io.dapr.examples.state.StateClient -D exec.args="'my message'" * java -jar examples/target/dapr-java-sdk-examples-exec.jar \
* io.dapr.examples.state.StateClient 'my message'
*/ */
public class StateClient { public class StateClient {

View File

@ -0,0 +1,33 @@
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT License.
*/
package io.dapr.springboot;
import java.lang.reflect.Method;
import java.util.Arrays;
/**
* Dapr's Main Application to run using fatjar. This class will call the main source provided by user dynamically.
*/
public class DaprMainApplication {
/**
* Calls main method of the class provided by the user.
* @param args Accepts the classname as the first parameter. The rest are passed as argument as args.
*/
public static void main(String[] args) throws Exception {
String[] arguments;
if (args.length < 1) {
throw new IllegalArgumentException("Requires at least one argument - name of the main class");
} else {
arguments = Arrays.copyOfRange(args, 1, args.length);
Class mainClass = Class.forName(args[0]);
Method mainMethod = mainClass.getDeclaredMethod("main", String[].class);
Object[] methodArgs = new Object[1];
methodArgs[0] = arguments;
mainMethod.invoke(mainClass, methodArgs);
}
}
}