Search code examples
javabashshellmavenappassembler

Maven Appassembler plugin - stdout and/or stderr redirection


I am migrating one java project into Maven and we are using Appassembler maven plugin (version 1.3) for generating shell start script. My problem is how to redirect stdout and/or output of java program? This Appassembler's pom.xml configuration

        <program>
          <mainClass>com.mycompany.app.App</mainClass>
          <commandLineArguments>
            <commandLineArgument>arg1</commandLineArgument>
            <commandLineArgument>arg2</commandLineArgument>
          </commandLineArguments>
          <name>app</name>
        </program>

generates:

exec "$JAVACMD" $JAVA_OPTS \
  $EXTRA_JVM_ARGUMENTS \
  -classpath "$CLASSPATH" \
  -Dapp.name="app" \
  -Dapp.pid="$$" \
  -Dapp.repo="$REPO" \
  -Dbasedir="$BASEDIR" \
  com.mycompany.app.App \
  arg1 arg2 "$@"

Parameters placeholder ($@) is the last generated token in the start script.


Solution

  • Found a workaround on this problem. Fortunately the parameters placeholder is on the same line as generated command line arguments. So this pom.xml configuration:

    <commandLineArguments>
        <commandLineArgument>"$@"</commandLineArgument>
        <commandLineArgument>&gt;&gt;out.log</commandLineArgument>
        <commandLineArgument>2&gt;&amp;1</commandLineArgument>
        <commandLineArgument>#</commandLineArgument>
    </commandLineArguments>
    

    will generate the script:

    ....
    com.mycompany.app.App \
    "$@" >>out.log 2>&1 # "$@"
    

    Hash is comment in bash so the last parameter placeholder will be ignored and this hack will do the redirecting job.