我是编写 shell 脚本的新手,正在尝试编写一个小脚本来使用以下 shell 脚本在 maven 项目中运行类文件:
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
但我不断得到
waiting for server at localhost:1111
有错误:
Error: Could not find or load main class com.paractice.Server
注意:此 sh 文件存在于项目文件夹中,即与 src 和目标文件夹平行。
请帮忙!!!
最佳答案
您可以使用 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"
有关插件的完整说明,请访问 http://www.mojohaus.org/exec-maven-plugin/index.html。
关于java - sh - 使用 shell 脚本在 maven 项目中运行一个类文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38294465/