在 netbeans 中使用 Ant 在构建期间动态获取最新版本的外部库

voi*_*urn 3 ant netbeans maven

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

谢谢

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

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

Mar*_*nor 5

GET任务是做到这一点使用标准ANT的唯一途径。

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

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

例子

Java项目来演示ivy的使用:

??? build.xml
??? ivy.xml
??? src
    ??? main
    ?   ??? java
    ?   ?   ??? org
    ?   ?       ??? demo
    ?   ?           ??? App.java
    ?   ??? resources
    ?       ??? log4j.properties
    ??? test
        ??? java
            ??? org
                ??? demo
                    ??? AppTest.java
Run Code Online (Sandbox Code Playgroud)

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

常春藤.xml

<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>
Run Code Online (Sandbox Code Playgroud)

笔记:

  • 此示例使用slf4j日志库,它使您可以通过添加不同的 jar 来选择运行时的实际实现。
  • ivy 文件声明了 3 个“配置”,它们是依赖项的逻辑分组。每个依赖项都有一个“conf”映射,它告诉 ivy jar 将用于什么。
  • 在此示例中,我们的代码将针对 slf4j-api jar 进行编译,并将配置为在运行时使用 log4j。
  • junit 是 ANT 所需的第 3 方 jar 示例。这些也可以由 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>
Run Code Online (Sandbox Code Playgroud)

几个注意事项:

  • 默认情况下,常春藤 jar 不随 ant 一起提供。特殊的“引导程序”目标用于将其下载到 ANT 用于其插件的位置。
  • “resolve”目标包含用于下载(和缓存)依赖项、生成有关这些文件的有用报告以及创建可用于编译和测试的 ANT 路径的 ivy 任务。
  • “构建”目标包含常春藤“检索”任务,该任务将依赖项放置到分发目录中。然后manifestclasspath可以使用这些来为这个构建创建的可执行 jar 文件生成正确的“类路径”清单条目。