eclipse 项目中带有 jar 的文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8098911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 17:18:54  来源:igfitidea点击:

Folder with jars in project

eclipseconfigurationclasspathbuildpath

提问by Piotr Sobczyk

When I work on small desktop projects I used to create libfolder in my project's root where I keep all project's jar dependencies. Then I use Configure Build Path-> Libraries-> Add JARs...to manually add all jars from this folder to buildpath/classpath. And because Add JARs...(unlike Add external JARs) uses relative paths, the project is portable, what is important for me.

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

The problem is that each time I add or remove a jar from my libfolder I need to manually add/remove this jar in project buildpath settings (and of course I often forget to do so).

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

Is there a way to just inform Eclipse that "This is a folder where I keep all of my jars. Please, add all the jars from there automatically to buildpath/classpath"? I tried to treat this folder as a class folder (Add class folder...) but it doesn't work that way :(.

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

P.S. I know about Maven and Eclipse-Maven integration but I want to keep my small project's simple (Maven integration is sometimes frustrating so I prefer to avoid it in these projects), so please don't suggest this in answer. Also as I mentioned, these are desktop projects, so there is no WEB-INF/libfolder in my project that is usually automatically handled by Java EE plugins.

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

采纳答案by aleroot

you can try with a classpath container, take a look herefor an example .

您可以尝试使用类路径容器,请查看此处的示例。

Take a look also at the Apache IvyDE classpath container.

还请查看Apache IvyDE 类路径容器

However adding a new library to the classpath is simple and quick as :

但是,将新库添加到类路径既简单又快捷,如下所示:

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

EDIT

编辑

Thislightweight plugin should do exactly what you want !

这个轻量级插件应该完全符合您的要求!

回答by Peter Perhá?

I am not too sure, but can't you have wildcards in your classpath? That way you could just edit your .classpath file for that Eclipse project and use * within a particular folder... I have not tried, i'm in a rush but that's my idea... don't know if works

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

EDIT here is something that you could find useful: How to use a wildcard in the classpath to add multiple jars?

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

Basically, just edit your .classpathfile, which is where Eclipse stores the classpath settings for a project

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

回答by Suminda Sirinath S. Dharmasena

I think the best is to use Gradle. This does not have the frustration of Maven with Eclipse. If you use STS it comes with Gradle pre-bundled.

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

See the link

见链接

回答by Alexander Wood

So yeah I did this before: Use Apache Ant and specify an ant configuration that suits your build path and eclipse should be able to use it with the use from existing ant build option. Here is an example ant file you might have:

所以是的,我以前这样做过:使用 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 Here is a linkwith someone with a similair problem using ant to solve his classpath problems. Ant is portable so it can actually be set up anywhere and you can also use global variables to keep all systems consistent or just use relative paths. And there is also an eclipse ant plugin

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

回答by savemaxim

just try including

试着包括

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