Java 如何在单个 Eclipse 项目中设置多个源文件夹?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/513697/
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
How to set up multiple source folders within a single Eclipse project?
提问by Paul Brinkley
I have several somewhat separate programs, that conceptually can fit into a single project. However, I'm having trouble telling Eclipse to make several folders inside a project folder.
我有几个稍微独立的程序,从概念上讲可以适合单个项目。但是,我无法告诉 Eclipse 在项目文件夹中创建多个文件夹。
A simple form of the structure would be:
该结构的一种简单形式是:
/UberProject
/UberProject/ProgramA/
/UberProject/ProgramA/com/pkg/NiftyReader.java
/UberProject/ProgramB/
/UberProject/ProgramB/com/pkg/NiftyWriter.java
That is, ProgramA
and ProgramB
are both projects (in fact, they're currently existing Java projects), which conceptually fit into UberProject
.
也就是说,ProgramA
并且ProgramB
都是项目(实际上,它们是当前现有的 Java 项目),它们在概念上适合UberProject
.
I don't think I'm supposed to make UberProject
be a Java project; it's not a classpath, for instance. ProgramA
and ProgramB
doseem like they should be Java projects (they might use different build dependencies as well), but I see no way in Eclipse 3.3 to create two folders under UberProject
that are intended to contain Java code. I thought about adding a .project file to each of the two sub-projects, but I'm not sure that's appropriate, either. Eclipse help isn't being helpful, and I didn't see anything on SO about this specific problem.
我不认为我应该UberProject
成为一个 Java 项目;例如,它不是类路径。 ProgramA
并且ProgramB
做看起来他们应该是Java项目(他们可能会使用不同的生成依赖关系为好),但我看到在Eclipse 3.3没有办法创建两个文件夹下UberProject
被用来包含Java代码。我想为两个子项目中的每一个添加一个 .project 文件,但我不确定这是否合适。Eclipse 帮助没有帮助,我没有看到关于这个特定问题的任何内容。
Just to be clear: assume as given the necessity of the existence of UberProject
. UberProject
can be a Java project, or not; it doesn't matter. (Incidentally, it does contain other folders that do not contain Java code.)
只是要清楚:假设存在的必要性UberProject
。 UberProject
可以是 Java 项目,也可以不是;没关系。(顺便说一句,它确实包含其他不包含 Java 代码的文件夹。)
采纳答案by Mat
There are probably several ways to do this:
可能有几种方法可以做到这一点:
1) UberProject is your JavaProject. Right click ProgramA -> Build Path -> Use as source folder. Right click ProgramB -> Build Path -> Use as source folder. Both ProgramA and ProgramB will do incremental builds to the same directory.
1) UberProject 是您的 JavaProject。右键单击 ProgramA -> 构建路径 -> 用作源文件夹。右键单击 ProgramB -> 构建路径 -> 用作源文件夹。ProgramA 和 ProgramB 都会对同一目录进行增量构建。
2) Two java projects (ProgramA and ProgramB). You can use UberProject as your eclipse workspace which would be easiest or you can use an outside workspace and import ProgramA and ProgramB as external projects.
2)两个java项目(ProgramA和ProgramB)。您可以使用 UberProject 作为最简单的 eclipse 工作区,或者您可以使用外部工作区并将 ProgramA 和 ProgramB 作为外部项目导入。
There are probably other ways as well (maven multi-module project). Your choice probably depends on whether you have cyclic dependencies between projects. It should be relatively easy to try both 1 and 2 and see what works best for you.
可能还有其他方法(maven 多模块项目)。您的选择可能取决于项目之间是否存在循环依赖。尝试 1 和 2 并查看最适合您的方法应该相对容易。
回答by Mat
Do you need UberProject? I have the same layout but have multiple top-level projects created with File|New project. If not, can you make it a General rather than Java project?
你需要优步项目吗?我有相同的布局,但有多个使用 File|New 项目创建的顶级项目。如果没有,您可以将其设为 General 而不是 Java 项目吗?
回答by evnafets
So you can do it via having two Java projects in your workspace.
Then the question is how to group the two projects together under "UberProject"
因此,您可以通过在工作区中拥有两个 Java 项目来实现。
那么问题是如何在“UberProject”下将两个项目组合在一起
One way is to have an "UberProject" workspace, and switch workspaces between UberProjects. An alternative is to define "UberProject" as a working set (Window:Working Sets) and add PrmgramA and ProgramB as projects of that working set. Select that working set, and you see only those projects.
一种方法是拥有一个“UberProject”工作区,并在 UberProjects 之间切换工作区。另一种方法是将“UberProject”定义为工作集(Window:Working Sets)并将 PrmgramA 和 ProgramB 添加为该工作集的项目。选择该工作集,您只会看到那些项目。
You can have one java project, and define multiple source folders for it. That is normally do that for "main" vs "test" hierarchies within the same project.
您可以拥有一个 Java 项目,并为其定义多个源文件夹。这通常是针对同一项目中的“主”与“测试”层次结构进行的。
There are ways, and ways. Pick one that works for you :-)
有办法,有办法。选择一个适合你的:-)
回答by Jason Day
You can have multiple source directories in a single project, but your description makes it sound like you want multiple sub-projects in a project, which eclipse doesn't allow (as far as I know).
您可以在一个项目中拥有多个源目录,但是您的描述听起来像是您想要在一个项目中包含多个子项目,而 eclipse 不允许(据我所知)。
If you really just want multiple source directories, where ProgramA
, ProgramB
, etc. contain only java files and nothing else, then you can do this relatively easy. Right-click on your project in the package explorer, and select Build Path -> Configure Build Path...
. In the dialog that pops up, make sure Java Build Path
is selected in the left pane, click the Source
tab, then click the Add Folder...
button. Pick ProgramA
from the list, and repeat for ProgramB
, etc.
如果你真的只想要多个源目录, where ProgramA
,ProgramB
等只包含 java 文件而不包含其他任何东西,那么你可以相对容易地做到这一点。在包资源管理器中右键单击您的项目,然后选择Build Path -> Configure Build Path...
。在弹出的对话框中,确保Java Build Path
在左窗格中选中,单击Source
选项卡,然后单击Add Folder...
按钮。匹克ProgramA
从列表中,并重复ProgramB
等。
If you want each source folder to have its own output folder, just check the Allow output folders for source folders
checkbox and edit the output folders as desired.
如果您希望每个源文件夹都有自己的输出文件夹,只需选中Allow output folders for source folders
复选框并根据需要编辑输出文件夹。
If that is not, in fact, what you want to do, then another option might be to group your projects into a working set. A working set is not an UberProject, but it does help keep your projects organized in the package explorer.
如果这实际上不是您想要做的,那么另一种选择可能是将您的项目分组到一个工作集中。工作集不是 UberProject,但它确实有助于在包浏览器中组织您的项目。