2016-07-10 116 views
3

我是新來編寫shell腳本運行在Maven項目類文件,並試圖寫一個小腳本使用下面的腳本在Maven項目運行的類文件:SH - 使用shell腳本

function cleanup() { 
    kill ${SERVER_PID} ${CLIENT_PID} 
    rm -f cp.txt 
} 

trap cleanup EXIT 

mvn test dependency:build-classpath -Dmdep.outputFile=cp.txt 
CLASSPATH=$(cat cp.txt):target/classes 
java -classpath ${CLASSPATH} com.practice.Server & 
SERVER_PID=$$ 

while ! nc localhost 1111 > /dev/null 2>&1 < /dev/null; do 
    echo "$(date) - waiting for server at localhost:1111..." 
    sleep 1 
done 

java -classpath ${CLASSPATH} com.practice.Client 
CLIENT_PID=$$ 
cleanup 

但我不斷收到

在本地主機等待服務器:1111

錯誤:

Error: Could not find or load main class com.paractice.Server 

注:該sh文件存在於項目文件夾中,即與src和目標文件夾平行。

請幫忙!!!

回答

1

您可以使用exec插件。

相反的:

mvn test dependency:build-classpath -Dmdep.outputFile=cp.txt 
CLASSPATH=$(cat cp.txt):target/classes 
java -classpath ${CLASSPATH} com.practice.Server & 

你可以這樣做:

mvn test exec:java -Dexec.mainClass=com.practice.Server 

這將同步運行程序。

可以通過在末端

使用mvn exec:exec要麼添加號(&)改變,其可異步運行:

mvn test exec:exec -Dexec.async=true -Dexec.executable="java" -Dexec.args="-classpath %classpath com.practice.Server" 

插件的全面介紹at http://www.mojohaus.org/exec-maven-plugin/index.html