简体   繁体   English

蚂蚁构建文件问题

[英]Ant build file issue

Hi I have a problem with ant build.xml and it works but sometimes not the first time or works on some computers and others not 嗨,我在使用ant build.xml时遇到问题,它可以工作,但有时不是第一次,或者在某些计算机上不能工作

so here it is: 所以这里是:

<project name="My-java-api" default="dist-api" basedir=".">

<description>
    Java API Buildfile
</description>

<property name="src" location="src"/>
<property name="build" location="build"/>
<property name="dist"  location="dist"/>
<property name="libs"  location="libs"/>

<!--if we don't remove folders, when we call compile-api
and classes have already been built, it doesn't build again-->

<target name="-init-api" depends="clean"
        description="Create folders libs and build">
    <mkdir dir="${build}"/>
    <mkdir dir="${libs}"/>
    <mkdir dir="${dist}"/>
</target>

 <!-- Here I call another buildfile of submodule located at the tree indicated I am 
 sure the other buildfile works perfect -->

<target name="-pre-build-api" depends="-init-api"
        description="Create jelly jar and copy it to libs folder">
    <ant dir="../Libraries/jelly/core/"
         antfile="build.xml"
         target="standalone-jar"/>
    <copy todir="${libs}">
        <fileset
                dir="../Libraries/jelly/core/dist"
                includes="jelly-standalone*.jar" />
    </copy>
</target>

 <!--so now I create this classpath to use for making jar-->

<path id="lib.classpath">
    <fileset dir="${libs}" includes="**/*.jar"/>
</path>

 <!--I compile source code including the jar that I have just copied to libs-->
<target name="compile-api" depends="-pre-build-api" >
    <javac srcdir="${src}"
           includeantruntime="false"
           destdir="${build}"
           classpathref="lib.classpath">
    </javac>
</target>
<!-- here i make jar with the classes and using the jar from external project,
I want just one jar for everything  -->

<target name="dist-api" depends="compile-api" >

    <jar jarfile="${dist}/name-java-api-0.1.jar" basedir="${build}" >
        <zipgroupfileset dir="${libs}" includes="**/*.jar" />
    </jar>
</target>

<target name="clean"
        description="clean up" >
    <delete dir="${build}"/>
    <delete dir="${dist}"/>
    <delete dir="${libs}"/>
</target>

EDIT: failonerror="false" added to all the delete lines 编辑:failonerror =“ false”添加到所有删除行

I am not used to ant and I have been trying and trying and it just doesn't quite work. 我不习惯蚂蚁,而且我一直在尝试,这并不起作用。 I wish I could just use command line but can't. 我希望我可以只使用命令行,但不能。 Strangely most of the time if run it 2 times it works but the first time really weird staff happen: either doesn't compile, either classes are duplicated. 奇怪的是,大多数情况下,如果将其运行两次就可以了,但是第一次真正奇怪的工作人员就发生了:要么不编译,要么重复两个类。 What could be the reason? 可能是什么原因? thanks a lot 非常感谢

Can you explain why it might fail? 您能解释一下为什么它会失败吗? A quick look, and I see that your compile-api task is dependent upon your lib.classpath task, but it isn't included in compile-api 's dependences. 快速浏览一下,我发现您的compile-api任务依赖于lib.classpath任务,但未包含在compile-api的依赖关系中。 That can cause a build script to work sometimes and not others. 这可能会导致构建脚本有时而不是其他人运行。

Also, lib.classpath is dependent upon the lib directory being created, so it should also be dependent upon -init-api . 另外, lib.classpath依赖于所创建的lib目录,因此它也应依赖于-init-api

And, you should never ever have anything depend upon clean . 而且,您永远都不应依赖清洁 Ant builds are setup, so they don't have to execute unnecessary steps. 已经设置了Ant构建,因此它们不必执行不必要的步骤。 For example, if you change just one source file, only that source file gets recompiled. 例如,如果仅更改一个源文件,则仅重新编译该源文件。 You break the whole idea of a build script forcing a clean each time a build is done. 您打破了构建脚本的整个想法,每次执行构建时都要强​​制执行清除操作。


Looking a bit closer at your build.xml, I realize there are some other issues. 仔细查看您的build.xml,我发现还有其他问题。

Here's a build.xml with corrected dependencies. 这是一个带有更正依赖性的build.xml

<project name="My-java-api" default="dist-api" basedir=".">

    <description>
        Java API Buildfile
    </description>

    <property name="src" location="src"/>
    <property name="build" location="build"/>
    <property name="dist"  location="dist"/>
    <property name="libs"  location="libs"/>

    <!--if we don't remove folders, when we call compile-api -->
    <!-- and classes have already been built, it doesn't build again-->

    <target name="-init-api"
        description="Create folders libs and build">
        <mkdir dir="${build}"/>
        <mkdir dir="${libs}"/>
        <mkdir dir="${dist}"/>
    </target>

    <!-- Here I call another buildfile of submodule located at the tree indicated I am -->
    <!--sure the other buildfile works perfect -->

    <target name="-pre-build-api" depends="-init-api"
        description="Create jelly jar and copy it to libs folder">
        <ant dir="../Libraries/jelly/core/"
            antfile="build.xml"
            target="standalone-jar"/>
        <copy todir="${libs}">
            <fileset
                dir="../Libraries/jelly/core/dist"
                includes="jelly-standalone*.jar" />
        </copy>
    </target>

    <!--so now I create this classpath to use for making jar-->

    <target name="lib.classpath"
        depends="-pre-build-api">
        <path id="lib.classpath"
            depends="-pre-build-api">
            <fileset dir="${libs}" includes="**/*.jar"/>
        </path>
    </target>

    <!--I compile source code including the jar that I have just copied to libs-->
    <target name="compile-api"
        depends="lib.classpath" >
        <javac srcdir="${src}"
            includeantruntime="false"
            destdir="${build}"
            classpathref="lib.classpath">
        </javac>
    </target>
    <!-- here i make jar with the classes and using the jar from external project,
    I want just one jar for everything  -->

    <target name="dist-api"
        depends="compile-api" >

        <jar jarfile="${dist}/name-java-api-0.1.jar" basedir="${build}" >
            <zipgroupfileset dir="${libs}" includes="**/*.jar" />
        </jar>
    </target>

    <target name="clean"
        description="clean up" >
        <delete dir="${build}"/>
        <delete dir="${dist}"/>
        <delete dir="${libs}"/>
    </target>
</project>

NOTE : dist-api depends upon compile-api which depends upon lib.classpath which depends upon pre-build-api which _depends upon -init.api . 注意dist-api 取决于 compile-api ,后者取决于 lib.classpath lib.classpath 取决于 _depends -init.api pre-build-api There is nothing dependent upon clean . 没有什么依赖clean

I think it's going to complain if the directories are not there, as would be the case the first time you run it. 我认为这会抱怨目录是否不存在,就像您第一次运行它的情况一样。 You might want to add a failonerror="false" attribute to the delete tasks. 您可能想将failonerror =“ false”属性添加到删除任务。

For future reference, ant is not really used that much anymore for building Java - most people have gone over to maven. 为了将来参考,ant在构建Java时不再使用太多了-大多数人都转而使用maven。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM