简体   繁体   English

将项目打包成JAR的最佳实践(及其影响)是什么?

[英]What is best practice (and implications) for packaging projects into JAR's?

What is considered best practice deciding how to define the set of JAR's for a project (for example a Swing GUI)? 决定如何为项目定义JAR集合(例如Swing GUI)的最佳实践是什么? There are many possible groupings: 有许多可能的分组:

  • JAR per layer (presentation, business, data) 每层JAR(演示文稿,业务,数据)
  • JAR per (significant?) GUI panel. JAR per(重要?)GUI面板。 For significant system, this results in a large number of JAR's, but the JAR's are (should be) more re-usable - fine-grained granularity 对于重要的系统,这导致大量的JAR,但JAR是(应该)更可重用 - 细粒度的粒度
  • JAR per "project" (in the sense of an IDE project); 每个“项目”的JAR(在IDE项目的意义上); "common.jar", "resources.jar", "gui.jar", etc “common.jar”,“resources.jar”,“gui.jar”等

    I am an experienced developer; 我是一位经验丰富的开发人 I know the mechanics of creating JAR's, I'm just looking for wisdom on best-practice. 我知道创建JAR的机制,我只是在寻求最佳实践的智慧。

    Personally, I like the idea of a JAR per component (eg a panel), as I am mad-keen on encapsulation, and the holy-grail of re-use accross projects. 就个人而言,我喜欢每个组件(例如一个小组)的JAR 的想法 ,因为我对封装很疯狂,并且在项目中重复使用的圣杯。 I am concerned, however, that on a practical, performance level, the JVM would struggle class loading over dozens, maybe hundreds of small JAR's. 但是,我担心的是,在实际的性能级别上,JVM会在几十甚至几百个小型JAR上加载类加载。 Each JAR would contain; 每个JAR都包含; the GUI panel code, necessary resources (ie not centralised) so each panel can stand alone. GUI面板代码,必要的资源(即不集中),因此每个面板可以独立。

    When I say "holy grail of reuse", I say this more because it demonstrates a cleanly decoupled, encapsulated design, rather than necessarily expecting its re-use elsewhere. 当我说“再利用的圣杯”时,我更多地说这是因为它展示了一种干净的解耦,封装的设计,而不是一定期望它在其他地方重复使用。 I consider myself a "normally intelligent" person; 我认为自己是“通常聪明”的人; I consider the spagetti of intertwined nonsense I've had to deal with during my career slows me down 10 to 100-fold. 我认为在我的职业生涯中我必须处理的交织在一起的废话的速度让我减慢了10到100倍。 A cleanly decoupled design allows me to deal with one concept at a time, one layer, one class. 一个干净的分离设计允许我一次处理一个概念,一层,一个类。

    Does anyone have wisdom to share? 有没有人有智慧分享?

  • I would recommend as fewer JARs as possible. 我建议尽量减少JAR。

    The logic behind it, the disk storage is the cheapest commodity there available, but time spending tracing down complex dependencies is priceless. 它背后的逻辑,磁盘存储是可用的最便宜的商品,但追踪复杂依赖的时间花费是无价的。

    Hence the emergence of the .war files where all dependencies of the web application are put into a single file. 因此出现了.war文件,其中Web应用程序的所有依赖项都放在一个文件中。

    BTW, Eclipse has a JAR exporter plugin which puts all dependent jars into a super jar and expose the entry level main method, so you can start your app with java -jar file.jar command. 顺便说一下,Eclipse有一个JAR导出器插件,它将所有依赖的jar放入一个超级jar并公开入门级main方法,这样你就可以用java -jar file.jar命令启动你的应用程序。 Although the resultant jar may be large, the flip side is not maintaining very complex class paths for you application. 尽管生成的jar可能很大,但是反面并没有为您的应用程序维护非常复杂的类路径。

    So, in your case I would go with one jar per project. 所以,在你的情况下,我会在每个项目中使用一个jar。 If you determine that you indeed need to reuse some code in another project, just refactor it into the base project and make it a dependency in your existent project and another project. 如果确定确实需要在另一个项目中重用某些代码,只需将其重构为基础项目,并使其成为现有项目和另一个项目的依赖项。

    You can actually use both approaches. 您实际上可以使用这两种方法。 Spring for example offers a big monolithic jar file, which contains most common functionality. 例如,Spring提供了一个大型的整体jar文件,其中包含最常见的功能。 If you want however you can also download independent jar files. 如果您需要,您也可以下载独立的jar文件。 It is then left to the user to select what is best. 然后由用户选择最佳选择。 Big jar files are easier to deploy, but they are harder to upgrade. 大jar文件更易于部署,但它们更难升级。 Also you may need to add a big jar whereas you only need a simple class. 此外,您可能需要添加一个大罐子,而您只需要一个简单的类。 I find that is is easier to spot dependencies with small jar files. 我发现用小jar文件更容易发现依赖关系。 I also thinK that updating/upgrading is easier. 我也知道更新/升级更容易。

    Java provides encapsulation and re-use at the class layer - the jar file format doesn't really provide it. Java在类层提供封装和重用 - jar文件格式并不真正提供它。 I don't see much advantage in putting a significant component in its own jar, unless you think lots of people will be downloading it. 除非你认为很多人会下载它,否则我认为将一个重要组件放在自己的jar中并没有多大优势。

    I read somewhere (and I was trying to find it when I found this) that project per layer is the best. 我在某个地方读过(当我发现这个时我试图找到它)​​每层的项目是最好的。 It's what I've been doing. 这就是我一直在做的事情。 Struts, Spring MVC, Swing, whatever in one layer, EJBs in another, business services in another and DAOs in another. Struts,Spring MVC,Swing,一层中的任何东西,另一层中的EJB,另一层中的业务服务以及另一层中的DAO。 I put all of the DTOs in its own project as well, even though they don't represent a layer, but are instead passed through the layers. 我将所有DTO放在它自己的项目中,即使它们不代表一个层,而是通过这些层。 The main benefit I remember reading about was being able to version each jar separately. 我记得读到的主要好处是能够分别对每个罐子进行版本化。 Oh, and BTW, each layer actually has two jars, one for the interfaces that the layer above uses, and another for the implementation(s). 哦,BTW,每层实际上有两个罐子,一个用于上面层使用的接口,另一个用于实现。

    声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM