Maven学习总结(48)——利用wagon-maven-plugin插件自动化部署Jar包/war包到Linux远程服务器

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://zhanghaiyang.blog.csdn.net/article/details/83720874

前言:

Maven项目可使用mvn package指令打包,打包完成后包位于target目录下,要想在远程服务器上部署,首先要将包上传到服务器。程序开发完成后部署到线上Linux服务器,可能需要经历打包、将包文件传到服务器、SSH连上服务器、敲命令启动程序等一系列繁琐的步骤。实际上这些步骤都可以通过Maven的一个插件wagon-maven-plugin来自动完成。wagon-maven-plugin是maven插件中的一种,其作用是去除我们部署时繁复的步骤,不用再手动上传jar包或者war包到指定服务器路径下面。

一、在Maven的配置文件settings.xml中配置好server的用户名和密码

<servers>
    <server>
        <id>zhy-linux-server</id>
        <username>zhy</username>
        <password>123456</password>
    </server>
</servers>

二、在项目的pom.xml中配置wagon-maven-plugin插件

1、jar包的几个配置示例如下

 <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>wagon-maven-plugin</artifactId>
                <version>1.0</version>
                <configuration>
                  <!--这里的id与之后的setting.xml配置项中的要一致 -->
                    <serverId>demo-test</serverId>
                  <!--本地包路径 -->
                    <fromDir>target/</fromDir>
                  <!--上传哪些包 -->
                    <includes>${project.artifactId}.jar</includes>
                  <!--包保存到服务器哪个路径下面,注意是scp,要支持这个命令才行 -->
                    <url>scp://172.168.1.11:22/opt/micservice</url>
                  <!--服务器目标路径,与url配合使用 -->
                    <toDir>apps/${project.artifactId}</toDir>
                  <!--因为存在服务器的重启之类的操作,wagon也支持使用shell命令,可以有多个command标签哦,根据先后顺序执行-->
                    <commands>
                        <command>cd /opt/micservice/ ; ./dev restart ${project.artifactId} test</command>
                    </commands>
                  <!-- 显示运行命令的输出结果 -->
                    <displayCommandOutputs>true</displayCommandOutputs>
                </configuration>
            </plugin>
        </plugins>
        <extensions>
            <extension>
                <groupId>org.apache.maven.wagon</groupId>
                <artifactId>wagon-ssh</artifactId>
                <version>2.8</version>
            </extension>
        </extensions>
    </build>
<build>
	<extensions>
		<extension>
			<groupId>org.apache.maven.wagon</groupId>
			<artifactId>wagon-ssh</artifactId>
			<version>2.8</version>
		</extension>
	</extensions>
	<plugins>
		<plugin>
			<groupId>org.codehaus.mojo</groupId>
			<artifactId>wagon-maven-plugin</artifactId>
			<version>1.0</version>
			<configuration>
				<fromFile>target/test.jar</fromFile>
				<url>scp://user:password@192.168.20.128/home/xxg/Desktop</url>
				<commands>
					<!-- 杀死原来的进程 -->
					<command>pkill -f test.jar</command>
					<!-- 重新启动test.jar,程序的输出结果写到nohup.out文件中 -->
					<command>nohup java -jar /home/xxg/Desktop/test.jar > /home/xxg/Desktop/nohup.out 2>&amp;1 &amp;</command>
				</commands>
				<!-- 显示运行命令的输出结果 -->
				<displayCommandOutputs>true</displayCommandOutputs>
			</configuration>
		</plugin>
	</plugins>
</build>

2、war包的几个配置示例如下

<build>
	<extensions>
		<extension>
			<groupId>org.apache.maven.wagon</groupId>
			<artifactId>wagon-ssh</artifactId>
			<version>2.8</version>
		</extension>
	</extensions>
	<plugins>
		<plugin>
			<groupId>org.codehaus.mojo</groupId>
			<artifactId>wagon-maven-plugin</artifactId>
			<version>1.0</version>
			<configuration>
				<fromFile>target/javawebdeploy.war</fromFile>
				<!-- 上传到Tomcat的webapps目录下 -->
				<url>scp://user:password@192.168.20.128/coder/tomcat/apache-tomcat-7.0.55/webapps</url>
				<commands>
					<!-- 重启Tomcat -->
					<command>sh /coder/tomcat/apache-tomcat-7.0.55/bin/shutdown.sh</command>
					<command>rm -rf /coder/tomcat/apache-tomcat-7.0.55/webapps/javawebdeploy</command>
					<command>sh /coder/tomcat/apache-tomcat-7.0.55/bin/startup.sh</command>
				</commands>
				<displayCommandOutputs>true</displayCommandOutputs>
			</configuration>
		</plugin>
	</plugins>
