Grails加载classpath两次的Ant任务

时间:2013-12-30 20:12:15

标签: grails ant

我正在使用macrodef中的默认grails integrate-with --ant。作为我的ant任务的一部分,我需要执行cleanwar和一些misc。文件操作。我得到的问题是,在clean任务运行后,war任务无法加载带有java.lang.reflect.InvocationTargetException的grails。将ant日志记录更改为debug会产生...jline_.dll already loaded in another classloader'错误。下面是我的蚂蚁文件。

<project xmlns:ivy="antlib:org.apache.ivy.ant" name="ant_project" default="normal">
    <property environment="env"/>
    <property file="ant/ant-global.properties" />
    <property name="IsCoverageBuild" value="false"/>
    <property name="DisplayCopiedFilesInWar" value="false"/>
    <property name="warFilename" value="PAC.war"/>
    <property name="ivy.install.version" value="2.2.0" />
    <condition property="ivy.home" value="${env.IVY_HOME}">
      <isset property="env.IVY_HOME" />
    </condition>
    <property name="ivy.home" value="${user.home}/.grails/ivy" />
    <property name="ivy.jar.dir" value="${ivy.home}/lib" />
    <property name="ivy.jar.file" value="${ivy.jar.dir}/ivy-${ivy.install.version}.jar" />

    <target name="download-ivy" unless="offline">
        <available file="${ivy.jar.file}" property="ivy.available"/>
        <antcall target="-download-ivy" />
    </target>

    <target name="-download-ivy" unless="ivy.available">
        <mkdir dir="${ivy.jar.dir}"/>
        <!-- download Ivy from web site so that it can be used even without any special installation -->
        <get src="http://archive.apache.org/dist/ant/ivy/${ivy.install.version}/apache-ivy-${ivy.install.version}-bin.zip"
            dest="${ivy.home}/ivy.zip" usetimestamp="true" verbose="true"/>
        <unzip src="${ivy.home}/ivy.zip" dest="${ivy.jar.dir}">
           <patternset>
                <include name="**/*.jar"/>
            </patternset>
            <mapper type="flatten"/>
        </unzip>
    </target>

    <target name="init-ivy" depends="download-ivy" unless="ivy.lib.path">
      <!-- try to load ivy here from ivy home, in case the user has not already dropped
              it into ant's lib dir (note that the latter copy will always take precedence).
              We will not fail as long as local lib dir exists (it may be empty) and
              ivy is in at least one of ant's lib dir or the local lib dir. -->
        <path id="ivy.lib.path">
            <fileset dir="${ivy.jar.dir}" includes="*.jar"/>
        </path>
        <taskdef resource="org/apache/ivy/ant/antlib.xml"
                 uri="antlib:org.apache.ivy.ant" classpathref="ivy.lib.path"/>
    </target>


    <property name="lib.dir" value="${basedir}/lib"/>

    <macrodef name="grails">
        <attribute name="script"/>
        <attribute name="args" default="" />
        <sequential>
            <grailsTask script="@{script}" args="@{args}" classpathref="grails.classpath">
                <compileClasspath refid="compile.classpath"/>
                <testClasspath refid="test.classpath"/>
                <runtimeClasspath refid="app.classpath"/>
            </grailsTask>
        </sequential>
    </macrodef>

    <!-- =================================
          target: resolve
         ================================= -->
    <target name="-resolve" description="--> Retrieve dependencies with ivy" depends="init-ivy">
        <ivy:retrieve pattern="${lib.dir}/[conf]/[artifact]-[revision].[ext]"/>
    </target>

    <target name="-init-grails" depends="-resolve">
        <path id="grails.classpath">
            <fileset dir="${lib.dir}/build"/>
            <fileset dir="${lib.dir}"/>
        </path>

        <taskdef name="grailsTask"
                 classname="grails.ant.GrailsTask"
                 classpathref="grails.classpath"/>
    </target>

    <target name="deps-report" depends="-resolve" description="--> Generate report of module dependencies.">
        <ivy:report conf="*"/>
    </target>

    <!-- =================================
          target: clean
         ================================= -->
    <target name="clean" description="--> Cleans a Grails application">
        <mkdir dir="${lib.dir}/build"/>
        <mkdir dir="${lib.dir}/compile"/>
        <mkdir dir="${lib.dir}/runtime"/>
        <mkdir dir="${lib.dir}/test"/>
        <delete failonerror="true">
        <fileset dir="${lib.dir}/build" includes="*/"/>
        <fileset dir="${lib.dir}/compile" includes="*/"/>
        <fileset dir="${lib.dir}/runtime" includes="*/"/>
        <fileset dir="${lib.dir}/test" includes="*/"/>
        </delete>
        <antcall target="--grails-clean"/>
    </target>

    <!-- extra target to avoid errors on Windows because libs on classpath can not be deleted -->
    <target name="--grails-clean" depends="-init-grails">
        <grails script="Clean"/>
    </target>

    <!-- =================================
          target: compile
         ================================= -->
    <target name="compile" depends="-init-grails" description="--> Compiles a Grails application">
        <grails script="Compile"/>
    </target>

    <!-- =================================
          target: war
         ================================= -->
    <target name="war" depends="-init-grails" description="--> Creates a WAR of a Grails application">
        <grails script="War"/>
    </target>

    <!-- =================================
          target: test
         ================================= -->
    <target name="test" depends="-init-grails" description="--> Run a Grails applications unit tests">
        <grails script="TestApp"/>
    </target>

    <!-- =================================
          target: run
         ================================= -->
    <target name="run" depends="-init-grails" description="--> Runs a Grails application using embedded Jetty">
        <grails script="RunApp"/>
    </target>
    <target name="normal" depends="-init-grails,--grails-clean,war">
    </target>
</project>

1 个答案:

答案 0 :(得分:0)

我最终使用的解决方案是使用<exec>而不是提供的macrodef。以下是我的grails命令现在的几个例子。

<!--CLEAN-->
<exec executable="cmd">
       <arg value="/c"/>
       <arg path="${env.GRAILS_HOME}/bin/grails.bat"/>
       <arg value="clean"/>
</exec>

    <!--AddProxy-->
<exec executable="cmd">
        <arg value="/c"/>
        <arg path="${env.GRAILS_HOME}/bin/grails.bat"/>
        <arg value="AddProxy"/>
        <arg value="myProxy"/>
        <arg value="--host=myproxyhost"/>
        <arg value="--port=80"/>
</exec>

  <!--SetProxy-->
<exec executable="cmd">
        <arg value="/c"/>
        <arg path="${env.GRAILS_HOME}/bin/grails.bat"/>
        <arg value="SetProxy"/>
        <arg value="myProxy"/>
</exec>