将同一包中的类构建到单独的 jar 中
我有一个服务器应用程序和两个基于 Swing 的客户端应用程序。我们正在 eclipse 中开发它们,并且每个都有一个单独的项目。
许多课程是共享的。例如,my.server
包有一些类适用于服务器和客户端,而其他类则仅适用于服务器。虽然我更喜欢将它们放在同一个包中,因为它们密切相关,并且其中一些依赖于包可见性,但我不想分发应用程序不需要的类,因为它不仅会增加文件大小,而且还会增加应用程序不需要的类。这将是一个安全风险。
目前,每个服务器和客户端都有相同的 jar,这很混乱。理想情况下,我想根据依赖关系自动创建 jar,如下所示。
服务器:
- server.jar:仅服务器使用的类
- server-client1-common.jar:服务器和客户端 1 共享的类
- server-client2-common.jar:服务器和客户端 2 共享的类
客户端 1:
- client1.jar:使用的类仅限客户端 1
- server-client1-common.jar:服务器和客户端 1 共享的类
- client-common.jar:客户端 1 和客户端 2 共享的类,但服务器不共享
客户端 2:
- client2.jar:客户端 2 使用的类仅
- server-client2-common.jar:服务器和客户端 2 共享的类
- client-common.jar:客户端 1 和客户端 2 共享的类,但不是服务器
我意识到您可以使用 ant 手动执行此操作,但这将是维修灾难。有没有一个工具可以自动处理这种依赖关系?
I have a server application and two Swing-based client applications. We're developing them in eclipse and there's a separate project for each.
Many classes are shared. For example, the my.server
package has some classes for both the server and the clients while others are for the server only. Although I prefer to put them in the same package because they are closely related and some of them rely on package visibility, I don't want to distribute classes that an application does not need as not only would it bloat the file size, but also it would be a security risk.
At the moment, each of the server and the clients has the same jars, which is a mess. Ideally, I'd like to automatically create jars based on dependency as following.
Server:
- server.jar: classes used by Server only
- server-client1-common.jar: classes shared by Server and Client 1
- server-client2-common.jar: classes shared by Server and Client 2
Client 1:
- client1.jar: classes used by Client 1 only
- server-client1-common.jar: classes shared by Server and Client 1
- client-common.jar: classes shared by Client 1 and Client 2, but not Server
Client 2:
- client2.jar: classes used by Client 2 only
- server-client2-common.jar: classes shared by Server and Client 2
- client-common.jar: classes shared by Client 1 and Client 2, but not Server
I realize that you can do this manually using ant, but it would be a maintenance disaster. Is there a tool that takes care of such dependency automatically?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
“维护灾难”是什么意思?如果您创建 ANT 脚本,只需运行它,它就会为您编译并打包 jar。
作为更强大的替代方案,您可以使用 Maven。对于更轻量级的东西,内置的 eclipse 导出工具可能会起作用。
What do you mean by "maintenance disaster"? If you create an ANT script, just run it and it will compile and pack the jars for you.
As a more robust alternative, you might use maven. For something more lightweight, the built-in eclipse export tool might work.
我无法向您提供现成的解决方案。不过,这里有一个想法:创建一个注释或一组注释,如下所示:
然后通过扩展 jar 任务(或 Maven 插件)来创建您自己的 ant 任务,或者编写您自己的任务,该任务采用此注释并根据注释打包类,然后您将其用作过滤器。
您只需创建一次任务 - 我过去已经完成过,它没有听起来那么复杂,而且问题听起来足够大,值得付出努力。
之后,您必须对所有类进行一次注释(或者根据您的实现,仅注释客户端需要的那些类,或者仅注释那些不被每个 jar 等共享的类)。任何人看到一个类都可以立即看出它的用途。创建新类时,您可以轻松添加注释。
我真的不认为有现成的 ant 任务或 Maven 插件可以做到这一点。
或者 - 如果您确实无法更改包结构 - 您也可以使用多个源目录来保留包,但将文件拆分到不同的目录中。 Eclipse 并不关心您使用了多少个源目录。然后,您只需针对源目录调整构建工具一次,然后就可以以这种方式对文件进行排序。
I cannot present you with a ready-to-go solution. Here's an idea though: create an annotation or a set of annotations like this:
Then create your own ant task by extending the jar task (or maven plugin) or write your own task, that takes this annotation and packages classes according to the annotation, which you would then be using as a filter.
You would only have to create the task once - I've done it in the past, it is less complicated than it sounds and the problem sounds big enough to warrant the effort.
Afterwards you have to annotate all your classes once (or depending on your implementation only those classes the clients need, or only those that are not shared by every jar etc.). Whoever sees a class can see immediately what it is used for. When creating a new class you can easily add the annotation.
I really don't think there is a ready made ant task or maven plugin that does this.
Alternatively - if you really cannot change your package structure - you could also use multiple source directories to keep the packages but split the files in different directories. Eclipse doesn't care how many source directories you use. You would then need to adapt your build tool just once for the source directories and could then sort the files that way.
关于构建应用程序的最佳实践之一是每个项目都有一个 jar。
例如,Maven 使用此作为默认值。你可以欺骗它做其他事情,但最好加入他们而不是“战斗”他们。
http://maven.apache.org/guides/mini/guide-using-one-source-directory.html
http://www.sonatype.com/people/2010/01/how-to-create-two-jars-from-one-project-and-why-you-shouldnt/
所以,在你的案件您应该创建 6 个项目:
Server, Client1, Client2, ServerClient1Common, ServerClient2Common, ClientCommon
为了选择所需的类,我认为没有工具,也许您应该更好地了解通用功能是什么。
创建 Common 项目,并将它们添加到构建路径 - 依赖项中。然后开始将您的类移至 Common 项目中,将它们保留在同一个包中。
例如,创建ServerClient1Common项目。
对于 Client1,转到配置构建路径 ->项目。添加ServerClient1Common。删除对服务器项目的所有引用。
对于服务器,转到配置构建路径 ->项目。添加ServerClient1Common。删除对 Client1 项目的所有引用。
您现在应该有很多缺失的类/导入。尝试一一解决。
最后,您应该能够编译 3 个项目并获得您提到的 jar。
PS:其他策略(比如一个具有不同构建目标的超级项目,或者 3 个具有缠绕的 ant/maven 构建器的项目)则更加混乱。也许有一个例外 - 另一种拆分类的方法,但我不知道它是否适用于您:client1.jar、client1-interface.jar、client2.jar、client2-interface.jar、server.jar、server-接口.jar。这样您就可以使用 3 个项目,每个项目都有两个目标 jar。要运行 client2.jar,您将需要 server-interface.jar
One of the best practices regarding building applications is to have one jar per project.
Maven, for example, uses this as default. You can trick it to do otherwise, but it is better to join them instead of "fight" them.
http://maven.apache.org/guides/mini/guide-using-one-source-directory.html
http://www.sonatype.com/people/2010/01/how-to-create-two-jars-from-one-project-and-why-you-shouldnt/
So, in your case you should create 6 projects:
Server, Client1, Client2, ServerClient1Common, ServerClient2Common, ClientCommon
In order to select the classes needed, I don't think there is a tool, and probably you should know better what is the common functionality.
Create the Common projects, and add them to the build path - dependencies. Then start moving your classes into the Common project, leaving them in the same package.
For example, create ServerClient1Common project.
For Client1, go to Configure Build Path -> Projects. Add ServerClient1Common. Remove all references to Server Project.
For Server, go to Configure Build Path -> Projects. Add ServerClient1Common. Remove all references to Client1 Project.
You should now have a lot of missing classes/imports. Try to solve them one by one.
At the end, you should be able compile the 3 projects and to obtain the jars you mentioned.
PS: Other strategies (like one uber-project with different build targets, or 3 projects with entwined ant/maven builders) are messier. There is maybe one exception - another way of splitting the classes, but I do not know if it applies to you: client1.jar, client1-interface.jar, client2.jar, client2-interface.jar, server.jar, server-interface.jar. This way you could use 3 projects with each having two target jars. To run client2.jar you will need server-interface.jar
为您要创建的每个 JAR 建立一个单独的 Eclipse 项目。然后在构建路径的“项目”选项卡上为每个顶级项目设置依赖关系。因此,“服务器”项目将“server-client1-common”和“server-client2-common”列为必需项目。等等。
我见过许多不同的组织使用这种模型,并且我一直认为这是“行业接受的”做法。它就是有效的!
Have a separate Eclipse project for each JAR that you're going to create. Then set up the dependencies on the "Projects" tab of the Build Path, for each of the top level projects. So, the "server" project will have "server-client1-common" and "server-client2-common" listed as required projects. And so on.
I've seen this model used by a number of different organisations, and I've always thought that this was the "industry accepted" way of doing it. It just works!