利用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花了我近两分钟

原文地址:https://www.cnblogs.com/huamulou/p/2976124.html