客户端/服务器项目结构、文件夹结构、构建文件等
我正在编写客户端和服务器应用程序。现在我只是自己开发它,所以我将它编写为一个 Eclipse 项目,组织成 Java 包(org.myorg.server、org.myorg.client、org.myorg.networksystem 等)。该项目部署到单个 jar 文件。然后,客户端是一个小程序,因此我只需将小程序code参数指向jar文件内的org.myorg.client.ClientApplet,而当我运行服务器时,它是一个命令行应用程序,我只需运行:
java -jar MyJar.jar org.myorg.server.ServerApplication
好吧,我需要清理它的结构并改进构建过程。我还想分离 jar 文件,以便用户无法像现在一样访问直接构建到 jar 文件中的服务器副本。
我想使用持续集成服务器,例如 CruiseControl。如果我只需要将我的代码签入 SVN 存储库,那就太酷了;然后 CruiseControl 服务器将获取代码,对其进行编译并将其打包到单独的 jar 中,然后将服务器 jar 部署到我的专用服务器上并运行它,还将客户端 jar 部署到我的 Web 服务器上,以便更新小程序。
我能弄清楚 CruiseControl。我当前的问题是如何分离代码。它很好地分为客户端和服务器包,但还有一些共享包,例如网络协议。我应该将我的项目分为客户端、服务器和公共 3 个项目吗?我应该做点别的吗?我正在考虑手动编写 Ant 构建文件,但我真的很想留在 Eclipse 中。
我怎样才能最好地组织我的代码来实现这些目标?
(顺便说一句,抱歉,如果这个问题令人困惑,我想我很难用语言来表达我脑海中盘旋的所有不同问题)
I'm writing a client and server application. Right now I've just been developing it on my own, so I have written it as one Eclipse project, organized into Java packages (org.myorg.server, org.myorg.client, org.myorg.networksystem, etc.). The project deploys to a single jar file. Then, the client is an applet, so I simply point the applet code parameter at org.myorg.client.ClientApplet inside the jar file, whereas when I run the server, it's a command line application and I just run:
java -jar MyJar.jar
org.myorg.server.ServerApplication
Well, I need to clean up its structure and improve the build process. I also want to separate the jar files, so that an user does not have access to a copy of the server built right into the jar file as it is now.
I want to use a continuous integration server such as CruiseControl. It would be really cool if I only needed to check my code into the SVN repository; then the CruiseControl server would grab the code, compile it and package it into separate jars, then deploy the server jar onto my dedicated server and run it, and also deploy the client jar onto my web server so that the applet is updated.
I can figure out CruiseControl. My current issue is how to separate the code. It's nicely separated into client and server packages, but then there are some shared packages such as the network protocol. Should I separate my project into 3 projects for client, server, and common? Should I do something else? I was considering writing Ant build files by hand, but I would really like to stay in Eclipse.
How can I best organize my code to meet these goals?
(by the way, sorry if this question is confusing, I think I'm having a hard time putting into words all of the different questions swirling around my head)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将您的项目分成 3 个独立的项目,您的做法是正确的 - 您将需要 1 个客户端、1 个服务器和 1 个公共项目。
您可以在 Eclipse 中的每个项目上定义项目间依赖关系 - 如果右键单击该项目,它位于由“配置构建路径”选项启动的对话框中。
不过,您应该考虑编写 ANT 文件来处理构建 - 稍后插入 CruiseControl 会容易得多。
You're on the right track with separating your project into 3 separate projects - you'll need 1 client, 1 server, and 1 common.
You can define inter-project dependencies on each project in Eclipse - if you right click the project, it's in the dialog launched by the "configure build path" option.
You should look into writing ANT files to handle the build though - it will be much easier to plug into CruiseControl later.
如果看起来你已经知道你需要做什么,但是,如果你想让 CruiseControl 编译并打包代码,那么你将需要使用像 Ant 这样的工具,或者可能的 Maven、Gradle 或其他一些构建工具,为了让它做你想做的事。即使您有构建工具包的代码,您仍然可以使用 Eclipse 来构建和运行所有内容。
我不确定您对 Ant 有多熟悉,但是使用 Ant 可以让您更好地控制构建过程,但需要定义过程。其他工具(如 Maven 和 Gradle)有一种规定的构建代码的方式,可以减少构建的配置量,而就 Maven 而言,我不确定是否可以改变它所期望的项目布局。另一方面,Gradle 可以更改其默认值。
就组织而言,我认为将代码分成客户端、服务器以及客户端和服务器共享的网络协议(正如您所提到的)会很好。我不使用 Eclipse,所以我不确定如何完成。您可以将客户端和服务器打包到单独的 JARS 中,并将网络协议模块中的类文件放入其中,或者只制作三个 JARS。
If seems you already know what you need to do, however, If you want CruiseControl to compile and package the code, then you will need to use a tool like Ant, or possible Maven, Gradle, or some other build tool, in order to have it do what you want. Even if you have a build tool package the code you can still use Eclipse to build and run everything.
I am not sure how familiar you are with Ant, but using Ant will give you more control over the build process at the expense of requiring defining the process. Other tools like Maven and Gradle have a prescribed way of structuring the code that reduces the amount of configuration for a build, and in the case of Maven I am not sure that one can change how it expects the project layout to be. Gradle on the other hand can have its defaults change.
As far as organization, I think separating the code into client, server, and the network protocol shared by the client and server, as you have mentioned, would be good. I don't use Eclipse, so I am not sure how that would be done. You could just package the client and server into separate JARS with the class files from the network protocol module in both, or just make three JARS.
我认为你的问题非常好。
我建议查看 maven。它专门设计用于提供通用的最佳实践工作流程,包括开发、测试和部署阶段。即使您最终不使用 Maven,它也可能会给您一些关于项目结构的想法。
Eclipse 与 ant 具有很好的集成,因此您可以使用 ant 构建/部署您的项目,并且仍然留在 Eclipse 中。
我绝对建议将您的代码分成几个 Eclipse 项目。这将强制执行正确的依赖关系:您可以确保您的客户端代码可以使用您的公共代码,并且您的服务器代码可以使用您的公共代码,但公共代码不能使用其他两个中的任何一个,同样服务器可以不要使用客户端代码。
I think your question is excellent.
I suggest looking into maven. It's specifically designed to provide common, best-practice workflow, including development, testing and deployment phases. Even if you end up not using maven, it may give you some ideas about projects structure.
Eclipse has great integration with ant, so you can use ant to build/deploy your projects and still stay in Eclipse.
I definitely suggest separating your code into several Eclipse projects. This would enforce the correct dependencies: you can make sure that your client code may use your common code, and your server code may use your common code, but the common code can't use any of the other two, and similarly the server can't use the client code.
对于大型项目,最好为每个客户端、服务器和公共项目都有单独的 Eclipse 项目。
然而,在我看来,对于一个小项目来说,这太过分了:在这种情况下,我会将其保留为一个 Eclipse 项目,但编写自定义 Ant 目标(全部在同一个 build.xml 文件中)来处理客户端 vs 服务器 jar。通用代码只会出现在两个罐子中。
但是,是的,您必须执行一些手动 Ant 工作,但无论如何,这是一项很好的技能。
For large projects, it would be better to have separate Eclipse projects for each of client, server and common.
However, that would be overkill, in my opinion, for a small project: in such a case I would keep it as one Eclipse project, but write custom Ant targets (all in the same build.xml file) to handle the packaging of the client vs the server jars. Common code would simply appear in both jars.
But yes, you would have to do some manual Ant work, but that's a good skill to have anyway.