如何在单个Eclipse项目中设置多个源文件夹?

时间:2023-01-13 12:40:42

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 do seem 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代码的文件夹。)

4 个解决方案

#1


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 - > Build Path - > Use as source folder。右键单击ProgramB - > Build Path - > Use as source folder。 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都应该相对容易,看看什么最适合你。

#2


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.

如果你真的只想要多个源目录,其中ProgramA,ProgramB等只包含java文件,那么你可以相对容易地做到这一点。在包资源管理器中右键单击您的项目,然后选择Build Path - > Configure Build Path ....在弹出的对话框中,确保在左窗格中选择了Java Build Path,单击Source选项卡,然后单击添加文件夹...按钮。从列表中选择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.

如果希望每个源文件夹都有自己的输出文件夹,只需选中“允许输出文件夹中的源文件夹”复选框,然后根据需要编辑输出文件夹。

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,但它确实有助于在包资源管理器中组织您的项目。

#3


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?

你需要UberProject吗?我有相同的布局,但有多个*项目使用文件|新项目创建。如果没有,你能把它变成一般而不是Java项目吗?

#4


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 :-)

有办法和方法。选一个适合你的:-)

#1


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 - > Build Path - > Use as source folder。右键单击ProgramB - > Build Path - > Use as source folder。 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都应该相对容易,看看什么最适合你。

#2


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.

如果你真的只想要多个源目录,其中ProgramA,ProgramB等只包含java文件,那么你可以相对容易地做到这一点。在包资源管理器中右键单击您的项目,然后选择Build Path - > Configure Build Path ....在弹出的对话框中,确保在左窗格中选择了Java Build Path,单击Source选项卡,然后单击添加文件夹...按钮。从列表中选择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.

如果希望每个源文件夹都有自己的输出文件夹,只需选中“允许输出文件夹中的源文件夹”复选框,然后根据需要编辑输出文件夹。

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,但它确实有助于在包资源管理器中组织您的项目。

#3


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?

你需要UberProject吗?我有相同的布局,但有多个*项目使用文件|新项目创建。如果没有,你能把它变成一般而不是Java项目吗?

#4


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 :-)

有办法和方法。选一个适合你的:-)