简体   繁体   English

将源代码包含在JAR文件中有什么弊端?

[英]What are the downsides to including source code in JAR files?

To assist in our ability to step through code when debugging I'd like to recommend our JAR files be built to include source code. 为了帮助我们在调试时逐步执行代码,我建议您将我们的JAR文件构建为包含源代码。 I'm wondering what issues that might cause. 我想知道可能会导致什么问题。 My immediate thought was that the JAR files would be slightly larger. 我立即想到的是JAR文件会更大。 I can live with that. 我可以忍受这一点。 Are there other issues I need to consider? 我还需要考虑其他问题吗?

TIA TIA

Normally you separate compiled code and source code into two different jars. 通常,您将编译后的代码和源代码分为两个不同的jar。

Then you can attach the source-code jar in your IDE when you need to debug the code. 然后,当您需要调试代码时,可以将源代码jar附加到IDE中。

A build tool like maven will easily do that for you. 像maven这样的构建工具将轻松为您完成此任务。

I highly recommend it. 我强烈推荐它。 I do this and so do some open source projects (eg jMock, Hamcrest, GWT). 我这样做,一些开源项目(例如jMock,Hamcrest,GWT)也是如此。

It saves fussing about with separate source jars. 它省去了使用单独的源jar的麻烦。 It also means that if at some point in the future the source project is lost (as sometimes happens in large organizations) the maintenance programmers down the line will have a fighting chance of recreating it. 这也意味着,如果将来某个时候源项目丢失(就像大型组织中有时发生的那样),那么下游的维护程序员将有很大的机会重新创建它。

If you're happy that those people with access to the jar can see your source code and are prepared to pay the penalty of an increase in jar size (and a probably negligible increase in the time to create, transfer and deploy the jar) then that's all you need to worry about. 如果您对那些可以访问jar的人感到满意,并且愿意为增加jar的大小所付出的代价(以及创建,传输和部署jar的时间增加可以忽略不计)付出了代价,那么这就是您需要担心的。 There aren't any other issues as far as I am aware. 据我所知,没有其他问题。

Just because the Maven standard is to keep source files separate doesn't mean you have to do it that way. 仅仅因为Maven标准是将源文件分开,并不意味着您必须这样做。

I would normally create a separate source .jar. 我通常会创建一个单独的源.jar。 Maven can easily create this , and IDEs know how to access/download these. Maven可以轻松创建它 ,并且IDE知道如何访问/下载它们。

Some tools in the java space will take the source files from the classpath (ie from within the jar) and do nasty things. Java空间中的某些工具将从类路径(即从jar内)获取源文件并执行令人讨厌的事情。

Typically where you see this is if you add a dependency on a jar that does this... then you end up with those source files being compiled again into the dependent project and bundled in its jar and then all classloader hell breaks loose as classes get loaded from one source and another. 通常情况下,您看到的是在要执行此操作的jar上添加依赖项...然后最终将那些源文件再次编译到依赖项目中,并捆绑在其jar中,然后所有类加载器在类获取时会崩溃从一个来源和另一个来源加载。

Just don't do it. 只是不要这样做。 Follow the Maven way (even if not using Maven) and create a separate -sources.jar 遵循Maven的方式(即使不使用Maven)并创建一个单独的-sources.jar

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

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