简体   繁体   English

Java是否具有像C#这样的“调试”和“发布”构建模式?

[英]Does Java have 'Debug' and 'Release' build mode like C#?

In C#, we have 2 modes to build projects : Debug and Release , I wonder if Java has the same thing. 在C#中,我们有两种模式来构建项目: DebugRelease ,我想知道Java是否有相同的东西。 I am using IntelliJ IDEA as Java IDE and so far I haven't seen anywhere to configure a build mode like in VS IDE. 我使用IntelliJ IDEA作为Java IDE,到目前为止我还没有看到任何地方配置VS IDE中的构建模式。

javac 
  -g                         Generate all debugging info
  -g:none                    Generate no debugging info
  -g:{lines,vars,source}     Generate only some debugging info

You can choose to include debug symbols in the compiled classes (this is the default) or to not do so. 您可以选择在已编译的类中包含调试符号(这是默认值)或不执行此操作。 There is not much benefit to not doing that. 不这样做没有多大好处。 The jar files will be a little smaller , but the performance benefit is minimal (if any). jar文件会稍微小一些 ,但性能优势很小(如果有的话)。 Without these symbols you no longer get line numbers in stack traces. 如果没有这些符号,则不再在堆栈跟踪中获取行号。 You also have the option to include additional symbols with local variable names (by default there are only source file names and line numbers). 您还可以选择包含具有局部变量名称的其他符号 (默认情况下,只有源文件名和行号)。

java
    -ea[:<packagename>...|:<classname>]
    -enableassertions[:<packagename>...|:<classname>]
                  enable assertions

You can also enable assertions at run-time (default is off), which is sometimes useful during development and testing. 您还可以在运行时启用断言(默认为关闭),这在开发和测试期间有时很有用。 This does have a performance impact (if the code in question did indeed make use of assertions, which I think is uncommon ). 这确实会对性能产生影响(如果有问题的代码确实使用了断言,我认为这种断言并不常见 )。

Regardless of any of these settings, the JVM always allows you to attach a debugger. 无论这些设置如何,JVM始终允许您附加调试器。

What Java does not have is conditional compilation where completely different code would be compiled based on some external setting. Java没有的是条件编译,其中根据某些外部设置编译完全不同的代码。 The closest you can get is something like public static final boolean DEBUG_BUILD = true; 你可以得到的最接近的是public static final boolean DEBUG_BUILD = true; somewhere in your code and use that in if statements. 代码中的某处,并在if语句中使用它。 This will actually make the compiler exclude code that becomes unreachable, but you have to set this constant in the source code. 这实际上会使编译器排除无法访问的代码,但您必须在源代码中设置此常量。

It is normal practice in Java to release everything is a manner which can be debugged. 在Java中,通常的做法是释放所有内容,这是一种可以调试的方式。 For some projects requiring obfuscation, they could have a release build, but I have never seen this in 12 years of developing Java. 对于一些需要混淆的项目,他们可能有一个发布版本,但我在12年的Java开发中从未见过这一点。

Things such as assertions and debug messages are usually turned off at runtime for a production instance but can be turned on at any time (even dynamically) if required. 断言和调试消息等事情通常在运行时为生产实例关闭,但如果需要,可以随时(甚至动态)打开。

IMHO it is best practice to use the same build in every environment, not just the same source but the same JARs. 恕我直言,最佳做法是在每个环境中使用相同的构建,而不仅仅是相同的源,而是相同的JAR。 This gives you the best chance that, if it works in test, it will work in production and if you have a problem in production, you can re-produce it in test. 这为您提供了最好的机会,如果它在测试中起作用,它将在生产中工作,如果您在生产中遇到问题,您可以在测试中重新生成它。

As so much Java code is written this way, the JIT is very good at optimising dead code which is never called. 由于以这种方式编写了如此多的Java代码,JIT非常擅长优化从未调用过的死代码。 So much so that IMHO most of the micro-"benchmarks" where Java out performs C++, is when the benchmark doesn't do any thing and the JIT is better at detecting this. 因此,恕我直言,大多数Java out out执行C ++的微观“基准测试”,当基准测试没有做任何事情并且JIT更好地检测到它时。 IMHO, C++ assumes the developer is smart enough not to write code which doesn't do anything. 恕我直言,C ++假设开发人员足够聪明,不会编写不做任何事情的代码。

You're asking for different kinds of builds to compile in different things I guess. 你想要用不同的东西来编译不同类型的构建。 For example to have Debug.WriteLine and Console.WriteLine. 例如,要有Debug.WriteLine和Console.WriteLine。

"No, Java doesn't have an exact match for that functionality. You could use aspects, or use an IOC container to inject different implementation classes." “不,Java与该功能没有完全匹配。您可以使用方面,或使用IOC容器注入不同的实现类。” stole this from the following question: Conditional Java compilation 从以下问题中偷走了这个: 条件Java编译

(there're other nice answers for you there) (那里还有其他很好的答案)

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

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