我真的是一个蚂蚁新手。这是我的问题:我在 netbeans 中有一个项目,它使用当前位于 /lib 目录中的几个外部库。我希望我的 netbeans 在尝试构建项目时动态获取这些库的最新版本。这是可行的还是我走在完全错误的道路上?
我有每个外部库的 URL,我需要在哪里指定这些 URL 才能使该实验正常进行?
感谢对此的任何帮助,我完全不知道如何做到这一点!
Thanks
大家好,
我已经检查了您的所有答案并进行了更多搜索。现在的问题是,虽然maven看起来不可思议,但我的项目已经使用了Ant,我不知道如何在netbeans上从ant迁移到maven以及有多困难。有什么帮助吗?
或者我可以使用 Apache Ivy 来实现此目的。
给你一个我正在使用的 jar 的例子:Apache 公共 jar
那么你们能指导我如何去做吗?这个 jar 包包含在 .zip 文件中,所以我想不容易检测到最新版本。
The gettask 是使用标准 ANT 执行此操作的唯一方法。
现代开源开发的问题通常是一个 jar 依赖于多个 jar,并且当还必须跟踪版本兼容性时,即使不是不可能跟踪,这也会变得很困难。
Maven是一种早于 ANT 的构建技术,具有针对第 3 方构建依赖项的依赖项管理功能。可以使用以下命令在 ANT 中复制此功能阿帕奇常春藤 plugin.
Example
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 中央存储库,最大的开源 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>
Notes:
- 此示例使用slf4j日志记录库使您能够通过添加不同的 jar 在运行时选择实际的实现。
- ivy 文件声明了 3 个“配置”,它们是依赖项的逻辑分组。每个依赖项都有一个“conf”映射,告诉 ivy 该 jar 的用途。
- 在此示例中,我们的代码将针对 slf4j-api jar 进行编译,并将配置为在运行时使用 log4j。
- junit 是 ANT 所需的第 3 方 jar 的示例。这些也可以通过ivy下载和管理。
构建.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”任务,它将依赖项放入分发目录中。这清单类路径然后可以使用这些为该构建创建的可执行 jar 文件生成正确的“类路径”清单条目。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)