繁体   English   中英

Eclipse:用于导出项目的用户库/库的Ant脚本

[英]Eclipse: Ant script to export User Library/Libraries for project

我是Java编程的新手。 我最初是从NetBeans开始的,但是根据朋友的建议,我已经搬到了Eclipse。

在NetBeans中,为项目预先编写的ant构建脚本将生成Project.jar文件,并将所有必需的库/ jar放在lib /文件夹中。

但是,在Eclipse中,似乎我需要编写自己的ant脚本。 我写了几行来生成jar文件:

<target name="compile"> <mkdir dir="${build.dir}"/> <javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="jars" debug="on"/> </target>

如何编写命令将我的用户库中的所有jar复制到$ {build.dir} / lib /文件夹?

谢谢。

使用复制任务

像这样,使用适当的包含或排除模式

  <copy todir="${build.dir}/lib/">
    <fileset dir="src_dir">
      <include name="**/*.jar"/>
    </fileset>
  </copy>



 <copy todir="${build.dir}/lib/">
    <fileset dir="src_dir" excludes="**/*.java"/>
  </copy>

我建议使用ant4eclipse库进行基于ant的eclipse项目。 当你使用它时,你可以访问eclipse工作区/项目设置,并且可以在ant中迭代eclipse项目类路径。

请参阅下面的示例代码:

<path id="ant.classpath">
    <fileset dir="${lib.dir}/ant4eclipse">
        <include name="*.jar" />
</fileset>

<taskdef resource="net/sf/antcontrib/antlib.xml" />
<taskdef resource="net/sf/ant4eclipse/antlib.xml" />

<targetPlatform

<target name="copy_jars">
  <getEclipseClasspath workspace="${basedir}/.."
                       projectname="TestProject"
                       targetPlatformLocation="c:/eclipse"
                       property="classpath"
                       relative="false"
                       runtime="true"
                       pathseparator="#" />

  <!-- iterate over all classpath entries -->
  <foreach list="${classpath}" delimiter="#"
    target="copy_jar_file" param="classpath.entry" />
</target>

<target name="copy_jar_file">
  <!-- check if current is a .jar-file ... -->
  <if>
    <isfileselected file="${classpath.entry}">
      <filename name="**/*.jar" />
    </isfileselected>
    <then>
      <!-- copy the jar file to a destination directory -->
      <copy file="${classpath.entry}" tofile="${dest.dir}"/>
    </then>
  </if>
</target>

如果要使用用户库,可以通过userlibraries命令进行定义。

如果您是Java新手,请抓紧机会看看maven 它是一个像ant一样的构建工具,具有许多预定义的“目标”和完全开发的依赖(对其他库)处理。 你会发现一个非常有用的eclipse插件

Maven项目有一个特殊的目录布局 ,这是一种最佳实践,对初学者很有帮助。 如果你在maven项目上工作,你可以使用该命令

mvn dependency:copy-dependencies

作为控制台命令(或eclipse运行配置)将项目依赖项(库)复制到<project>\\target\\dependency目录。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM