mirror of https://github.com/dapr/java-sdk.git
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:
parent
4e6c55f94e
commit
dad47d251f
|
@ -143,6 +143,22 @@
|
|||
<skip>true</skip>
|
||||
</configuration>
|
||||
</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>
|
||||
</build>
|
||||
</project>
|
||||
|
|
|
@ -17,8 +17,8 @@ import java.util.List;
|
|||
* 1. Build and install jars:
|
||||
* mvn clean install
|
||||
* 2. Run the client:
|
||||
* dapr run --app-id demoactorclient --port 3006 -- mvn exec:java \
|
||||
* -pl=examples -Dexec.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
|
||||
*/
|
||||
public class DemoActorClient {
|
||||
|
||||
|
|
|
@ -18,7 +18,8 @@ import org.apache.commons.cli.Options;
|
|||
* mvn clean install
|
||||
* 2. Run the server:
|
||||
* 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 {
|
||||
|
||||
|
|
|
@ -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:
|
||||
```sh
|
||||
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
|
||||
|
@ -164,7 +164,7 @@ Use the follow command to execute the DemoActorClient:
|
|||
|
||||
```sh
|
||||
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:
|
||||
|
|
|
@ -18,7 +18,8 @@ import org.apache.commons.cli.Options;
|
|||
* 2. cd to [repo-root]/examples
|
||||
* 3. Run :
|
||||
* 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 {
|
||||
|
||||
|
|
|
@ -15,7 +15,7 @@ import io.dapr.client.DaprClientBuilder;
|
|||
* 2. cd to [repo-root]/examples
|
||||
* 3. Run the program:
|
||||
* 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 {
|
||||
|
||||
|
|
|
@ -89,7 +89,7 @@ public class InputBindingController {
|
|||
Execute the follow script in order to run the Input Binding example:
|
||||
```sh
|
||||
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
|
||||
|
@ -123,7 +123,7 @@ Use the follow command to execute the Output Binding example:
|
|||
|
||||
```sh
|
||||
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:
|
||||
|
|
|
@ -13,7 +13,8 @@ import io.dapr.client.domain.Verb;
|
|||
* 1. Build and install jars:
|
||||
* mvn clean install
|
||||
* 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 {
|
||||
|
||||
|
|
|
@ -30,8 +30,9 @@ import static io.dapr.examples.DaprExamplesProtos.SayResponse;
|
|||
* mvn clean install
|
||||
* 2. Run in server mode:
|
||||
* 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"
|
||||
* -- java -jar examples/target/dapr-java-sdk-examples-exec.jar \
|
||||
* io.dapr.examples.invoke.grpc.HelloWorldService \
|
||||
* -p 5000
|
||||
*/
|
||||
public class HelloWorldService {
|
||||
|
||||
|
|
|
@ -70,7 +70,7 @@ In the `GrpcHelloWorldDaprService` class, the `onInvoke` method is the most impo
|
|||
Now run the service code:
|
||||
|
||||
```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).
|
||||
|
@ -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.
|
||||
|
||||
```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.
|
||||
|
|
|
@ -16,7 +16,8 @@ import org.apache.commons.cli.Options;
|
|||
* mvn clean install
|
||||
* 2. Run in server mode:
|
||||
* 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 {
|
||||
|
||||
|
|
|
@ -13,8 +13,8 @@ import io.dapr.client.domain.Verb;
|
|||
* 1. Build and install jars:
|
||||
* mvn clean install
|
||||
* 2. Send messages to the server:
|
||||
* dapr run --port 3006 -- mvn exec:java -pl=examples \
|
||||
* -Dexec.mainClass=io.dapr.examples.invoke.http.InvokeClient -Dexec.args="'message one' 'message two'"
|
||||
* dapr run --port 3006 -- java -jar examples/target/dapr-java-sdk-examples-exec.jar \
|
||||
* 'message one' 'message two'
|
||||
*/
|
||||
public class InvokeClient {
|
||||
|
||||
|
|
|
@ -89,7 +89,7 @@ public class DemoServiceController {
|
|||
Use the follow command to execute the demo service example:
|
||||
|
||||
```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.
|
||||
|
@ -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:
|
||||
```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:
|
||||
|
||||
|
|
|
@ -16,7 +16,7 @@ import java.util.Collections;
|
|||
* mvn clean install
|
||||
* 2. Run the program:
|
||||
* 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 {
|
||||
|
||||
|
|
|
@ -74,7 +74,7 @@ public class SubscriberController {
|
|||
```
|
||||
Execute the follow script in order to run the Subscriber example:
|
||||
```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
|
||||
|
@ -122,7 +122,7 @@ public class Publisher {
|
|||
Use the follow command to execute the Publisher example:
|
||||
|
||||
```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:
|
||||
|
|
|
@ -17,7 +17,7 @@ import org.apache.commons.cli.Options;
|
|||
* mvn clean install
|
||||
* 2. Run the server:
|
||||
* 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 {
|
||||
|
||||
|
|
|
@ -62,7 +62,7 @@ The code uses the `DaprClient` created by the `DaprClientBuilder`. Notice that t
|
|||
|
||||
Run this example with the following command:
|
||||
```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:
|
||||
|
||||
|
|
|
@ -15,7 +15,8 @@ import reactor.core.publisher.Mono;
|
|||
* mvn clean install
|
||||
* 2. send a message to be saved as state:
|
||||
* 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 {
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue