Clean up the execution mechanism of examples

Motivation:

- There's no way to pass an argument to an example.
- Assigning a Maven profile for each example is an overkill.
  It makes the pom.xml crowded.

Modifications:

- Remove example profiles from example/pom.xml
- Keep the list of examples in run-example.sh
- run-example.sh passes all options to exec-maven-plugin.
  For example, we can now do this:

    ./run-example.sh -Dssl -Dport=443 http-server

Result:

- It's much easier to add a new example and provide an easy way to
  launch it.
- We can still pass an arbitrary argument to the example being launched.
  (I'll update all examples to make them get their options from system
  properties rather than from args[].
This commit is contained in:
Trustin Lee 2014-05-20 23:24:34 +09:00
parent 5354ccaa8f
commit 4436870b28
2 changed files with 36 additions and 23 deletions

View File

@ -101,21 +101,6 @@
</dependency> </dependency>
</dependencies> </dependencies>
<profiles>
<profile>
<id>spdy-server</id>
<properties>
<exampleClass>io.netty.example.spdy.server.SpdyServer</exampleClass>
</properties>
</profile>
<profile>
<id>spdy-client</id>
<properties>
<exampleClass>io.netty.example.spdy.client.SpdyClient</exampleClass>
</properties>
</profile>
</profiles>
<build> <build>
<plugins> <plugins>
<plugin> <plugin>
@ -129,6 +114,7 @@
${argLine.leak} ${argLine.leak}
${argLine.coverage} ${argLine.coverage}
-classpath %classpath -classpath %classpath
${argLine.example}
${exampleClass} ${exampleClass}
</commandlineArgs> </commandlineArgs>
<classpathScope>runtime</classpathScope> <classpathScope>runtime</classpathScope>

View File

@ -1,15 +1,42 @@
#!/bin/bash -e #!/bin/bash -e
cd "`dirname "$0"`"/example declare -A EXAMPLE_MAP=(
if [[ $# -ne 1 ]]; then ['spdy-server']='io.netty.example.spdy.server.SpdyServer'
echo "Usage: $0 <example-name>" >&2 ['spdy-client']='io.netty.example.spdy.client.SpdyClient'
)
EXAMPLE=''
EXAMPLE_CLASS=''
EXAMPLE_ARGS=''
I=0
while [[ $# -gt 0 ]]; do
ARG="$1"
shift
if [[ "$ARG" =~ (^-.+) ]]; then
if [[ -z "$EXAMPLE_ARGS" ]]; then
EXAMPLE_ARGS="$ARG"
else
EXAMPLE_ARGS="$EXAMPLE_ARGS $ARG"
fi
else
EXAMPLE="$ARG"
EXAMPLE_CLASS="${EXAMPLE_MAP["$EXAMPLE"]}"
break
fi
done
if [[ -z "$EXAMPLE" ]] || [[ -z "$EXAMPLE_CLASS" ]] || [[ $# -ne 0 ]]; then
echo " Usage: $0 [-D<name>[=<value>] ...] <example-name>" >&2
echo "Example: $0 -Dport=8443 -Dssl http-server" >&2
echo >&2 echo >&2
echo "Available examples:" >&2 echo "Available examples:" >&2
grep -E '^ <id>[-a-z0-9]*</id>' pom.xml | sed -e 's#\(^.*<id>\|</id>.*$\)##g' | sed -e 's#^# #' >&2 for E in "${!EXAMPLE_MAP[@]}"; do
echo " $E"
done | sort >&2
exit 1 exit 1
fi fi
EXAMPLE_NAME="$1" cd "`dirname "$0"`"/example
echo "[INFO] Running: $EXAMPLE ($EXAMPLE_CLASS $EXAMPLE_ARGS)"
echo "[INFO] Running: $EXAMPLE_NAME" exec mvn -nsu compile exec:exec -DargLine.example="$EXAMPLE_ARGS" -DexampleClass="$EXAMPLE_CLASS"
mvn -X -P "$EXAMPLE_NAME" compile exec:exec