在 netbeans 中使用 Ant 在构建期间动态获取最新版本的外部库 [英] Use Ant in netbeans to dynamically fetch latest versions of external libraries during build

查看:21
本文介绍了在 netbeans 中使用 Ant 在构建期间动态获取最新版本的外部库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的是一个蚂蚁新手.这是我的问题:我在 netbeans 中有一个项目,它使用当前存在于/lib 目录中的几个外部库.我希望我的 netbeans 在尝试构建项目时动态获取这些库的最新版本.这是可行的还是我走上了完全错误的道路?我有每个外部库的 URL,我需要在哪里指定这些 URL 才能使这个实验工作?对此的任何帮助表示赞赏,我完全不知道如何做到这一点!

I am an Ant newbie really. Here is my problem : I have a project in netbeans which uses several external libraries which are currently present in /lib directory. I want my netbeans to dynamically fetch latest versions of those libraries when it tries to build the project. Is this doable or am I on the completely wrong path? I have URLs for each of the external libraries, where do I need to specify those in order for this experiment to work? Any help on this is appreciated, I am completely clueless about how to do this !

谢谢

大家好,我已经检查了您所有的答案并对其进行了更多搜索.现在的问题是,虽然maven看起来很不可思议,但我的项目已经使用了Ant,我不知道如何在netbeans上从ant迁移到maven,难度如何.有什么帮助吗?否则我可以为此目的使用 Apache Ivy.举一个我正在使用的 jar 示例:Apache 公共 jar

Hi All, I have checked out all your answers and searched more on them. Now the problem is that although maven looks incredible, my project already uses Ant and I don't know how to migrate from ant to maven on netbeans and how difficult is it. Any help? Or else I can use Apache Ivy for this purpose. To give you an example of jar I am using : Apache commons jar

那么你们能指导我如何去做吗?这个 jar 被包裹在一个 .zip 文件中,所以我猜不容易检测到最新版本.

So can you guys guide me how to go about this ? This jar is wrapped inside a .zip file so not easy to detect latest version I guess.

推荐答案

get 任务是使用标准 ANT 执行此操作的唯一方法.

The get task is the only way to do this using standard ANT.

现代开源开发的问题通常是一个 jar 依赖于多个 jar,当一个人还必须跟踪版本兼容性时,如果不是不可能跟踪,这会变得很困难.

The problem with modern open source development is often one jar depends on several more and when one also must keep track of version compatibilities, this can become difficult if not impossible to keep track of.

Maven 是一种早于 ANT 的构建技术,并且具有针对 3rd 方构建依赖项的依赖项管理功能.可以使用 Apache ivy 插件在 ANT 中复制此功能.

Maven is a build technology that pre-dates ANT and has a dependency management feature for 3rd party build dependencies. This functionality can be replicated in ANT by using the Apache ivy plugin.

演示ivy的使用的Java项目:

Java project to demonstrate the use of ivy:

├── build.xml
├── ivy.xml
└── src
    ├── main
    │   ├── java
    │   │   └── org
    │   │       └── demo
    │   │           └── App.java
    │   └── resources
    │       └── log4j.properties
    └── test
        └── java
            └── org
                └── demo
                    └── AppTest.java

还有一个额外的ivy.xml"文件,其中列出了第 3 方的依赖项.默认情况下,这些 jar 将从 Maven 中央存储库(最大的开源 Java jar 存储库)下载.>

ivy.xml

There is an additional "ivy.xml" file which lists the 3rd party dependencies. These jars will be downloaded by default from the Maven Central repository, the largest repository of open source java jars.

<ivy-module version="2.0">
    <info organisation="com.myspotontheweb" module="demo"/>

    <configurations>
        <conf name="compile" description="Required to compile application"/>
        <conf name="runtime" description="Additional run-time dependencies" extends="compile"/>
        <conf name="test"    description="Required for test only" extends="runtime"/>
    </configurations>

    <dependencies>
        <!-- compile dependencies -->
        <dependency org="org.slf4j" name="slf4j-api" rev="1.7.5" conf="compile->default"/>

        <!-- runtime dependencies -->
        <dependency org="org.slf4j" name="slf4j-log4j12" rev="1.7.5" conf="runtime->default"/>

        <!-- test dependencies -->
        <dependency org="junit" name="junit" rev="4.11" conf="test->default"/>
    </dependencies>

</ivy-module>

注意事项:

  • 此示例使用 slf4j 日志库,它使您可以通过添加不同的 jar 在运行时选择实际实现.
  • ivy 文件声明了 3 个配置",它们是依赖项的逻辑分组.每个依赖项都有一个conf"映射,用于告诉 ivy jar 的用途.
  • 在此示例中,我们的代码将针对 slf4j-api jar 进行编译,并将配置为在运行时使用 log4j.
  • junit 是 ANT 所需的第 3 方 jar 示例.这些也可以由 ivy 下载和管理.
  • This example uses the slf4j logging library which enables you to choose the actual implementation at runtime by adding different jars.
  • The ivy file declares 3 "configurations" which are logical groupings of dependencies. Each dependency has a "conf" mapping that tells ivy what the jar will be used for.
  • In this example our code will compile against the slf4j-api jar and will be configured to use log4j at runtime.
  • junit is an example of a 3rd party jar required by ANT. These can also be downloaded and managed by ivy.
