如何指示 Eclipse 记录外部 jars 的相对路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8317957/
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:22:58  来源:igfitidea点击:

How to instruct Eclipse to record relative paths of the external jars?

eclipse

提问by mark

Here is my .classpath file, after I have added two more external jars (org.restlet.ext.simple.jar and org.simpleframework.jar):

这是我的 .classpath 文件,在我添加了另外两个外部 jar(org.restlet.ext.simple.jar 和 org.simpleframework.jar)之后:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="src"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
    <classpathentry kind="lib" path="../3rd_party/restlet-jse-2.0.10/lib/org.restlet.ext.Hymanson.jar" sourcepath="C:/Program Files/Java/restlet-jse-2.0.10/src">
        <attributes>
            <attribute name="javadoc_location" value="file:/C:/Program Files/Java/restlet-jse-2.0.10/docs/ext/"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="lib" path="../3rd_party/restlet-jse-2.0.10/lib/org.restlet.ext.ssl.jar" sourcepath="C:/Program Files/Java/restlet-jse-2.0.10/src">
        <attributes>
            <attribute name="javadoc_location" value="file:/C:/Program Files/Java/restlet-jse-2.0.10/docs/ext/"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="lib" path="../3rd_party/restlet-jse-2.0.10/lib/org.restlet.jar" sourcepath="C:/Program Files/Java/restlet-jse-2.0.10/src">
        <attributes>
            <attribute name="javadoc_location" value="file:/C:/Program Files/Java/restlet-jse-2.0.10/docs/api"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="lib" path="../3rd_party/restlet-jse-2.0.10/lib/org.jsslutils_1.0/org.jsslutils.jar"/>
    <classpathentry kind="lib" path="../3rd_party/restlet-jse-2.0.10/lib/org.codehaus.Hymanson_1.4/org.codehaus.Hymanson.core.jar"/>
    <classpathentry kind="lib" path="../3rd_party/restlet-jse-2.0.10/lib/org.codehaus.Hymanson_1.4/org.codehaus.Hymanson.mapper.jar"/>
    <classpathentry kind="lib" path="../3rd_party/guice-3.0/aopalliance.jar"/>
    <classpathentry kind="lib" path="../3rd_party/guice-3.0/guice-3.0.jar"/>
    <classpathentry kind="lib" path="../3rd_party/guice-3.0/javax.inject.jar"/>
    <classpathentry kind="lib" path="C:/dev/poc/3rd_party/restlet-jse-2.0.10/lib/org.restlet.ext.simple.jar" sourcepath="C:/Program Files/Java/restlet-jse-2.0.10/src">
        <attributes>
            <attribute name="javadoc_location" value="file:/C:/Program Files/Java/restlet-jse-2.0.10/docs/ext/"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="lib" path="C:/dev/poc/3rd_party/restlet-jse-2.0.10/lib/org.simpleframework_4.1/org.simpleframework.jar"/>
    <classpathentry kind="output" path="bin"/>
</classpath>

Notice that they have been added with absolute paths, unlike other entries which are with relative ones, but only because I manually edit this file each time new external jars are added.

请注意,它们已添加了绝对路径,这与其他具有相对路径的条目不同,但这仅仅是因为每次添加新的外部 jar 时我都会手动编辑此文件。

My question is can I somehow tell Eclipse to use relative paths of newly added external jars?

我的问题是我可以以某种方式告诉 Eclipse 使用新添加的外部 jar 的相对路径吗?

Thanks.

谢谢。

回答by Matthew Farwell

One solution is to not use external jars, but to put your jars into a project, and then use Add Jar(s) instead of Add External Jar(s).

一种解决方案是不使用外部 jar,而是将您的 jar 放入项目中,然后使用 Add Jar(s) 而不是 Add External Jar(s)。

This makes sense from the point of view of source control, you can add/remove dependencies as you need them. It also means that when you update one jar for a separate project, it'll not affect this one.

从源代码控制的角度来看,这是有道理的,您可以根据需要添加/删除依赖项。这也意味着当你为一个单独的项目更新一个 jar 时,它不会影响这个。

We've done this in the past, we had a single project which contained all of the jars, which were referred to in the build paths of other projects.

我们过去已经这样做了,我们有一个包含所有 jars 的单个项目,这些 jars 在其他项目的构建路径中被引用。

But now we use maven, so we don't need to do that any more.

但是现在我们使用maven,所以我们不再需要这样做了。

回答by Igor Popov

In eclipse, right-click the project, choose Properties, then Java Build Path, Librariestab and select "Add Jars"... this will add it with a relative path. "Add External JARs" adds the jar with an absolute pathwhich is not what you want.

在 Eclipse 中,右键单击该项目,选择Properties,然后选择Java Build PathLibraries选项卡并选择“ Add Jars”...这将使用相对路径添加它。“添加外部 JARs”添加带有绝对路径的 jar,这不是您想要的。

回答by Renardo

If your paths are relative to your Eclipse installation, declare them using the “Add Variable…” button (in [Project] → Properties → Java Build Path → Libraries). There you should be offered a variable called ECLIPSE_HOME, which you can then Extend. This will produce a ?kind=var“ entry in your .classpath(rather than a ?kind=lib“).

如果您的路径与 Eclipse 安装相关,请使用“添加变量...”按钮(在 [项目] → 属性 → Java 构建路径 → 库中)声明它们。在那里你应该得到一个名为 的变量ECLIPSE_HOME,然后你可以扩展它。这将在您的.classpath(而不是 ?kind=lib“)中生成一个 ?kind=var“ 条目。

<classpathentry kind="var" path="ECLIPSE_HOME/...

If your paths are relative to somewhere else you can declare your own variables (Configure variables). This way, when your workspace changes or someone makes a copy of it you just need to update these variables. This is often clearer than having true relative paths starting with one or two dots.

如果您的路径相对于其他地方,您可以声明自己的变量(配置变量)。这样,当您的工作区发生变化或有人复制它时,您只需要更新这些变量。这通常比以一两个点开头的真实相对路径更清晰。