java - sh - 使用 shell 脚本在 maven 项目中运行一个类文件

标签 java bash shell maven

我是编写 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/

相关文章:

java - JTidy Node.findBody() — 如何使用?

bash - 在变量中放置多行输出

linux - 如何在 gnome-terminal 中选择默认选项卡焦点?

linux - 列出并更改目录并在 Shell 中运行命令

shell - 在 nix-shell 中配置 Fish shell 提示符

php - 使用 php 启动 shell 脚本并退出它而不停止进程

java - 推荐的人脸识别开源库有哪些?

java - firebase错误默认FirebaseApp在此过程中未初始化

linux - bash:打印:找不到命令

java - 将文件读入数组时出现 NullPointerException