1. 程式人生 > >產品打包工具的製作,ant 編譯原始碼,打jar包,打tag,打war包,備份release版本等

產品打包工具的製作,ant 編譯原始碼,打jar包,打tag,打war包,備份release版本等

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow

也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!

               

1、  在進行打包工具的製作前,需要準備的軟體有:

   

svnant-1.3.1

作用是讓ant和svn相關聯

apache-ant-1.9.7

需要設定ant_home,path,我的配置是:

 

ANT_HOME = D:\kaipu\cms4all\app-tpl-pack\apache-ant-1.9.7

 

Path = %ANT_HOME%\bin

2、準備好上面的操作之後,找個英文的目錄,比如:D:\kaipu\cms4all\app-tpl-pack,在目錄下建立兩個檔案build.xml和build.properties,並把上面的svnant-1.3.1和apache-ant-1.9.7同樣放到相同目錄下,最後的目錄如下:

3、編寫build.xml檔案

<?xml version="1.0"  encoding="UTF-8"?>

 

<!--default="dist"表示當只執行ant的時候,預設執行下面的dist命令-->

 

<project name="tpl-release"  basedir="." default="dist">

 

     <!--表示build.xml中引入的變數是從build.properties中獲得的-->

 

         <property  file="build.properties" />

 

         <tstamp>

 

                   <format  property="TODAY" pattern="yyyy-MM-dd HH:mm:ss"  locale="zh" />

 

         </tstamp>

 

         <tstamp>

 

                   <format  property="date" pattern="yyyyMMdd" locale="zh"  />

 

         </tstamp>

 

         <!--ant要和svn相關聯,需要配置一個svnant,下面表示相關的內容依賴svnant-->

 

         <path  id="svn.classpath">

 

                   <fileset  dir="${svnant.dir}/lib">

 

                            <include  name="**/*.jar" />

 

                   </fileset>

 

         </path>

 

         <path  id="depends.classpath">

 

                   <fileset  dir="${java.depends.lib.dir}">

 

                            <include  name="**/*.jar" />

 

                   </fileset>

 

         </path>

 

         <!--引用 svnantlib.xml ,其中定義了ANT的SVN命令,見下方詳解。-->

 

         <typedef  resource="org/tigris/subversion/svnant/svnantlib.xml"  classpathref="svn.classpath" />

 

         <!--<taskdef  name="yui-compressor"  classname="net.noha.tools.ant.yuicompressor.tasks.YuiCompressorTask"  classpathref="depends.classpath"></taskdef>-->

 

 

 

         <!--通過ant clean命令能夠清除dir.source、dir.compile和dir.release這些資料夾-->

 

         <target  name="clean">

 

                   <echo  message="delete all dirs ..." />

 

                   <delete  dir="${dir.source}" />

 

                   <delete  dir="${dir.compile}" />

 

                   <delete  dir="${dir.release}" />

 

         </target>

 

 

 

         <!--定義svnSetting ,新的設定SVN屬性方式-->

 

         <!--  通過JAVAHL模式(即設定為TRUE),而不是SVNKIT模式 -->

 

         <svnSetting  id="svn.settings" svnkit="true" javahl="false"  username="${svn.user}" password="${svn.pass}" />

 

         <svnSetting  id="svn.settings.1" svnkit="true"  javahl="false" username="${svn.user}"  password="${svn.pass}" failonerror="false" />

 

 

 

         <!--  匯出原始碼,匯出的過程中是從svn上checkout下來的,執行的命令是:ant  export-source-->

 

         <target  name="export-source">

 

                   <echo  message="export source" />

 

                   <mkdir  dir="${dir.source}" />

 

                   <svn  refid="svn.settings">

 

                            <export  srcUrl="${trunk.url}/${proj.app-tpl-api}"  destPath="${dir.source}/${proj.app-tpl-api}"  revision="HEAD" />                  

 

                            <export  srcUrl="${trunk.url}/${proj.app-tpl-impl}"  destPath="${dir.source}/${proj.app-tpl-impl}"  revision="HEAD"/>

 

                            <export  srcUrl="${trunk.url}/${proj.app-tpl-webapp}"  destPath="${dir.source}/${proj.app-tpl-webapp}"  revision="HEAD"/>

 

                   </svn>

 

         </target>

 

        

 

         <!--從tag版本建立tag,在svn上打上標記-->

 

         <target  name="create-tag-from-trunk">

 

                   <svn  refid="svn.settings.1">

 

                            <delete  url="${tags.url}/${tag.version.label}" message="tags  duplicate!" />

 

                   </svn>

 

                   <echo  message="create tags ${tag.version.label}" />

 

                   <svn  refid="svn.settings">

 

                            <mkdir  url="${tags.url}/${tag.version.label}" message="Tag created by  tuzq on ${TODAY}" />

 

                            <copy  srcUrl="${trunk.url}/${proj.app-tpl-api}"  destUrl="${tags.url}/${tag.version.label}" message="Tag  created by toto on ${TODAY}" />

 

                            <copy  srcUrl="${trunk.url}/${proj.app-tpl-impl}"  destUrl="${tags.url}/${tag.version.label}" message="Tag  created by toto on ${TODAY}" />

 

                            <copy  srcUrl="${trunk.url}/${proj.app-tpl-webapp}"  destUrl="${tags.url}/${tag.version.label}" message="Tag  created by toto on ${TODAY}" />

 

                   </svn>

 

         </target>

 

 

 

         <!--編譯api程式碼-->

 

         <target  name="compile-app-tpl-api">

 

                   <echo  message="compile app-tpl-api project" />

 

                   <!--javac,編譯,對應java中的javac命令,其中srcdir定義原始檔路徑  destdir定義編譯檔案路徑

 

                   includeantruntime作用是指定編譯任務是否包含ant的classpath,可有可無,不影響編譯,但不寫可能會出現警告,為了眼不見心不煩,加上吧

 

                   -->

 

                   <delete  dir="${dir.compile}/${proj.app-tpl-api}"/>

 

                   <mkdir  dir="${dir.compile}/${proj.app-tpl-api}"/>

 

                   <javac  srcdir="${dir.source}/${proj.app-tpl-api}${java.source.path}"  destdir="${dir.compile}/${proj.app-tpl-api}"  debug="${option.compile.debug}"  optimize="${option.compile.optimize}"  deprecation="${option.compile.deprecation}"  encoding="${option.compile.encoding}"  target="${option.compile.target}"  verbose="${option.compile.verbose}"  includeantruntime="false">

 

                            <classpath>

 

                                <!--依賴的lib包下的jar檔案-->

 

                                     <fileset  dir="${java.depends.lib.dir}">

 

                                               <include  name="**/*.jar" />

 

                                     </fileset>

 

                                     <!--依賴的tomcat中的jar檔案-->

 

                                     <fileset  dir="${tomcat.lib.dir}">

 

                                               <include  name="**/*.jar" />

 

                                     </fileset>

 

                            </classpath>

 

                   </javac>

 

         </target>

 

        

 

         <!--通過ant命令進行打包,打成的包是:app-tpl-api-1.0-SNAPSHOT.jar-->

 

         <target  name="app-tpl-api-jar" description="make jar file">

 

                   <delete  dir="${dir.compile}/${proj.app-tpl-api}/${tpl.jar.app-tpl-api}"/>

 

                   <property  name="app-tpl-api-jar-filename"  value="${dir.compile}/${proj.app-tpl-api}/${tpl.jar.app-tpl-api}"  />

 

                   <jar  jarfile="${app-tpl-api-jar-filename}"  basedir="${dir.compile}/${proj.app-tpl-api}">

 

                       <!--為jar包指定manifest,如果jar包不需要打成runnable的形式,manifest可以不要-->

 

                   </jar>

 

         </target>

 

        

 

         <!--編譯app-tpl-impl.jar檔案-->

 

         <target  name="compile-app-tpl-impl">

 

                   <echo  message="compile app-tpl-api project" />

 

                   <!--javac,編譯,對應java中的javac命令,其中srcdir定義原始檔路徑  destdir定義編譯檔案路徑

 

                   includeantruntime作用是指定編譯任務是否包含ant的classpath,可有可無,不影響編譯,但不寫可能會出現警告,為了眼不見心不煩,加上吧

 

                   -->

 

                   <delete  dir="${dir.compile}/${proj.app-tpl-impl}"/>

 

                   <copy  file="${dir.compile}/${proj.app-tpl-api}/${tpl.jar.app-tpl-api}"  todir="${java.depends.lib.dir}"/>

 

                   <mkdir  dir="${dir.compile}/${proj.app-tpl-impl}"/>

 

                   <copydir  dest="${dir.compile}/${proj.app-tpl-impl}"  src="${dir.source}/${proj.app-tpl-impl}${proj.app-tpl-impl-mapper}">

 

                            <include  name="sqlmaps/**" />

 

                   </copydir>

 

                   <javac  srcdir="${dir.source}/${proj.app-tpl-impl}${java.source.path}"  destdir="${dir.compile}/${proj.app-tpl-impl}"  debug="${option.compile.debug}" optimize="${option.compile.optimize}"  deprecation="${option.compile.deprecation}"  encoding="${option.compile.encoding}"  target="${option.compile.target}"  verbose="${option.compile.verbose}" includeantruntime="false">

 

                            <classpath>

 

                                     <fileset  dir="${java.depends.lib.dir}">

 

                                               <include  name="**/*.jar" />

 

                                     </fileset>

 

                                     <fileset  dir="${tomcat.lib.dir}">

 

                                               <include  name="**/*.jar" />

 

                                     </fileset>

 

                            </classpath>

 

                   </javac>

 

         </target>

 

        

 

         <!--打包命令,打成的包是:app-tpl-impl-1.0-SNAPSHOT.jar-->

 

         <target  name="app-tpl-impl-jar" description="make jar file">

 

                   <delete  dir="${dir.compile}/${proj.app-tpl-impl}/${tpl.jar.app-tpl-impl}"/>

 

                   <property  name="app-tpl-impl-jar-filename"  value="${dir.compile}/${proj.app-tpl-impl}/${tpl.jar.app-tpl-impl}"  />

 

                   <jar  jarfile="${app-tpl-impl-jar-filename}"  basedir="${dir.compile}/${proj.app-tpl-impl}">

 

                       <!--為jar包指定manifest,如果jar包不需要打成runnable的形式,manifest可以不要-->

 

                   </jar>

 

         </target>

 

        

 

         <!--  需要引數proj.name,主要是編譯web工程 -->

 

         <target  name="compile-app-tpl-webapp">

 

                   <echo  message="compile web project ${tpl.war.app-tpl-webapp}" />

 

                   <property  name="targetpath"  value="${dir.compile}/${proj.app-tpl-webapp}" />

 

                   <property  name="sourcepath"  value="${dir.source}/${proj.app-tpl-webapp}" />

 

                   <!--  編譯swf web工程 -->

 

                   <mkdir  dir="${targetpath}" />

 

                   <mkdir  dir="${targetpath}WEB-INF/classes" />

 

                   <copy  file="${dir.compile}/${proj.app-tpl-impl}/${tpl.jar.app-tpl-impl}"  todir="${java.depends.lib.dir}"/>

 

                   <copy  todir="${targetpath}" overwrite="true"  preservelastmodified="true">

 

                            <fileset  dir="${sourcepath}${web.source.path}" />

 

                   </copy>

 

                  

 

                   <javac  srcdir="${sourcepath}${java.source.path}"  destdir="${targetpath}WEB-INF/classes"

 

                            debug="${option.compile.debug}"  optimize="${option.compile.optimize}"  deprecation="${option.compile.deprecation}"

 

                            encoding="${option.compile.encoding}"

 

                            target="${option.compile.target}"  verbose="${option.compile.verbose}"  includeantruntime="false">

 

                            <classpath>

 

                                     <fileset  dir="${java.depends.lib.dir}">

 

                                               <include  name="**/*.jar" />

 

                                     </fileset>

 

                                     <fileset  dir="${tomcat.lib.dir}">

 

                                               <include  name="**/*.jar" />

 

                                     </fileset>

 

                            </classpath>

 

                   </javac>

 

                  

 

                   <copy  todir="${targetpath}WEB-INF/classes" overwrite="true"  preservelastmodified="true">

 

                            <fileset  dir="${sourcepath}${config.source.path}" />

 

                   </copy>

 

                   <copy  todir="${targetpath}WEB-INF/lib" overwrite="true"  preservelastmodified="true">

 

                       <fileset  dir="${java.depends.lib.dir}" />

 

                   </copy>

 

         </target>

 

        

 

         <!--  需要war的名稱 war.name -->

 

         <target  name="war.app-tpl-webapp">

 

                   <delete  dir="${dir.compile}/${tpl.war.app-tpl-webapp}"/>

 

                   <war  warfile="${dir.compile}/${tpl.war.app-tpl-webapp}"  webxml="${dir.compile}/${proj.app-tpl-webapp}/WEB-INF/web.xml">

 

                            <fileset  dir="${dir.compile}/${proj.app-tpl-webapp}"/>

 

                   </war>

 

         </target>

 

        

 

         <!--  釋出相關工程及依賴工具包,通過ant dist命令,可以一次性執行清除、checkout原始碼、打tag、編譯、打jar、打war包等操作 -->

 

         <target  name="dist"  depends="clean,export-source,create-tag-from-trunk,compile-app-tpl-api,app-tpl-api-jar,compile-app-tpl-impl,app-tpl-impl-jar,compile-app-tpl-webapp,war.app-tpl-webapp">

 

         <!--<target  name="dist"  depends="compile-app-tpl-api,app-tpl-api-jar,compile-app-tpl-impl,app-tpl-impl-jar,compile-app-tpl-webapp,war.app-tpl-webapp">-->

 

         <!--  <target name="dist"  depends="create-tag-from-trunk">-->

 

                   <!--  target name="dist" -->

 

                   <!--  編譯 -->

 

                   <!--  compile-web-proj proj.swf,proj.jod,proj.imagemagick-->

 

         </target>

 

        

 

     <!--將需要relase的東西copy到dist目錄下-->

 

         <target  name="copy-to-dist">

 

                   <mkdir  dir="${dir.release}/${app-tpl.name}" />

 

                   <copy  todir="${dir.release}/${app-tpl.name}" overwrite="true"  preservelastmodified="true" includeEmptyDirs="false">

 

                            <!--  copy war檔案  -->

 

                            <fileset  file="${dir.compile}/${tpl.war.app-tpl-webapp}" />

 

                   </copy>

 

         </target>

 

        

 

         <!--將所有的dist裡面的東西放到svn上的release包中-->

 

         <target  name="commit-release">

 

                   <!--  提交到release庫中 -->

 

                   <echo  message="commit to release repository."/>

 

                   <svn  refid="svn.settings.1">

 

                            <delete  url="${release.url}/${tag.version.label}" message="Release  duplicate!"/>

 

                   </svn>

 

                   <svn  refid="svn.settings">

 

                            <mkdir  url="${release.url}${tag.version.label}" message="Release  created by tuzq on ${TODAY}" />

 

                            <import  url="${release.url}${tag.version.label}"  path="${dir.release}" message="Release created by tuzq on  ${TODAY}" />

 

                   </svn>

 

         </target>

 

