Maven - 在远程服务器上部署Spring引导jar文件并运行它

时间:2017-07-18 16:21:38

标签: maven spring-boot

我正在使用maven来制作我的弹簧靴。

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

我曾尝试过男人:spring-boot:run但看起来它只适用于本地,没有上传的可能性......

1 个答案:

答案 0 :(得分:0)

将以下内容添加到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>