将 API 代码与类一起放入 JAR 中是否有任何缺点?

发布于 2024-09-02 17:23:36 字数 395 浏览 4 评论 0原文

在 Java 中,如果将源代码 (.java) 文件与类 (.class) 一起打包到 jar 中,大多数 IDE(例如 Eclipse)将显示用于代码完成的 javadoc 注释。

IIRC 很少有开源项目能像 JMock 那样做到这一点。

可以说,我已经将 API 代码与实现代码完全分离,这样我就有了 myproject-api.jar 和 myproject-impl.jar 之类的东西,我有什么理由不应该将源代码放入 myproject-api.jar 中?

因为性能?尺寸?

为什么其他项目不这样做?

编辑:除了 Maven 下载问题之外,将我的源代码放入类 jar 中以支持尽可能多的开发人员(无论是否为 Maven)是否会造成任何伤害?

In Java if you package the source code (.java) files into the jar along with classes (.class) most IDE's like eclipse will show the javadoc comments for code completion.

IIRC there are few open-source projects that do this like JMock.

Lets say I have cleanly separated my API code from implementation code so that I have something like myproject-api.jar and myproject-impl.jar is there any reason why I should not put the source code in my myproject-api.jar ?

Because of Performance? Size?

Why don't other projects do this?

EDIT: Other than the Maven download problem will it hurt anything to put my sources into the classes jar to support as many developers as possible (maven or not)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自在安然 2024-09-09 17:23:36

通常是由于分发原因:

如果您保留单独的二进制文件和源代码,则只能下载您需要的内容。
例如:

  • myproject-api.jarmyproject-impl.jar
  • myproject-api-src.jarmyproject-impl- src.jar
  • myproject-api-docs.zipmyproject-impl-docs.zip

现在,m2eclipse - Maven for Eclipse 可以 自动下载源

mvn eclipse:eclipse -DdownloadSources=true -DdownloadJavadocs=true 

现在,它还可以 生成正确的pom当任何人声明对您的 jar 的依赖项时,防止分发源或 javadoc jar。
OP评论:

也无法想象下载大小是一个问题(我的意思是现在是 2010 年,几个 100k 应该不是问题)。

好吧,实际上它(即“大小”)是一个问题。
Maven 已经患有“第一次构建时下载一半互联网”综合症。
如果还下载源代码和/或 javadocs,那就真的很烦人了。

另外,“分发”方面包括部署:在 webapp 服务器,部署一个包含源代码的 jar 并没有真正的优势

最后,如果您确实需要将源代码与二进制文件关联起来,那么SO关于 Maven 的问题可能会有所帮助

Generally because of distribution reason:

if you keep separate binaries and sources, you can download only what you need.
For instance:

  • myproject-api.jar and myproject-impl.jar
  • myproject-api-src.jar and myproject-impl-src.jar
  • myproject-api-docs.zip and myproject-impl-docs.zip

Now, m2eclipse - Maven for Eclipse can download sources automatically as well

mvn eclipse:eclipse -DdownloadSources=true -DdownloadJavadocs=true 

Now, it can also generate the right pom to prevent distribution of the source or javadoc jar when anyone declare a dependency on your jar.
The OP comments:

also can't imagine download size being an issue (i mean it is 2010 a couple 100k should not be a problem).

Well actually it (i.e. "the size) is a problem.
Maven suffers already from the "downloading half the internet on first build" syndrome.
If that downloads also sources and/or javadocs, that begins to be really tiresome.

Plus, the "distribution" aspect includes the deployment: in a webapp server, there is no real advantage to deploy a jar with sources in it.

Finally, if you really need to associate sources with binaries, this SO question on Maven could help.

帅哥哥的热头脑 2024-09-09 17:23:36

使用 Maven,自动附加源,如下所示:

http://maven。 apache.org/plugins/maven-source-plugin/usage.html

和 javadoc 像这样:

http://maven.apache.org/plugins/maven-javadoc-plugin/jar-mojo.html

拾取

mvn eclipse:eclipse -DdownloadSources=true -DdownloadJavadocs=true 

这样它们就会自动被 m2eclipse

Using maven, attach the sources automatically like this:

http://maven.apache.org/plugins/maven-source-plugin/usage.html

and the javadocs like this:

http://maven.apache.org/plugins/maven-javadoc-plugin/jar-mojo.html

That way they will automatically be picked up by

mvn eclipse:eclipse -DdownloadSources=true -DdownloadJavadocs=true 

or by m2eclipse

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文