简体   繁体   English

缺少依赖项而无法建立ANT

[英]Fail ANT Build On Missing Dependencies

I have come across the same issue as this Proper usage of Apache Commons Configuration ie Commons Lang is not included as a dependency. 我遇到了与正确使用Apache Commons Configuration相同的问题,即Commons Lang不作为依赖项包括在内。

Although eclipse and IDEA both pick it up, javac from ANT does not. 尽管eclipse和IDEA都选择了它,但ANT中的javac却没有。

<javac debug="true" destdir="${build.classes.dir}" srcdir="${src.dir}" includeantruntime="false">
  <classpath refid="build.classpath"/>
</javac>

I want the build server to be able to pick up these dependency issues and fail the build if someone has missed them out. 我希望构建服务器能够解决这些依赖问题,如果有人错过了它们,则构建失败。

Add a task as below 如下添加任务

<fail  message="Missing the commons lang jar dependency.">
  <condition>
     <not>  
     <available file="common-lang.jar"/>
     </not>
  </condition>
</fail>

Ant fail task link: https://ant.apache.org/manual/Tasks/fail.html 蚂蚁失败任务链接: https : //ant.apache.org/manual/Tasks/fail.html

Use ivy to manage your builds 3rd party dependencies. 使用常春藤来管理您的构建第三方依赖性。

<ivy:cachepath pathid="build.classpath">
    <dependency org="commons-lang" name="commons-lang" rev="2.6" conf="default"/>
</ivy:cachepath>

By default ivy will download (and cache) from the Maven Central repository and is aware of transitive dependencies (dependencies of dependencies). 默认情况下, ivy将从Maven Central存储库下载(和缓存),并且知道传递依赖关系(依赖关系的依赖关系)。

Suresh Koya is close, but there's a couple of issues: Suresh Koya接近了,但是有两个问题:

  • Suresh's way will only work if commons-lang.jar is actually in the current directory. 只有在commons-lang.jar实际上在当前目录中时,Suresh的方法才有效。
  • It also won't work if the jar is called something like commons-lang-2.6.jar . 如果jar被称为commons-lang-2.6.jar类的东西,它也将commons-lang-2.6.jar

The first can be fixed by adding a <filepath> sub-entity to <available> . 可以通过在<available>添加<filepath>子实体来解决第一个问题。 This gives the <available> condition a directory path to search for the jar. 这为<available>条件提供了搜索jar的目录路径。 However, it won't work if the user downloaded commons-lang with the version number on it (as you would from a Maven repository). 但是,如果用户下载了带有版本号的commons-lang (与从Maven存储库中一样),它将无法正常工作。

To get around this issue, you need to create a path resource using something like <fileset> and then checking to see if you picked up any version of the commons-lang jarfile. 要解决此问题,您需要使用<fileset>类的方法创建路径资源,然后检查是否选择了commons-lang jarfile的任何版本。 You can see below that I am looking for any file that begins with commons-lang and has a .jar suffix. 您可以在下面看到我正在寻找以commons-lang开头且后缀为.jar任何文件。

After I set my resource, I can use a <resoucecount> condition to see if I picked up any version of commons-lang : 设置资源后,可以使用<resoucecount>条件查看是否选择了commons-lang任何版本:

<path id="commons-lang.path">
     <fileset dir="${lib.dir}">
         <incude name="commons-lang*.jar"/>
     </fileset>
</path>

<fail message="Missing commons-lang Jar">
    <condition>
        <resourcecount refid="commons-lang.path" 
             when="equals" count="0"/>
    </condition>

Now, if I know something about the commons-lang jar, I could have used the <available> condition to search for a particular class in my classpath: 现在,如果我对commons-lang jar有所了解,则可以使用<available>条件在类路径中搜索特定的类:

<fail message="Can't find commons-lang Jar">
     <condition>
         <not>
            <available classname="org.apache.commons.lang.Entities">
                 <classpath refid="compile.classpath"/>
            </available>
         </not>
     </condition>
</fail>

This will find that commons-lang jar no matter what it's called. 无论它叫什么,都会找到commons-lang jar。

If you're interested in Ivy, I have a project in GitHub that makes it pretty easy to add Ivy to an entire development site. 如果您对Ivy感兴趣,那么我在GitHub上有一个项目,可以很轻松地将Ivy添加到整个开发站点。 Third party dependency management is the way to go. 第三方依赖管理是必经之路。 Using Ivy is great because it integrates with Ant. 使用Ivy很棒,因为它与Ant集成在一起。 Maven and Gradle have their advantages, but you'll have to redo your whole build infrastructure in order to use them. Maven和Gradle有其优势,但是必须重做整个构建基础架构才能使用它们。

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

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