【问题标题】:Use Ant in netbeans to dynamically fetch latest versions of external libraries during build在 netbeans 中使用 Ant 在构建期间动态获取外部库的最新版本
【发布时间】:2013-06-09 08:58:38
【问题描述】:

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

谢谢

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

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

【问题讨论】:

  • ant.apache.org/ivy 是否适合您的需求?
  • @Joachim Rohde:是的。你能帮我看看如何在netbeans上安装它吗?我搜索了一下,在netbeans插件中没有。
  • 我自己从来没有用过 Ivy,但是看看wiki.netbeans.org/FaqIvy
  • 你改变了原来的问题。请接受提供的答案并针对新问题提出新问题。处理 zip 文件中的 jar 是 Maven 无法做到的。常春藤可以(使用打包解析器),但这不是微不足道的。见:stackoverflow.com/questions/908371/…
  • @MarkO'Connor :正如你在这个问题和我问的另一个问题上所建议的那样,集成 netbeans 和 apache ivy 的东西工作得很好。到目前为止,它给了我一些错误。我正在尝试解决它们。如果没有;我会回来提出更多问题。谢谢

标签: maven netbeans ant


【解决方案1】:

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

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

ivy.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>

注意事项:

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

build.xml

<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 不附带 ivy jar。特殊的“bootstrap”目标用于将其下载到 ANT 用于其插件的位置。
  • “resolve”目标包含 ivy 任务,用于下载(和缓存)依赖项、生成有关这些文件的有用报告以及创建可用于编译和测试的 ANT 路径。
  • “构建”目标包含常春藤“检索”任务,该任务将依赖项放入分发目录。然后,manifestclasspath 可以使用这些为由此构建创建的可执行 jar 文件生成正确的“类路径”清单条目。

【讨论】:

  • 感谢您如此详尽的描述。经过一番搜索,我发现我别无选择,只能选择 Ivy 或 Maven。由于我的项目已经使用了 Ant,我认为使用 Ivy 会比转向 Maven 更好。一个非常基本的问题,如何将 ivy 插件安装到 netbeans 中?
  • @tejas 运行示例中的“bootstrap”目标,这将安装 ivy jar,以便 ANT 可以拾取它。我不是 netbeans 用户,但我发现以下插件可能对 UI 集成有用:code.google.com/p/ivybeans 最后在 netbeans wiki 上有一个常春藤常见问题解答:wiki.netbeans.org/FaqIvy
  • 您已经在上面展示了如何添加 ivy.xml。一个问题是:如何告诉 ivy 更新那个外部 jar?
  • @tejas 我不明白这个问题。
  • 好的,据我所知,Ivy 的主要用途是在构建期间检查和获取外部库的最新版本。那么常春藤从哪里获取这些最新版本呢?为此,我必须对 ivy.xml 文件进行一些更改吗?例如: 在 ant 中我们有 ..所以在 Ivy.xml 中我必须做类似的事情吗?
【解决方案2】:

作为第一个建议,我会说 ant 可能不是做到这一点的最佳工具。 Maven 做得很好(至少如果您的外部库作为 SNAPSHOT 发布在(公共)Maven 存储库中)。此外,maven 和 netbeans 可以很好地协同工作。

但是,如果你需要使用 ant,你应该看看ant get task。如果远程副本比本地副本更新(具有 usetimestamp 属性),它允许您下载文件。所以像这样的东西会在需要时进行下载:

<get src="http://some.url/somelib.jar" 
     dest="lib/somelib.jar"
     usetimestamp="true"/>

【讨论】:

  • 你能给我一些关于如何将我的项目从 Ant 迁移到 Maven 的建议吗?因为看起来我有一些罐子,如果我使用 ant get,则必须从 zip 文件中手动提取。
  • 可以用ant解压:ant.apache.org/manual/Tasks/unzip.html从ant迁移到maven没有简单的方法。它非常依赖于您当前构建过程的复杂性。这是一个很好的从 maven 开始的指针:books.sonatype.com/mvnex-book/reference/index.html
  • 嗨,我认为 unzip 可能会帮助我。但我仍然很困惑。您能否查看我对该问题的最新编辑并针对该示例相应地指导我?谢谢。
猜你喜欢
  • 1970-01-01
  • 2023-03-18
  • 1970-01-01
  • 2019-01-06
  • 1970-01-01
  • 2013-02-08
  • 1970-01-01
  • 2019-08-22
  • 1970-01-01
相关资源
最近更新 更多