</build>
<build>
    <extensions>
        <extension>
            <groupId>org.apache.maven.wagon</groupId>
            <artifactId>wagon-ssh</artifactId>
            <version>2.8</version>
        </extension>
    </extensions>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>wagon-maven-plugin</artifactId>
            <version>1.0</version>
            <executions>
                <execution>
                    <id>upload-deploy</id>
                    <!-- 运行package打包的同时运行upload-single和sshexec-->
                    <phase>package</phase>
                    <goals>
                        <goal>upload-single</goal>
                        <goal>sshexec</goal>
                    </goals>
                    <configuration>
                        <serverId>mylinuxserver</serverId>
                        <fromFile>target/javawebdeploy.war</fromFile>
                        <url>scp://192.168.20.128/coder/tomcat/apache-tomcat-7.0.55/webapps</url>
                        <commands>
                            <command>sh /coder/tomcat/apache-tomcat-7.0.55/bin/shutdown.sh</command>
                            <command>rm -rf /coder/tomcat/apache-tomcat-7.0.55/webapps/javawebdeploy</command>
                            <command>sh /coder/tomcat/apache-tomcat-7.0.55/bin/startup.sh</command>
                        </commands>
                        <displayCommandOutputs>true</displayCommandOutputs>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

注意:<serverId>mylinuxserver</serverId>即上一步中在settings.xml中配置的service的id,<fromFile>target/zhy-pig.war</fromFile>是要上传到服务器的文件,一般来说是jar或者war包,<url><![CDATA[scp://ssh-user:ssh-password@ip:port/opt/dev/zhy-pig]]></url>配置服务器的地址以及文件上传的目录。

三、配置完成后,运行命令

mvn clean package wagon:upload-single wagon:sshexec

wagon:upload-single是上传jar或者war包,wagon:sshexec是执行配置中的shell命令。如果不想执行上面的这么长串命令,也可以使用execution。配置如下:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
	<artifactId>wagon-maven-plugin</artifactId>
    <executions>
	    <execution>
		    <id>upload-deploy</id>
		    <phase>package</phase>
		    <goals>
	            <goal>upload-single</goal>
	            <goal>sshexec</goal>
	        </goals>
	        <configuration>
		    <fromFile>target/zhy-pig.war</fromFile>
	            <url><![CDATA[scp://ssh-user:ssh-password@ip:port/opt/dev/zhy-pig]]></url>
		    <commands>
			    <command><![CDATA[docker restart zhy-pig-dev]]></command>
		    </commands>
		    <displayCommandOutputs>true</displayCommandOutputs>
		    </configuration>
	    </execution>
    </executions>
</plugin>
<build>
    <extensions>
        <extension>
            <groupId>org.apache.maven.wagon</groupId>
            <artifactId>wagon-ssh</artifactId>
            <version>2.8</version>
        </extension>
    </extensions>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>wagon-maven-plugin</artifactId>
            <version>1.0</version>
            <executions>
                <execution>
                    <id>upload-deploy</id>
                    <!-- 运行package打包的同时运行upload-single和sshexec-->
                    <phase>package</phase>
                    <goals>
                        <goal>upload-single</goal>
                        <goal>sshexec</goal>
                    </goals>
                    <configuration>
                        <serverId>mylinuxserver</serverId>
                        <fromFile>target/javawebdeploy.war</fromFile>
                        <url><![CDATA[scp://ssh-user:ssh-password@ip:port/opt/dev/zhy-pig]]></url>
                        <commands>
                            <command>sh /coder/tomcat/apache-tomcat-7.0.55/bin/shutdown.sh</command>
                            <command>rm -rf /coder/tomcat/apache-tomcat-7.0.55/webapps/javawebdeploy</command>
                            <command>sh /coder/tomcat/apache-tomcat-7.0.55/bin/startup.sh</command>
                        </commands>
                        <displayCommandOutputs>true</displayCommandOutputs>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
<build>
    <extensions>
        <extension>
            <groupId>org.apache.maven.wagon</groupId>
            <artifactId>wagon-ssh</artifactId>
            <version>2.8</version>
        </extension>
    </extensions>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>wagon-maven-plugin</artifactId>
            <version>1.0</version>
            <executions>
                <execution>
                    <id>upload-deploy</id>
                    <!-- 运行package打包的同时运行upload-single和sshexec-->
                    <phase>package</phase>
                    <goals>
                        <goal>upload-single</goal>
                        <goal>sshexec</goal>
                    </goals>
                    <configuration>
                        <serverId>mylinuxserver</serverId>
                        <fromFile>target/javawebdeploy.war</fromFile>
                        <url>scp://192.168.20.128/coder/tomcat/apache-tomcat-7.0.55/webapps</url>
                        <commands>
                            <command>sh /coder/tomcat/apache-tomcat-7.0.55/bin/shutdown.sh</command>
                            <command>rm -rf /coder/tomcat/apache-tomcat-7.0.55/webapps/javawebdeploy</command>
                            <command>sh /coder/tomcat/apache-tomcat-7.0.55/bin/startup.sh</command>
                        </commands>
                        <displayCommandOutputs>true</displayCommandOutputs>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

以上mvn clean package 来代替 mvn clean package wagon:upload-single wagon:sshexec,当然开发时期,我觉得还是使用命令的方式执行,或者通过自己写一个shell文件,需要的时候执行一下即可。

展开阅读全文

war包到tomcat运行报错

05-04

04-May-2018 10:07:00.735 信息 [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"]rn04-May-2018 10:07:00.777 信息 [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/readrn04-May-2018 10:07:00.792 信息 [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-nio-8009"]rn04-May-2018 10:07:00.799 信息 [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/readrn04-May-2018 10:07:00.800 信息 [main] org.apache.catalina.startup.Catalina.load Initialization processed in 870 msrn04-May-2018 10:07:00.858 信息 [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina]rn04-May-2018 10:07:00.859 信息 [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/9.0.7rn04-May-2018 10:07:00.897 信息 [main] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive [D:\web\apache-tomcat-9.0.7\webapps\book1.war]rn04-May-2018 10:07:39.514 严重 [main] org.apache.catalina.core.ContainerBase.addChildInternal ContainerBase.addChild: start:rn org.apache.catalina.LifecycleException: Failed to initialize component [org.apache.catalina.webresources.JarResourceSet@4eb1c69]rn at org.apache.catalina.util.LifecycleBase.handleSubClassException(LifecycleBase.java:441)rn at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:139)rn at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:173)rn at org.apache.catalina.webresources.StandardRoot.startInternal(StandardRoot.java:724)rn at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)rn at org.apache.catalina.core.StandardContext.resourcesStart(StandardContext.java:4717)rn at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:4852)rn at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)rn at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:742)rn at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:718)rn at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:703)rn at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:986)rn at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1857)rn at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)rn at java.util.concurrent.FutureTask.run(FutureTask.java:266)rn at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)rn at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:112)rn at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:772)rn at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:426)rn at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1585)rn at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:308)rn at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:123)rn at org.apache.catalina.util.LifecycleBase.setStateInternal(LifecycleBase.java:424)rn at org.apache.catalina.util.LifecycleBase.setState(LifecycleBase.java:367)rn at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:966)rn at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:839)rn at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)rn at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1427)rn at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1417)rn at java.util.concurrent.FutureTask.run(FutureTask.java:266)rn at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)rn at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134)rn at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:943)rn at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:258)rn at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)rn at org.apache.catalina.core.StandardService.startInternal(StandardService.java:422)rn at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)rn at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:770)rn at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)rn at org.apache.catalina.startup.Catalina.start(Catalina.java:682)rn at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)rn at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)rn at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)rn at java.lang.reflect.Method.invoke(Method.java:497)rn at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:350)rn at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:492)rnCaused by: java.lang.IllegalArgumentException: java.util.zip.ZipException: invalid LOC header (bad signature)rn at org.apache.catalina.webresources.AbstractSingleArchiveResourceSet.initInternal(AbstractSingleArchiveResourceSet.java:143)rn at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:136)rn ... 44 morernCaused by: java.util.zip.ZipException: invalid LOC header (bad signature)rn at java.util.zip.ZipFile.read(Native Method)rn at java.util.zip.ZipFile.access$1400(ZipFile.java:60)rn at java.util.zip.ZipFile$ZipFileInputStream.read(ZipFile.java:716)rn at java.util.zip.ZipFile$ZipFileInflaterInputStream.fill(ZipFile.java:419)rn at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:158)rn at sun.misc.IOUtils.readFully(IOUtils.java:65)rn at java.util.jar.JarFile.getBytes(JarFile.java:425)rn at java.util.jar.JarFile.getManifestFromReference(JarFile.java:193)rn at java.util.jar.JarFile.getManifest(JarFile.java:180)rn at org.apache.catalina.webresources.AbstractSingleArchiveResourceSet.initInternal(AbstractSingleArchiveResourceSet.java:141)rn ... 45 morernrn04-May-2018 10:07:39.519 严重 [main] org.apache.catalina.startup.HostConfig.deployWAR Error deploying web application archive [D:\web\apache-tomcat-9.0.7\webapps\book1.war]rn java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to initialize component [org.apache.catalina.webresources.JarResourceSet@4eb1c69]rn at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:746)rn at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:718)rn at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:703)rn at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:986)rn at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1857)rn at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)rn at java.util.concurrent.FutureTask.run(FutureTask.java:266)rn at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)rn at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:112)rn at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:772)rn at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:426)rn at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1585)rn at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:308)rn at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:123)rn at org.apache.catalina.util.LifecycleBase.setStateInternal(LifecycleBase.java:424)rn at org.apache.catalina.util.LifecycleBase.setState(LifecycleBase.java:367)rn at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:966)rn at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:839)rn at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)rn at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1427)rn at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1417)rn at java.util.concurrent.FutureTask.run(FutureTask.java:266)rn at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)rn at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134)rn at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:943)rn at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:258)rn at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)rn at org.apache.catalina.core.StandardService.startInternal(StandardService.java:422)rn at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)rn at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:770)rn at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)rn at org.apache.catalina.startup.Catalina.start(Catalina.java:682)rn at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)rn at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)rn at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)rn at java.lang.reflect.Method.invoke(Method.java:497)rn at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:350)rn rn这是 报错信息,用eclipse运行没一点事,放到tomcat下就报错 论坛

没有更多推荐了,返回首页