<ivy:cachepath>是慢速的,如何避免每次构建都这样做并提高常春藤缓存路径性能?

时间:2018-12-04 22:51:14

标签: java performance ant ivy

在此行的每次构建,javac步骤的依赖库解析速度降低了4秒钟:

    <target name="compile" depends="bootstrap">
        <ivy:cachepath pathid="classpath"/> <!-- SLOW -->

我本质上想缓存ivy:cachepath的结果,所以我不必每次构建都这样做。我希望${classpath}存在不到0.2秒

上下文: 这是一个香草蚂蚁build.xml,它引导常春藤,使用ivy.xml文件列出标准的maven依赖项,然后使用类路径中的这些lib调用javac

    <property name="ivy.install.version" value="2.4.0"/>
    <property name="ivy.jar.dir" value="${basedir}/ivy"/>
    <property name="ivy.jar.file" value="${ivy.jar.dir}/ivy.jar"/>
    <property name="build.dir" value="build"/>
    <property name="src.dir" value="src"/>
    <target name="bootstrap" description="--> install ivy">
        <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>

    <!-- Download any missing libs -->
    <target name="install" depends="bootstrap">
        <!-- install ivy.jar -->
        <mkdir dir="${ivy.jar.dir}"/>
        <echo message="installing ivy..."/>
        <get src="https://repo1.maven.org/maven2/org/apache/ivy/ivy/${ivy.install.version}/ivy-${ivy.install.version}.jar" dest="${ivy.jar.file}" usetimestamp="true"/>
        <!-- Downloads all the libraries from maven central via ivy.xml and saves them to classpath-->
        <ivy:settings file="ivysettings.xml"/>
        <ivy:resolve/>
        <ivy:retrieve sync="true"
                      type="jar,bundle"/>
        <ivy:cachepath pathid="classpath"/>
    </target>

  <target name="clean" depends="bootstrap">
        <delete dir="${build_dir}"/>
        <delete dir="./lib"/>
        <delete dir="./bin"/>
        <ivy:cleancache/>
  </target>


    <!-- Compile -->
    <target name="compile" depends="bootstrap">
        <ivy:cachepath pathid="classpath"/>
        <ivy:settings file="ivysettings.xml"/>
        <mkdir dir="${build_dir}"/>
        <javac encoding="ISO-8859-1"
               includeantruntime="false"
               srcdir="${src_dir}"
               destdir="${build_dir}"
               classpathref="classpath"
               debug="on">
            <!--<withKotlin/> &lt;!&ndash; If this is undefined, run "Build" target first. &ndash;&gt;-->
        </javac>
        <!--<kotlinc src="${src_dir}/TestSuite/TestSuite.java" output="${build_dir}/${jar_name}.jar"/>-->

        <delete file="${dist_dir}/${jar_name}.jar"/>

        <pathconvert property="classpath.name" pathsep=" ">
            <path refid="classpath" />
            <mapper>
                <chainedmapper>
                    <flattenmapper />
                    <globmapper from="*.jar" to="../lib/*.jar" />
                </chainedmapper>
            </mapper>
        </pathconvert>

        <echo message="classpath.name : ${classpath.name} " />
        <jar destfile="bin/${jar_name}.jar" basedir="build" includes="**/*.class lib/*">
            <manifest>
                <attribute name="Main-Class" value="${main-class}"/>
                <attribute name="Class-Path" value="${classpath.name}"/>
            </manifest>
        </jar>
    </target>

1 个答案:

答案 0 :(得分:0)

这可能是一种愚蠢的方法,但是我只是通过将类路径写入磁盘并将构建时间缩短一半来强制执行解决方案:

通过以下方式写入:

    <pathconvert property="classpath">
        <path refid="classpath" />
    </pathconvert>
    <propertyfile file="my.properties" comment="My properties">
        <entry key="classpath" value="${classpath}"/>
    </propertyfile>

阅读方式:

    <loadproperties srcFile="my.properties"/>

通过以下方式使用:

    <javac classpath="${classpath}" ... >

<echo file=任务的执行速度甚至更快