Linux自动化项目部署

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

操作系统:centos6.5(服务器)   windows(客户端)

开发环境:javaIDE,SVN,码云

服务器环境:JDK,tomcat,SVN,apache ant,码云

项目部署思路:

1、windows环境下我们使用了idea进行了一个简单javaWeb项目的开发。

2、为了测试方便,这里我们使用码云作为我们的SVN服务器进行代码托管。

3、开发人员在idea上开发完成之后提交到码云。

4、我们编写自动化shell脚本使用SVN将代码从码云进行检出(手动检出一次,以后都是更新)。

5、自动化shell脚本调用ant对代码进行编译,打包,发布。

6、对tomcat服务器进行重启,使得新代码进行生效。

部署演练

1、开发javaWeb项目(省略)

2、发布项目到码云,并进行SVN管理

    https://blog.csdn.net/qq_28189423/article/details/93620253

3、部署服务器

安装jdk

    https://blog.csdn.net/qq_28189423/article/details/89434171

安装tomcat

    https://blog.csdn.net/qq_28189423/article/details/89435072

安装apache ant

    https://blog.csdn.net/qq_28189423/article/details/102622124

安装svn

    https://blog.csdn.net/qq_28189423/article/details/102621981

4、第一次对项目进行手工检出(因为以后都更新) 

svn checkout 路径信息 --username=用户名 --password=密码 本地路径

5、build.xml配置文件编写   

mkdir ~/buildconfig

vi ~/buildconfig/build.xml

注意:这部分配置信息需要按需进行修改。

<?xml version="1.0" encoding="UTF-8"?>
<project name="autoProj" default="deploy" basedir="/root/autoProj">  
    <property environment="env" />  
    <property name="webapp.name" value="autoProj"/>  
    <property name="catalina.home" value="/usr/apache-tomcat-7.0.50" />  
    <property name="dist.dir" value="/root/classDir" />   <!-- class编译路径 -->
    <property name="webRoot.dir" value="${basedir}/html" />  <!-- WEB-INF上一级 -->
    <property name="src.dir" value="${basedir}/src" />  <!-- 源码路径 -->
    <property name="lib.dir" value="${webRoot.dir}/WEB-INF/lib" />  <!-- lib路径 -->  
    <property name="build.dir" value="/root/classDir" />  <!-- class编译路径 -->
    <!-- 使用eclipse jdt进行编译,而不使用JDK编译  -->  

    <!-- 初始化classpath -->  
    <path id="project.classpath">  
        <fileset dir="${lib.dir}">  
            <include name="**/*.jar" />  
        </fileset>  
        <!-- 添加tomcat类路径 -->  
        <fileset dir="${catalina.home}/lib">  
            <include name="*.jar" />  
        </fileset>  

    </path>  

    <!-- get the source compile classpath in a printable form -->  
    <pathconvert pathsep="${line.separator}|   |-- "  
             property="echo.path.compile"  
             refid="project.classpath">  
    </pathconvert>  

    <!-- show classpath jars -->  
    <target name="print_classpath">  
        <echo message="|-- compile classpath"/>  
        <echo message="|   |"/>  
        <echo message="|   |-- ${echo.path.compile}"/>  
    </target>  


    <!-- 删除之前的目录结构 -->  
    <target name="clear" description="清理旧文件">  
        <delete dir="${build.dir}" />  
        <delete dir="${dist.dir}" />  
        <delete file="${catalina.home}/WebRoot/${webapp.name}.war" />  
        <delete dir="${catalina.home}/WebRoot/${webapp.name}" />  
    </target>  

    <!-- 创建目录结构 -->  
    <target name="init" depends="clear" description="创建初始化目录结构">  
        <mkdir dir="${build.dir}/classes" />  
        <mkdir dir="${dist.dir}" />  
    </target>  

    <!-- 编译java -->  
    <target name="compile" depends="init" description="编译java文件">  
        <echo message="begin compile..." />  
        <javac srcdir="${src.dir};${lib.dir}" destdir="${build.dir}/classes"   
            includeantruntime="false" nowarn="on"   
            source="1.6" target="1.6" deprecation="true" debug="true"   
            encoding="UTF-8" classpathref="project.classpath">
            <compilerarg line="-Xlint:unchecked" />  
            <!-- <classpath refid="classpath" /> -->  
        </javac>  
        <copy todir="${build.dir}/classes">  
            <fileset dir="${src.dir}">  
                <include name="**/*.xml" />  
                <include name="**/*.properties" />  
                <include name="**/*.sql" />  
            </fileset>   
        </copy>  
        <echo message="end compile..." />  
    </target>  

    <!-- 将class文件打成 jar包 -->  
    <!--    
        <target name="pack" depends="compile">   
            <jar jarfile="${build.dir}/${webapp.name}.jar">   
                <fileset dir="${build.dir}/classes">   
                    <include name="**/*.class"/>   
                </fileset>   
            </jar>   
        </target>   
    -->  

    <!-- 打成war包, 名称默认为 项目名 -->  
    <target name="war" depends="compile" description="将工程打成war包">  
        <echo message="begin war..." />  
        <war destfile="${dist.dir}/${webapp.name}.war" basedir="${webRoot.dir}"   
            webxml="${webRoot.dir}/WEB-INF/web.xml">  
            <lib dir="${lib.dir}" />  
            <classes dir="${build.dir}/classes" />  
            <fileset dir="${webRoot.dir}">  
                <include name="***.*" />  
            </fileset>  
        </war>  
        <echo message="end war..." />  
    </target>  

    <!-- copy war包 tomcat的deploy目录 -->  
    <target name="deploy" depends="war" description="部署项目">  
        <echo message="begin deploy..." />  
        <copy file="${dist.dir}/${webapp.name}.war" todir="${catalina.home}/webapps" />  
        <echo message="end deploy..." />  
    </target>
</project>

5、自动化脚本编写

vi ~/buildconfig/auto_build.sh

#~/autoProj  本地路径
svn update ~/autoProj

#调用ant  使用我们的配置文件进行编译
ant -buildfile ~/buildconfig/build.xml

#关闭tomcat
/usr/apache-tomcat-7.0.50/bin/shutdown.sh

#启动tomcat
/usr/apache-tomcat-7.0.50/bin/startup.sh

6、脚本赋权

chmod +x ~/buildconfig/auto_build.sh

7、执行脚本

~/buildconfig/auto_build.sh

到此案例部署完成。

猜你喜欢

转载自blog.csdn.net/qq_28189423/article/details/102624795