利用ant編譯maven項目


目前把自己使用的ide從eclipse遷移到idea上

雖然開發爽了很多,但在部署過程中苦不堪言。因為每次都要跑maven的腳本clean&package,需要浪費很多時間。

所以自己寫了一個簡單的ant build腳本來編譯項目。

這個腳本適用於以下場景

  • 沒有修改spring配置文件(因為項目用到maven的antx插件來替換配置文件里面的變量,如果修改了run的時候就會出問題)
  • 之前已經使用過maven package編譯過項目,而且項目的exploded文件夾下的東西是完整的
  • 還沒想好

編譯需要maven-ant-tasks插件,請到右邊地址下載http://maven.apache.org/ant-tasks/download.html

build.xml文件

<?xml version="1.0" encoding="GBK"?>

<project name="daogou" basedir="." default="mail" xmlns:artifact="urn:maven-artifact-ant">


    <property file="build.properties"/>
    <property name="project.root" value="${basedir}"/>

    <description>${description}</description>
    <target name="clean"
            description="delete classes">
        <echo message=" --刪除lib文件夾和biz包以及protocal包開始 " />

        <delete file="${biz.jar}"/>
        <delete file="${protocal.jar}"/>
        <echo message=" --刪除lib文件夾和biz包以及protocal包結束 " />
    </target>

    <target name="clean lib" >
        <echo message=" --刪除lib文件夾開始 "/>
        <delete dir="${lib}"/>
        <echo message=" --刪除lib文件夾結束 "/>
    </target>


    <target name="jarClean">
        <echo message=" --刪除臨時編譯的文件開始 " />
        <delete dir="${ant.archiver.classes}"/>
        <mkdir dir = "${ant.archiver.classes}" />
        <echo message=" --刪除臨時編譯的文件結束 " />
    </target>

    <target name="init" depends="clean lib">
        <echo message="*****初始化,下載maven庫****"/>
        <echo message="${description}"/>
        <path id="maven-ant-tasks.classpath" path="${maven.ant.task.lib}"/>
        <typedef resource="org/apache/maven/artifact/ant/antlib.xml"
                 uri="urn:maven-artifact-ant"
                 classpathref="maven-ant-tasks.classpath"/>

        <artifact:pom id="maven.project" file="pom.xml"/>
        <artifact:pom id="maven.project.biz" file="biz/pom.xml"/>
        <artifact:pom id="maven.project.web" file="web/pom.xml"/>
        <artifact:pom id="maven.project.protocal" file="web/pom.xml"/>
        <artifact:dependencies pathId="maven.classpath" filesetid="maven.fileset">
            <pom refid="maven.project.web"/>
        </artifact:dependencies>
        <copy todir="${lib}">
            <fileset refid="maven.fileset"/>
            <!-- This mapper strips off all leading directory information -->
            <mapper type="flatten"/>
        </copy>
        <echo message="*****初始化,下載maven庫結束****"/>
    </target>
    <path id="class.path">
        <fileset dir="${lib}" includes="*.jar" />
    </path>
    <target name="compile protocal" depends="clean,jarClean">
        <echo message=" --編譯protocal包開始 " />
        <javac srcdir="${protocal.src}" destdir="${ant.archiver.classes}">
            <classpath refid="class.path"/>
        </javac>

        <jar destfile="${protocal.jar}"
             basedir="${ant.archiver.classes}"/>
        <echo message=" --編譯protocal包結束 " />
    </target>


    <target name="compile biz" depends="jarClean,compile protocal">
        <echo message=" --編譯biz包開始 " />
        <mkdir dir="${classes}"/>
        <javac srcdir="${biz.src}" destdir="${ant.archiver.classes}">
            <classpath refid="class.path"/>
        </javac>
        <jar destfile="${biz.jar}"
             basedir="${ant.archiver.classes}"/>
        <echo message=" --編譯biz包結束 " />
    </target>


    <target name="compile web" depends="compile biz">
        <echo message=" --編譯web開始 " />
        <mkdir dir="${classes}"/>
        <javac srcdir="${web.src}" destdir="${classes}">
            <classpath refid="class.path"/>
        </javac>
        <echo message=" --編譯web結束 " />
    </target>
    <!-- 如果你有修改pom文件,請使用這個編譯  -->
    <target name="main" >
        <antcall target="init"/>
        <antcall target="compile web"/>
    </target>
    <!-- 如果你沒有修改pom文件,請使用這個編譯  -->
    <target name="main without init" >
        <antcall target="compile web"/>
    </target>
</project>

 

build.properties文件

##描述
description=Mulou's build script for daogoudian

##項目根目錄
basedir= D:/projects/daogoudian
##webRoot地址
webRoot =${basedir}/web/target/exploded/daogou
##項目編譯之后的class地址
classes= ${webRoot}/WEB-INF/classes
##把你的class編譯到的臨時地址
ant.archiver.classes=${basedir}/web/target/ant-archiver/classes
##項目依賴包地址
lib=${webRoot}/WEB-INF/lib
##zip包地址
biz.jar=${lib}/daogou-biz-1.0-SNAPSHOT.jar
##protocal包地址
protocal.jar=${lib}/daogou-protocal-1.0-SNAPSHOT.jar
ant的maven插件地址
maven.ant.task.lib=  D:/tools/libs/maven-ant-tasks-2.1.3.jar
##biz代碼地址
biz.src=${basedir}/biz/src/main/java
##protocal代碼地址
protocal.src= ${basedir}/protocal/src/main/java
  ##web代碼地址
web.src= ${basedir}/web/src/main/java

使用idea跑ant build

main適用於修改了pom文件的情況

main without init 適用於沒有修改pom文件的情況

ant編譯只需要11秒

maven package花了我近兩分鍾

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM