【问题标题】:Folder with jars in project项目中带有 jar 的文件夹
【发布时间】:2011-12-27 06:54:14
【问题描述】:

当我处理小型桌面项目时,我曾经在项目的根目录中创建 lib 文件夹,用于保存所有项目的 jar 依赖项。然后我使用 Configure Build Path -> Libraries -> Add JARs... 手动将此文件夹中的所有 jar 添加到 buildpath/classpath。而且因为 Add JARs...(不像 Add external JARs)使用相对路径,所以项目是可移植的,这对我来说很重要。

问题是每次我从我的lib 文件夹中添加或删除一个 jar 时,我都需要在项目构建路径设置中手动添加/删除这个 jar(当然我经常忘记这样做)。

有没有办法只通知 Eclipse “这是我保存所有 jar 的文件夹。请自动将所有 jar 从那里添加到 buildpath/classpath”?我试图将此文件夹视为类文件夹(Add class folder...),但它不起作用:(。

附:我知道 Maven 和 Eclipse-Maven 集成,但我想保持我的小项目的简单(Maven 集成有时令人沮丧,所以我更愿意在这些项目中避免它),所以请不要在回答中建议这个。同样正如我所提到的,这些是桌面项目,所以我的项目中没有通常由 Java EE 插件自动处理的 WEB-INF/lib 文件夹。

【问题讨论】:

  • 完全同意,maven 对于小型项目来说可能是一种过度杀伤力。这是一个比基本修补更大的东西的救生员,但麻烦......天哪
  • 这看起来很像这个 stackoverflow 问题的副本:stackoverflow.com/questions/483563/…

标签: eclipse configuration classpath buildpath


【解决方案1】:

我不太确定,但你的类路径中不能有通配符吗?这样你就可以为那个 Eclipse 项目编辑你的 .classpath 文件并在一个特定的文件夹中使用 * ......我没有尝试过,我很着急,但这是我的想法......不知道是否有效

这里的编辑是你会发现有用的东西: How to use a wildcard in the classpath to add multiple jars?

基本上,只需编辑您的 .classpath 文件,这是 Eclipse 存储项目类路径设置的地方

【讨论】:

  • 谢谢,这似乎是个好主意。不幸的是,Eclipse 似乎不理解这种表示法。我在我的.classpath 文件中添加了<classpathentry kind="lib" path="lib/*"/> 条目,但是我有编译错误,因为Eclipse 没有将jar 从lib/* 添加到构建路径(可能它会被正确地解析为类路径)。无论如何,很高兴了解类路径通配符表示法。
  • @PiotrSobczyk 试试这个:<classpathentry kind="lib" path="lib/*.jar"/><classpathentry kind="lib" path="lib/**/*.jar"/>
  • @HarryJoy 它仍然不起作用。 Eclipse 只是不解释*s。它对它们进行详细处理并尝试在名为 lib/* 的目录中找到名为 *.jar 的 jar。
【解决方案2】:

我认为最好的方法是使用 Gradle。这没有 Maven 与 Eclipse 的挫败感。如果您使用 STS,它会预先捆绑 Gradle。

See the link

【讨论】:

  • 你也可以试试@harry Joy 的建议。
【解决方案3】:

您可以尝试使用类路径容器,查看here 示例。

看看Apache IvyDE classpath container

然而,向类路径添加一个新库是简单快捷的:

Right click on it ---> Build Path ---> Add To Build Path

编辑

This 轻量级插件应该做你想做的事!

【讨论】:

  • 似乎没有办法以 easy 的方式达到我所需要的。然而,指向类路径容器的答案最接近我的要求。问题是实现类路径容器似乎是一项巨大的努力。是否还有更多预实现的类路径容器,例如 IvyDE 类路径容器?也许其中一个可以立即解决我的问题?
  • 太好了,这正是我所需要的!你绝对应该得到赏金!
  • aleroot 指向的插件很好用。您在 Eclipse 构建路径设置 -> 库 -> 添加库... -> 目录类路径容器中添加基于目录的类路径。唯一需要担心的是,如果您在没有安装插件的其他 Eclipse 实例上打开项目,并将新文件添加到目录中,它们将不再自动添加到类路径中(但已经添加的 jars 仍保留在类路径中),因此插件需要安装在 Eclipse 中以使用它为项目生成的类路径容器。但这对我来说不是问题。
  • 404 链接,并且导航器窗口中没有“构建路径”
  • @user1133275 答案是 5 年前写的,我不会每天检查链接是否仍然有效......无论如何,项目导航器中不存在构建路径,那么你必须使用包资源管理器视图 ...
【解决方案4】:

所以是的,我以前这样做过: 使用 Apache Ant 并指定适合您的构建路径的 ant 配置,并且 eclipse 应该能够通过现有 ant 构建选项的使用来使用它。 这是您可能拥有的示例 ant 文件:

<?xml version="1.0"?>

<project name="Demo Project" basedir="." default="package">
  <!-- ================================================================= -->
  <!-- C O N F I G U R A T I O N                                         -->
  <!-- ================================================================= -->
  <!--
    Access the environment properties
    -->
  <property environment="env" />
  <!--
  TODO: Access the environment properties with a prefix of "env".
  -->
  <!--  
  Additional 3rd-party tools
   -->
  <property name="ant.home"      value="${env.ANT_HOME}"/>
  <property name="junit.home"    value="${env.JUNIT_HOME}"/>
  <property name="jmock.home"    value="${env.JMOCK_HOME}"/>
  <!--
  Project folders
  -->
  <property name="project.home"  value="${basedir}" />
  <property name="bin.dir"       value="${project.home}/bin" />
  <property name="dist.dir"      value="${project.home}/dist" />
  <property name="dist.file"     value="${dist.dir}/lab03.jar" />
  <property name="col.file"      value="${dist.dir}/lab03-col.jar" />
  <property name="src.dir"       value="${project.home}/src" />
  <property name="lib.dir"       value="${project.home}/lib" />

  <!--
  TODO: Define the classpath to be used during compilation. This should
  consist of all of the JAR files in the ${lib.dir} folder.
  -->
  <path id="project.class.path">
    <path location="${dist.file}" />
    <path location="${bin.dir}" />
    <fileset dir="${junit.home}">
    <include name="junit-4.7.jar"/>
    </fileset>
    <fileset dir="${jmock.home}">
    <include name="**/*.jar"/>
    </fileset>
    <fileset dir="${ant.home}/lib">
        <include name="**/*.jar"/>
    </fileset>
    <fileset dir="${lib.dir}">
        <include name="**/*.jar"/>
    </fileset>  
  </path>

  <!--
  TODO: Define the classpath to be used during execution. This should
  consist of all of the JAR files in the ${lib.dir} folder as well as
  ${dist.file}.
  -->
  <path id="execution.class.path">
       <path location="${bin.dir}" />
       <path location="${bin.dir}/MyPath1/MyPath" />
       <path location="${bin.dir}/MyPath1/MyPath/impl" />
       <fileset dir="${lib.dir}">
          <include name="**/*.jar"/>
       </fileset>   
  </path>

  <!-- ================================================================= -->
  <!-- C L E A N                                                         -->
  <!-- ================================================================= -->
  <target name="clean"
          description="Clean all build products">
    <delete dir="${bin.dir}" />
    <delete dir="${dist.dir}" />
  </target>

  <!-- ================================================================= -->
  <!-- C O M P I L E                                                     -->
  <!-- ================================================================= -->
  <target name="compile" 
          depends="clean,init"
            description="Compiles the application code">
    <!--
    TODO: Add the javac task. It should compile everything in ${src.dir}
    and place the output in ${bin.dir}. The classpath should refer to the
    "project.class.path" defined above.
    -->
    <javac srcdir="${src.dir}"
               destdir="${bin.dir}">
       <classpath refid="project.class.path" />
    </javac>
  </target>

  <!-- ================================================================= -->
  <!-- E N V                                                             -->
  <!-- ================================================================= -->
  <target name="env"
            description="Displays information about the build">
    <echo message="src.dir..........${src.dir}" />
    <echo message="lib.dir..........${lib.dir}" />
    <echo message="bin.dir..........${bin.dir}" />
    <echo message="dist.dir.........${dist.dir}" />
    <echo message="dist.file........${dist.file}" />
    <echo message="col.file.........${col.file}" />
    <echo message="reports.dir......${reports.dir}" />
  </target>

  <!-- ================================================================= -->
  <!-- I N I T                                                           -->
  <!-- ================================================================= -->
  <target name="init" 
          depends="env"
          description="Initializes the environment">
    <mkdir dir="${bin.dir}" />
    <mkdir dir="${dist.dir}" />
  </target>

  <!-- ================================================================= -->
  <!-- P A C K A G E                                                     -->
  <!-- ================================================================= -->
  <target name="package" 
          depends="compile"
            description="Creates the application distribution file">
    <!--
    TODO: Create a JAR file. The target JAR should be ${dist.file} and it
    should contain everything from ${bin.dir}.
    -->
    <jar destfile="${dist.file}"
           basedir="${bin.dir}"
           excludes="**/*Test*.class"
    />
  </target>

  <!-- ================================================================= -->
  <!-- P A C K A G E - C O L                                             -->
  <!-- ================================================================= -->
  <target name="package-col" 
          depends="compile"
            description="Creates the file to be submitted to COL.">
    <jar destfile="${col.file}">
      <fileset dir="${project.home}"
               includes="src/**/*.java" />
      <fileset dir="${project.home}"
               includes="lib/**/*.jar" />
      <fileset dir="${project.home}"
               includes="build.xml" />
    </jar>
  </target>

  <!-- ================================================================= -->
  <!-- R U N                                                             -->
  <!-- ================================================================= -->
  <target name="run" 
          depends="package"
            description="Executes the test file">
    <java classname="MyPath1.MyPath.FileScanner">
      <classpath refid="execution.class.path" />
      <arg value="file:///" />
    </java>
  </target>
</project>

AND 这是一个link 与一个有类似问题的人使用 ant 来解决他的类路径问题。 Ant 是可移植的,因此它实际上可以在任何地方设置,您还可以使用全局变量来保持所有系统的一致性,或者只使用相对路径。还有an eclipse ant plugin

【讨论】:

    【解决方案5】:

    试试包括

    <classpathentry kind="lib" path="lib/spring/4.2.1" including="*.jar"/>
    

    【讨论】:

    • 请编辑更多信息。纯代码和“试试这个”的答案是discouraged,因为它们不包含可搜索的内容,也没有解释为什么有人应该“试试这个”。
    猜你喜欢
    • 2013-02-23
    • 1970-01-01
    • 2016-05-10
    • 2019-10-13
    • 1970-01-01
    • 2012-08-04
    • 1970-01-01
    • 2013-05-09
    • 1970-01-01
    相关资源
    最近更新 更多