</project>

4、編寫build.properties檔案

#svn的相關屬性

 

svn.user=這裡填寫svn的使用者名稱

 

svn.pass=這裡填寫svn的使用者名稱和密碼

 

svnant.dir=D:/kaipu/cms4all/app-tpl-pack/svnant-1.3.1

 

 

 

#打tag時的版本資訊

 

tag.version.label=app-tpl-v1.0.0

 

branch.version.label=app-tpl-v1.0.0

 

 

 

java.depends.lib.dir=./lib

 

#java.compile.lib.dir=D:/app/alfresco/tomcat-7.0.32/webapps/alfresco/WEB-INF/lib

 

tomcat.lib.dir=./tomcatlib

 

 

 

#svn伺服器上的相關程式碼位置

 

trunk.url=這裡放svn上trunk版本的url地址

 

tags.url=這裡放svn上tag版本的url地址

 

branches.url=這裡放branches版本的url地址

 

release.url=這裡放置release版本的url地址

 

#相關工程的資訊

 

proj.app-tpl-api=app-tpl-api/

 

proj.app-tpl-impl=app-tpl-impl/

 

proj.app-tpl-impl-mapper=src/main/resources

 

proj.app-tpl-webapp=app-tpl-webapp/

 

 

 

tpl.jar.app-tpl-api=app-tpl-api-1.0-SNAPSHOT.jar

 

tpl.jar.app-tpl-impl=app-tpl-impl-1.0-SNAPSHOT.jar

 

tpl.war.app-tpl-webapp=app-tpl-webapp.war

 

 

 

#本地編譯及釋出的目錄結構

 

dir.source=./source

 

 

 

java.source.path=src/main/java

 

web.source.path=src/main/webapp

 

config.source.path=src/main/resources

 

 

 

#java的編譯選項

 

option.compile.encoding=utf-8

 

option.compile.deprecation=on

 

#將這個開啟的時候,才認SpringMVC中的註解

 

option.compile.debug=true

 

option.compile.optimize=on

 

option.compile.target=1.7

 

#通過下面的方式實現列印資訊

 

option.compile.verbose=true

 

 

 

dir.compile=./build

 

#目錄結構

 

#---------------------------------------------------------------

 

dir.release=./dist

 

app-tpl.name=app-tpl

 

           

給我老師的人工智慧教程打call!http://blog.csdn.net/jiangjunshow

這裡寫圖片描述