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

标签 maven netbeans ant

我真的是一个Ant新手。这是我的问题:我在 netbeans 中有一个项目,它使用当前存在于/lib 目录中的几个外部库。我希望我的 netbeans 在尝试构建项目时动态获取这些库的最新版本。这是可行的还是我在完全错误的道路上? 我有每个外部库的 URL,我需要在哪里指定这些 URL 才能让这个实验起作用? 感谢您对此提供的任何帮助,我完全不知道该怎么做!

谢谢

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

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

最佳答案

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

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

Maven是一种早于 ANT 的构建技术,具有针对第 3 方构建依赖项的依赖项管理功能。可以使用 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 将从 Maven Central repository 下载。 ,最大的开源 Java jar 存储库。

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>

几个注意事项:

  • 默认情况下,ivy jar 不随 ant 一起提供。特殊的“ Bootstrap ”目标用于将其下载到 ANT 用于其插件的位置。
  • “resolve”目标包含 ivy 任务,用于下载(和缓存)依赖项、生成有关这些文件的有用报告以及创建可用于编译和测试的 ANT 路径。
  • “build”目标包含 ivy“retrieve”任务,它将依赖项放入分发目录中。 manifestclasspath然后可以使用它们为由此构建创建的可执行 jar 文件生成正确的“类路径” list 条目。

关于maven - 在 netbeans 中使用 Ant 在构建期间动态获取最新版本的外部库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17079198/

相关文章:

java - Maven找不到包

java - Izpack Maven 插件 Eclipse Maven 项目上的基目录无效

c++ - NetBeans6.9 C++ 运行时错误

ant - 如何解析文件路径并获取其父目录的父目录?

java - JMH 不在 Java 模块内运行(无法找到资源 :/META-INF/BenchmarkList)

mysql - Spring Boot 应用程序运行正常,但 hibernate 和 MySql 相关属性不起作用

java - 单击 JList 项如何在 JTabbedPane 中打开新选项卡?

java - 获取资源时的NPE

android - 如何使用 Ant 编译和 jar Android 项目?

java - 无法在liferay中部署portlet war