【问题标题】:Maven - deploy a Spring boot jar file on remote server and run itMaven - 在远程服务器上部署 Spring Boot jar 文件并运行它
【发布时间】:2017-07-18 16:21:38
【问题描述】:

我正在使用 maven 构建我的 spring boot jar。

使用 wagon-maven-plugin 我可以将它上传到我的开发服务器上,但是我需要 ssh 到服务器并执行 java -jar package.jar 命令来启动应用程序. maven有没有办法上传jar然后运行它?

我已经尝试过 men:spring-boot:run 但它看起来只适用于本地,无法上传...

【问题讨论】:

标签: maven spring-boot


【解决方案1】:

在 pom.xml 文件中(build><plugins>...</plugins></build> 内部)添加下面的内容并运行命令:mvn antrun:run@deploy

                <!-- command: mvn clean package -Pdev; mvn antrun:run@deploy -Pdev -->
                <!-- http://devserver:8081/ -->
                <plugin>
                    <inherited>false</inherited>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>1.8</version>
                    <executions>
                        <execution>
                            <id>deploy</id>
                            <phase>install</phase>
                            <goals>
                                <goal>run</goal>
                            </goals>
                            <configuration>
                                <target name="scp-deploy" description="Use antrun plugin to deploy with SCP and SSH">
                                    <!-- remote host and the command to be executed there -->

                                    <echo message="Stopping deployed app ..." />
                                    <sshexec trust="true" failonerror="true"
                                        host="${devserver.host}"
                                        username="${devserver.username}"
                                        password="${devserver.password}"
                                        command="stop.sh"
                                        timeout="120000" />

                                    <!-- file to be transferred -->
                                    <echo message="Transfering ${project.build.directory}/${project.build.finalName}.${project.packaging} ..." />
                                    <scp trust="true" failonerror="true" verbose="off" sftp="true" 
                                        file="${project.build.directory}/${project.build.finalName}.${project.packaging}"
                                        todir="${devserver.username}:${devserver.password}@${devserver.host}:${devserver.path}/${project.artifactId}.${project.packaging}" />

                                    <!-- remote host and the command to be executed there -->
                                    <echo message="Starting htct app ..." />
                                    <sshexec trust="true" failonerror="true"
                                        host="${devserver.host}"
                                        username="${devserver.username}"
                                        password="${devserver.password}"
                                        command="start.sh"
                                        timeout="120000" />
                                    <echo message="The deployment is done." />

                                    <taskdef name="scp" classname="org.apache.tools.ant.taskdefs.optional.ssh.Scp">
                                        <classpath refid="maven.plugin.classpath" />
                                    </taskdef>
                                    <taskdef name="scp" classname="org.apache.tools.ant.taskdefs.optional.ssh.SSHExec">
                                        <classpath refid="maven.plugin.classpath" />
                                    </taskdef>
                                </target>
                            </configuration>
                        </execution>
                    </executions>
                    <dependencies>
                        <dependency>
                            <groupId>ant</groupId>
                            <artifactId>ant-commons-net</artifactId>
                            <version>1.6.5</version>
                        </dependency>
                        <dependency>
                            <groupId>org.apache.ant</groupId>
                            <artifactId>ant-jsch</artifactId>
                            <version>1.10.5</version>
                        </dependency>
                    </dependencies>
                </plugin>

【讨论】: