Eclipse 类路径条目仅用于测试

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

Eclipse classpath entries only used for tests

javaeclipseunit-testingclasspath

提问by Lóránt Pintér

In Maven, you can have compile-timedependencies and testdependencies. This is a feature I love, and the M2Eclipse plugin makes this available in Eclipse, too, which is great. So if I add jmock.jarto my project as a test dependency, it will show up on the classpath for JUnit tests, but won't be present when I'm debugging the application itself.

在 Maven 中,您可以拥有编译时依赖项和测试依赖项。这是我喜欢的一个特性,M2Eclipse 插件也使 Eclipse 可以使用它,这很棒。因此,如果我将jmock.jar作为测试依赖项添加到我的项目中,它将显示在 JUnit 测试的类路径中,但在我调试应用程序本身时不会出现。

This is exactly what I'd like to achieve now, but without M2Eclipse or Maven. Is there a way to do this in plain Eclipse? (Possibly without installing any plugins.)

这正是我现在想要实现的,但没有 M2Eclipse 或 Maven。有没有办法在普通的 Eclipse 中做到这一点?(可能无需安装任何插件。)

采纳答案by Robert Munteanu

I'm afraid the answer is that you can't. There are 2 open issues which were postponed from 3.5 related to your problem:

恐怕答案是你不能。有 2 个未解决的问题从 3.5 推迟到与您的问题相关:

回答by Rich Seller

You could separate all your tests into another project and add the main project as a dependency (Project->Properties->Java Build Path->Projects->Add...)

您可以将所有测试分离到另一个项目中,并将主项目添加为依赖项(Project->Properties-> Java Build Path-> Projects-> Add...

Update: To avoid changing the original project structure, your test projects can use linked locations.

更新:为避免更改原始项目结构,您的测试项目可以使用链接位置。

Create the test project as normal, you now need to create a linked resource to bring in the src/test/java folder. It is best to create it using a variable so that your projects can retain some platform independence. To create a new linked folder select New->Folder, input srcin the folder name:field then click Advanced>>

正常创建测试项目,您现在需要创建一个链接资源以引入 src/test/java 文件夹。最好使用变量创建它,以便您的项目可以保留一些平台独立性。要创建一个新的链接文件夹,选择New-> Folder,在文件夹名称中输入src :字段,然后单击Advanced>>

Click Link to folder in the file systemClick on Variables...to bring up the Select Path Variabledialogue.

单击文件系统中的链接到文件夹单击变量...以显示选择路径变量对话框。

If this is your first time, or you are linking to a new location select New...and give the variable a sensible name and path. If all your projects are located in c:\workspaces\foo** it makes sense to call the variable **WORKSPACE_ROOTand give it that path. If you have some other convention that is fine, but it makes sense to put a comment in the .project file so someone has a chance of figuring out what the correct value should be.

如果这是您的第一次,或者您要链接到一个新位置,请选择New...并为变量指定一个合理的名称和路径。如果您的所有项目都位于c:\workspaces\foo** 中,则调用变量 **WORKSPACE_ROOT并为其指定路径是有意义的。如果您有其他一些很好的约定,但在 .project 文件中添加注释是有意义的,这样有人就有机会弄清楚正确的值应该是什么。

Assuming the values above you can now set a value of WORKSPACE_ROOT/[subject project name]/srcon the input field

假设上面的值,您现在可以在输入字段上设置WORKSPACE_ROOT/[subject project name]/src的值

Once you confirm that you should see the src folder with a little arrow, and if you look in the .project file see something like this:

确认您应该看到带有小箭头的 src 文件夹后,如果您查看 .project 文件,则会看到如下内容:

<linkedResources>
    <link>
        <name>src</name>
        <type>2</type>
        <locationURI>WORKSPACE_ROOT/esf-ns-core-rp/src</locationURI>
    </link><!--NOTE the WORKSPACE_ROOT variable points to the folder containing the subject project's sandbox-->
</linkedResources>

You can now add the src/test/java folder as a source location as normal.

您现在可以像往常一样将 src/test/java 文件夹添加为源位置。

Note you can also share just the src/test/java folder by changing the config to something like this:

请注意,您还可以通过将配置更改为以下内容来仅共享 src/test/java 文件夹:

<linkedResources>
    <link>
        <name>src/test/java</name>
        <type>2</type>
        <locationURI>WORKSPACE_ROOT/my-project/src/test/java</locationURI>
    </link>
</linkedResources>

This gives more control over the config, but you would have to repeat for src/test/resources, src/it/java etc.

这可以更好地控制配置,但您必须对 src/test/resources、src/it/java 等重复。

You then set all the test dependencies only in the test project.

然后,您只在测试项目中设置所有测试依赖项。

Very not pretty, but it does work (I've also used this where my test compliance level is different to the main compliance level, e.g. 1.5 for tests, but 1.4 for the target environment).

非常不漂亮,但它确实有效(我也在我的测试合规级别与主要合规级别不同的情况下使用了它,例如 1.5 用于测试,但 1.4 用于目标环境)。

回答by martin-g

Since you use both Eclipse and Maven you can workaround it. Create a new "Maven Build" run configuration with goal "exec:java" and parameters "exec.mainClass=com.example.Starter". This way the classpath will be calculated by Maven.

由于您同时使用 Eclipse 和 Maven,您可以解决它。使用目标“exec:java”和参数“exec.mainClass=com.example.Starter”创建一个新的“Maven Build”运行配置。这样,类路径将由 Maven 计算。

回答by Ben

Eclipse Photon finallyadded this feature, with m2e support for it.

Eclipse Photon终于添加了这个功能,并提供了 m2e 支持。

回答by Newtopian

Actually if you look in eclipse as to how Maven integrates dependencies it will not make the difference in test or runtime dependencies your test libraries are always accessible.

实际上,如果您在 Eclipse 中查看 Maven 如何集成依赖项,它不会对测试或运行时依赖项产生影响,您的测试库始终是可访问的。

Maven will keep the difference when packaging the application and when it generates the runtime classpath if maven has control over the execution of that part. When eclipse is concerned Maven simply adds them all without question to the eclipse build path.

如果 Maven 可以控制该部分的执行,Maven 将在打包应用程序和生成运行时类路径时保持差异。当涉及 eclipse 时,Maven 只是毫无疑问地将它们全部添加到 eclipse 构建路径中。

Why is it you need to have this separated like so ? What will this help you acheive ?

为什么你需要这样分开?这对你有什么帮助?