eclipse 条件 Java 编译
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1922521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Conditional Java compilation
提问by Seva Alekseyev
I'm a longtime C++ programmer, new to Java. I'm developing a Java Blackberry project in Eclipse. Question - is there a way to introduce different configuration sets within the project and then compile slightly different code based on those?
我是一名长期的 C++ 程序员,刚接触 Java。我正在 Eclipse 中开发一个 Java Blackberry 项目。问题 - 有没有办法在项目中引入不同的配置集,然后根据这些配置稍微不同的代码?
In Visual Studio, we have project configurations and #ifdef; I know there's no #ifdef in Java, but maybe something on file level?
在 Visual Studio 中,我们有项目配置和 #ifdef;我知道 Java 中没有 #ifdef,但也许是文件级别的?
回答by monojohnny
You can set up 'final' fields and ifs to get the compiler to optimize the compiled byte-codes.
您可以设置“final”字段和 ifs 以使编译器优化已编译的字节码。
...
public static final boolean myFinalVar=false;
...
if (myFinalVar) {
do something ....
....
}
If 'myFinalVar' is false when the code is compiled the 'do something....' bit will be missed out of the compiled class. If you have more than one condition - this can be tidied up a bit: shift them all to another class (say 'Config.myFinalVar') and then the conditions can all be kept in one neat place.
如果在编译代码时“myFinalVar”为假,则“做某事....”位将被编译类遗漏。如果您有多个条件 - 这可以稍微整理一下:将它们全部转移到另一个类(例如“Config.myFinalVar”),然后条件可以全部保存在一个整洁的地方。
This mechanism is described in 'Hardcore Java'.
这种机制在“Hardcore Java”中有描述。
[Actually I think this is the same mechanism as the "poor man's ifdef" posted earlier.]
[其实我觉得这和之前贴的“穷人的ifdef”是一样的机制。]
回答by Pierre
you can manage different classpath, for example, implement each 'Action' in a set of distinct directories:
您可以管理不同的类路径,例如,在一组不同的目录中实现每个“操作”:
dir1/Main.java
dir2/Action.java
dir3/Action.java
then use a different classpath for each version
然后为每个版本使用不同的类路径
javac -sourcepath dir1 -cp dir2 dir1/Main.java
or
或者
javac -sourcepath dir1 -cp dir3 dir1/Main.java
回答by Shamik
回答by Jeremy Raymond
If you want this specifically for BlackBerry, the BlackBerry JDE has a pre-processor:
如果您想专门为 BlackBerry 使用此功能,BlackBerry JDE 有一个预处理器:
You can enable preprocessing for your applications by updating the Eclipse? configuration file.
In C:\Program Files\Eclipse\configuration\config.ini, add the following line: osgi.framework.extensions=net.rim.eide.preprocessing.hook If you enable preprocessing after you have had a build, you must clean the project from the Project menu before you build the project again.
您可以通过更新 Eclipse 来为您的应用程序启用预处理吗?配置文件。
在 C:\Program Files\Eclipse\configuration\config.ini 中,添加以下行: osgi.framework.extensions=net.rim.eide.preprocessing.hook 如果在构建后启用预处理,则必须清理重新构建项目之前,从项目菜单中选择项目。
Then you can do things in the code like:
然后您可以在代码中执行以下操作:
//#ifdef SOMETHING
// do something here
//#else
// do something else
//#endif
For details see Specifying preprocessor defines
有关详细信息,请参阅指定预处理器定义
回答by miku
Can one call that a poor mans ifdef
: http://www.javapractices.com/topic/TopicAction.do?Id=64?
有人可以称之为穷人吗ifdef
:http: //www.javapractices.com/topic/TopicAction.do? Id =64 ?
回答by Nathan Hughes
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 容器来注入不同的实现类。
回答by leventov
There are a couple of projects that bring support for comment-based conditional compilation to Java:
有几个项目为 Java 带来了对基于注释的条件编译的支持:
Example in JPSG:
JPSG 中的示例:
/* with Android|Iphone platform */
class AndroidFoo {
void bar() {
/* if Android platform */
doSomething();
/* elif Iphone platform */
doSomethingElse();
/* endif */
}
}
回答by whiskeysierra
You could use maven's resource filtering in combination mit public static final fields, which will be indeed get compiled conditionally.
您可以将 maven 的资源过滤与 mit public static final 字段结合使用,这些字段确实会被有条件地编译。
private static final int MODE = ${mode};
...
if (MODE == ANDROID) {
//android specific code here
} else {
}
Now you need to add a property to your maven pom called "mode", which should be of the same value as your ANDROID constant.
现在您需要向您的 maven pom 添加一个名为“mode”的属性,该属性应该与您的 ANDROID 常量具有相同的值。
The java compiler should (!) remove the if and the else block, thus leaving your android code.
java 编译器应该 (!) 删除 if 和 else 块,从而留下您的 android 代码。
Not testet, so there is no guarantee and i would prefer configurationinstead of conditional compilation.
不是 testet,所以没有保证,我更喜欢配置而不是条件编译。
回答by seh
You can integrate m4into your build process to effectively strap an analogue to the C preprocessor in front of the Java compiler. Much hand-waving lies in the "integrate" step, but m4is the right technology for the text processing job.
您可以将m4集成到您的构建过程中,以在 Java 编译器前面有效地将模拟绑定到 C 预处理器。很多麻烦在于“集成”步骤,但m4是文本处理工作的正确技术。
回答by Hardcoded
Besides Maven, Ant and other build tools that provide similar functionality, one would rather build interfaces in Java and switch the implementations at Runtime.
See the Strategy Patternfor more details
除了 Maven、Ant 和其他提供类似功能的构建工具,人们更愿意用 Java 构建接口并在运行时切换实现。
有关更多详细信息,请参阅策略模式
In opposite to C/C++ this will not come with a big performance penality, as Javas JIT-compiler optimizes at runtime and is able to inline this patterns in most cases.
The big pro of this pattern is the flexibility - you can change the underlying Implementation without touching the core classes.
与 C/C++ 相反,这不会带来很大的性能损失,因为 Java 的 JIT 编译器在运行时进行优化,并且在大多数情况下能够内联这种模式。
这种模式的最大优点是灵活性——您可以在不触及核心类的情况下更改底层实现。
You should also check IoCand the Observer Patternfor more details.