2013-01-18 46 views
3

我正在將一個Java項目遷移到Maven,我們正在使用Appassembler maven插件(版本1.3)來生成shell啓動腳本。我的問題是如何重定向標準輸出和/或Java程序的輸出?這Appassembler的pom.xml配置Maven Appassembler插件 - 標準輸出和/或stderr重定向

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

產生:

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 "[email protected]" 

參數佔位符($ @)是在啓動腳本中最後生成的令牌。

+0

爲什麼不簡單地重定向生成的腳本的輸出而不是java調用,這比重定向jre調用的輸出更明顯。 – khmarbaise

+0

因爲我們將確保應用程序線程中的某些未捕獲的異常將被記錄。用你的方法,它會依賴腳本的調用者。 – luboskrnac

回答

3

找到解決此問題的解決方法。幸運的是,參數佔位符和生成的命令行參數在同一行。所以這pom.xml的配置:

<commandLineArguments> 
    <commandLineArgument>"[email protected]"</commandLineArgument> 
    <commandLineArgument>&gt;&gt;out.log</commandLineArgument> 
    <commandLineArgument>2&gt;&amp;1</commandLineArgument> 
    <commandLineArgument>#</commandLineArgument> 
</commandLineArguments> 

會生成腳本:

.... 
com.mycompany.app.App \ 
"[email protected]" >>out.log 2>&1 # "[email protected]" 

哈希是在bash議論,最後一個參數佔位符將被忽略,這個技巧將做轉向工作。

相關問題