maven项目打包、远程部署

一、说明:基于maven项目的打包发布

二、工具:

    eclipse、maven插件、tomcat7、远程服务器

三、项目打包配置

 1、按环境配置打包。

    由于我们经常在部署的时候 会部署多套地址,比如在本地会部署自己测试,在开发环境部署测试的,在线上部署联调测试等等
所以在打包的时候 就得分别打包出不同的配置文件。

首先我们需要配置<profiles>分别配置多个环境的名称和插件。,一开始我们不使用远程部署 则可以先不配置内部的<build>插件

例:

<profile>
        <!-- 测试环境 -->
        <id>dev</id>
        <properties>
            <package.type>dev</package.type>
        </properties>
    </profile>

        这里配置的id 是test 稍后我们在运行maven命令的时候会使用到。内部的package.type,是在公共插件下使用的

        配置公用插件,运行maven打包命令会自动执行,maven-war-plugin这个打包插件
其他的没什么说的${package.type} 对应的是上面配置的<profile>中的package.type
然后主要看我们的<resource>,<directory>是选择目录,<targetPath>是目的地目录,做用就是将<directory>目录下的东西打包到<targetPath>目录下,所以在我的项目根目录下会建立这么个目录,内部放置的就是需要打包的文件,这个插件是在项目打包完成之后再进行运行的。

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.1.1</version>
            <configuration>
                <!-- <warName>zbox-bios</warName> -->
                <webResources>
                    <resource>
                        <directory>resources/classPath/${package.type}</directory>
                        <targetPath>WEB-INF/classes</targetPath>
                        <filtering>true</filtering>
                    </resource>
                    <resource>
                        <directory>resources/webInfo/${package.type}</directory>
                        <targetPath>WEB-INF</targetPath>
                        <filtering>true</filtering>
                    </resource>
                    <resource>
                        <directory>resources/webRoot/${package.type}</directory>
                        <targetPath></targetPath>
                        <filtering>true</filtering>
                    </resource>
                </webResources>
                <!-- warSourceExcludes是在编译周期进行完成后从src/main/webapp
                目录复制文件时忽略,而packagingExcludes是在复制webapp目录完成后打包时
                忽略target/mywebapp-1.0.4 文件夹的文件 -->
                <!-- <packagingExcludes>  
                    src/main/webapp/node_modules/**
                </packagingExcludes> -->
                <warSourceExcludes>node_modules/**</warSourceExcludes>
            </configuration>
        </plugin>
    </plugins>
</build>

2、运行maven命令进行打包

右键项目》run as》maven build

在Goals栏输入打包命令 :clean package,在Profiles输入我们之前配置的profile的id,确认无误点击下方的run

等待打包完成,在项目的target下会生成war包

当然,有可能会失败,先看下我的整个pom.xml的配置

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.hao.zbox</groupId>
    <version>0.0.2</version>

    <artifactId>zbox-bios</artifactId>
    <name>zbox-bios</name>
    <packaging>war</packaging>

    <url>http://maven.apache.org</url>
    <dependencies>
    </dependencies>

    <profiles>
        <profile>
            <!-- 本地开发环境 -->
            <id>dev</id>
            <properties>
                <package.type>dev</package.type>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <!-- ali开发环境 -->
            <id>ali</id>
            <properties>
                <package.type>ali</package.type>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <!-- 测试环境 -->
            <id>test</id>
            <properties>
                <package.type>test</package.type>
            </properties>
        </profile>
        <profile>
            <!-- 生产环境 -->
            <id>prod</id>
            <properties>
                <package.type>prod</package.type>
            </properties>
        </profile>
    </profiles>
    <build>
        <plugins>
            <!-- define the project compile level -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1.1</version>
                <configuration>
                    <!-- <warName>zbox-bios</warName> -->
                    <webResources>
                        <resource>
                            <directory>resources/classPath/${package.type}</directory>
                            <targetPath>WEB-INF/classes</targetPath>
                            <filtering>true</filtering>
                        </resource>
                        <resource>
                            <directory>resources/webInfo/${package.type}</directory>
                            <targetPath>WEB-INF</targetPath>
                            <filtering>true</filtering>
                        </resource>
                        <resource>
                            <directory>resources/webRoot/${package.type}</directory>
                            <targetPath></targetPath>
                            <filtering>true</filtering>
                        </resource>
                    </webResources>
                    <warSourceExcludes>node_modules/**</warSourceExcludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

大部分错误其实都出在打包目录上。咱们仔细看一下打包插件的目录配置这里,仔细看右边,假设我们现在运行的profile是dev,那么下图右侧的${package.type}则为dev,意思就是右侧第一个红框内的意思就是:将resources/classPath/dev目录下的所有文件打包到WEB-INF/classes目录下,这时,这两个文件夹必须同时存在。只要有一个文件夹不存在都不行,就算文件夹内没有需要打包的文件,只要你配置了,那就必须得有这个文件夹。

四、远程发布项目到tomcat7

要远程打包发布,首先得完成上面的第三项,打包。

配置上其实就是在profile的配置中新增了远程发布的插件而已

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.hao.zbox</groupId>
    <version>0.0.2</version>

    <artifactId>zbox-bios</artifactId>
    <name>zbox-bios</name>
    <packaging>war</packaging>

    <url>http://maven.apache.org</url>
    <dependencies>
    </dependencies>

    <profiles>
        <profile>
            <!-- 本地开发环境 -->
            <id>dev</id>
            <properties>
                <package.type>dev</package.type>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.tomcat.maven</groupId>
                        <artifactId>tomcat7-maven-plugin</artifactId>
                        <version>2.2</version>
                        <configuration>
                            <update>true</update>
                            <path>/ROOT</path>
                            <url>http://127.0.0.1:8022/manager/text</url>
                            <username>tomcat</username>
                            <password>123456</password>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <!-- ali开发环境 -->
            <id>ali</id>
            <properties>
                <package.type>ali</package.type>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.tomcat.maven</groupId>
                        <artifactId>tomcat7-maven-plugin</artifactId>
                        <version>2.2</version>
                        <configuration>
                            <update>true</update>
                            <path>/ROOT</path>
                            <url>http://0.0.0.0:8021/manager/text</url>
                            <username>tomcat</username>
                            <password>123456</password>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <!-- 测试环境 -->
            <id>test</id>
            <properties>
                <package.type>test</package.type>
            </properties>
        </profile>
        <profile>
            <!-- 生产环境 -->
            <id>prod</id>
            <properties>
                <package.type>prod</package.type>
            </properties>
        </profile>
    </profiles>
    <build>
        <plugins>
            <!-- define the project compile level -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1.1</version>
                <configuration>
                    <!-- <warName>zbox-bios</warName> -->
                    <webResources>
                        <resource>
                            <directory>resources/classPath/${package.type}</directory>
                            <targetPath>WEB-INF/classes</targetPath>
                            <filtering>true</filtering>
                        </resource>
                        <resource>
                            <directory>resources/webInfo/${package.type}</directory>
                            <targetPath>WEB-INF</targetPath>
                            <filtering>true</filtering>
                        </resource>
                        <resource>
                            <directory>resources/webRoot/${package.type}</directory>
                            <targetPath></targetPath>
                            <filtering>true</filtering>
                        </resource>
                    </webResources>
                    <warSourceExcludes>node_modules/**</warSourceExcludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

如上,我们的配置其实就好了。但是,其实远程发布的重点不在pom.xml的配置上。问题出在远程服务器上。

要远程打包成功,需要多项条件:

1、远程服务器上必须能跑起来一个裸的tomcat7,这里我用的是apache-tomcat-7.0.59这个版本。

2、远程服务器启动tomcat之后能够打开tomcat主页,也就是直接浏览器输入服务器IP+tomcat端口。能够进入主页即可

3、在tomcat安装目录中conf/目录下 修改tomcat-users.xml配置文件,新增用户及其权限。

<?xml version='1.0' encoding='utf-8'?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<tomcat-users>
<!--
  NOTE:  By default, no user is included in the "manager-gui" role required
  to operate the "/manager/html" web application.  If you wish to use this app,
  you must define such a user - the username and password are arbitrary.
-->
<!--
  NOTE:  The sample user and role entries below are wrapped in a comment
  and thus are ignored when reading this file. Do not forget to remove
  <!.. ..> that surrounds them.
-->
<!-- 
  <role rolename="tomcat"/>
  <role rolename="role1"/>
  <user username="tomcat" password="tomcat" roles="tomcat"/>
  <user username="both" password="tomcat" roles="tomcat,role1"/>
  <user username="role1" password="tomcat" roles="role1"/>-->
  <role rolename="manager-gui"/>
  <role rolename="manager-script"/>
  <user username="tomcat" password="123456" roles="manager-gui,manager-script" />

</tomcat-users>

4、修改本地maven配置文件,同样新增用户配置,必须和第三条的账户密码一致。

settings.xml中的servers栏新增

<server>
		<id>tomcat7</id>
		<username>tomcat</username>
		<password>123456</password>
	</server>

5、需要在tomcat安装目录中 conf/Catalina/localhost  这个路径的文件夹。一开始在conf文件夹中其实是没有Catalina这个文件夹的。然后        在这个目录中新增配置文件manager.xml。

<?xml version="1.0" encoding="UTF-8"?>
<Context privileged="true" antiResourceLocking="false"
         docBase="${catalina.home}/webapps/manager">
             <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow=".*" />
</Context>

6、确保pom.xml文件中的远程发布插件中的配置的ip和端口和本机是通的,本人用的ali服务器,ali服务器的端口是需要手动开通的。

7、在执行远程打包时,远程服务器中的tomcat必须是启动状态。

注意:上述条件是必须通过的,否则远程发布的时候是肯定不行的。如果上述条件都达到了还不行,还请另行百度。

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值