<project name="demo" default="build" xmlns:ivy="antlib:org.apache.ivy.ant">

    <!--
    ================
    Build properties
    ================
    -->
    <property name="src.dir"          location="src/main/java"/>
    <property name="resources.dir"    location="src/main/resources"/>
    <property name="test.src.dir"     location="src/test/java"/>
    <property name="build.dir"        location="build"/>
    <property name="classes.dir"      location="${build.dir}/classes"/>
    <property name="test.classes.dir" location="${build.dir}/test-classes"/>
    <property name="ivy.reports.dir"  location="${build.dir}/ivy-reports"/>
    <property name="test.reports.dir" location="${build.dir}/test-reports"/>
    <property name="dist.dir"         location="${build.dir}/dist"/>

    <property name="jar.main.class" value="org.demo.App"/>
    <property name="jar.file"       value="${dist.dir}/${ant.project.name}.jar"/>

    <!--
    ===========
    Build setup
    ===========
    -->
    <target name="bootstrap" description="Install ivy">
        <mkdir dir="${user.home}/.ant/lib"/>
        <get dest="${user.home}/.ant/lib/ivy.jar" src="http://search.maven.org/remotecontent?filepath=org/apache/ivy/ivy/2.3.0/ivy-2.3.0.jar"/>
    </target>

    <target name="resolve" description="Use ivy to resolve classpaths">
        <ivy:resolve/>

        <ivy:report todir='${ivy.reports.dir}' graph='false' xml='false'/>

        <ivy:cachepath pathid="compile.path" conf="compile"/>
        <ivy:cachepath pathid="test.path"    conf="test"/>
    </target>

    <!--
    ===============
    Compile targets
    ===============
    -->
    <target name="resources" description="Copy resources into classpath">
        <copy todir="${classes.dir}">
            <fileset dir="${resources.dir}"/>
        </copy>
    </target>

    <target name="compile" depends="resolve,resources" description="Compile code">
        <mkdir dir="${classes.dir}"/>
        <javac srcdir="${src.dir}" destdir="${classes.dir}" includeantruntime="false" debug="true" classpathref="compile.path"/>
    </target>

    <target name="compile-tests" depends="compile" description="Compile tests">
        <mkdir dir="${test.classes.dir}"/>
        <javac srcdir="${test.src.dir}" destdir="${test.classes.dir}" includeantruntime="false" debug="true">
            <classpath>
                <path refid="test.path"/>
                <pathelement path="${classes.dir}"/>
            </classpath>
        </javac>
    </target>

    <!--
    ============
    Test targets
    ============
    -->
    <target name="test" depends="compile-tests" description="Run unit tests">
        <mkdir dir="${test.reports.dir}"/>
        <junit printsummary="yes" haltonfailure="yes">
            <classpath>
                <path refid="test.path"/>
                <pathelement path="${classes.dir}"/>
                <pathelement path="${test.classes.dir}"/>
            </classpath>
            <formatter type="xml"/>
            <batchtest fork="yes" todir="${test.reports.dir}">
                <fileset dir="${test.src.dir}">
                    <include name="**/*Test*.java"/>
                    <exclude name="**/AllTests.java"/>
                </fileset>
            </batchtest>
        </junit>
    </target>

    <!--
    =====================
    Build and run targets
    =====================
    -->
    <target name="build" depends="test" description="Create executable jar archive">
        <ivy:retrieve pattern="${dist.dir}/lib/[artifact]-[revision](-[classifier]).[ext]" conf="runtime"/>

        <manifestclasspath property="jar.classpath" jarfile="${jar.file}">
            <classpath>
                <fileset dir="${dist.dir}/lib" includes="*.jar"/>
            </classpath>
        </manifestclasspath>

        <jar destfile="${jar.file}" basedir="${classes.dir}">
            <manifest>
                <attribute name="Main-Class" value="${jar.main.class}" />
                <attribute name="Class-Path" value="${jar.classpath}" />
            </manifest>
        </jar>
    </target>

    <target name="run" depends="build" description="Run code">
        <java jar="${jar.file}" fork="true"/>
    </target>

    <!--
    =============
    Clean targets
    =============
    -->
    <target name="clean" description="Cleanup build files">
        <delete dir="${build.dir}"/>
    </target>

    <target name="clean-all" depends="clean" description="Additionally purge ivy cache">
        <ivy:cleancache/>
    </target>

</project>

注意事项:

  • 默认情况下,常春藤罐不随 ant 一起提供.特殊的引导程序"目标用于将其下载到 ANT 用于其插件的位置.
  • resolve"目标包含用于下载(和缓存)依赖项、生成有关这些文件的有用报告以及创建可用于编译和测试的 ANT 路径的 ivy 任务.
  • 构建"目标包含常春藤检索"任务,该任务将依赖项放置到分发目录中.manifestclasspath 然后可以使用这些来生成正确的类路径"清单条目用于此构建创建的可执行 jar 文件.
  • The ivy jar is not shipped by default with ant. The special "bootstrap" target is used to download this into a location which ANT uses for it's plug-ins.
  • The "resolve" target contains ivy tasks for downloading (and caching) dependencies, generating useful reports on these files and creating ANT paths which can be used for compilation and testing.
  • The "build" target contains the ivy "retrieve" task which places the dependencies into a distribution directory. The manifestclasspath can then use these to generate the correct "Class-path" manifest entry for the executable jar file created by this build.

这篇关于在 netbeans 中使用 Ant 在构建期间动态获取最新版本的外部库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