Forum Stats

  • 3,817,474 Users
  • 2,259,340 Discussions
  • 7,893,791 Comments

Discussions

Java Conditional Compilation

1324063
1324063 Member Posts: 2
edited Jun 27, 2017 12:12PM in Java Programming
  1. IHi All,

I need a help on java conditional compilation. Any input or reference document regarding the below requirement is greatly appreciated.

Requirement:

A provision need to be provided to the build team where they can prepare a build of a project by either removing or including certain dependent libraries of this project. When the build team chooses to remove the libraries from the class path of the project, the code snippets that depends on these libraries need to be removed from java compilation.

Thanks,

Nagaraja

Mail Id: ***Moderator action (Timo): removed private contact data as it's not allowed here.***

Answers

  • Gianni Ceresa
    Gianni Ceresa Managing Director | Oracle ACE Director Member Posts: 6,541 Blue Diamond
    edited Jun 23, 2017 6:17AM

    Hi,

    I hardly see a link with which is where you posted ...

  • Unknown
    edited Jun 23, 2017 10:34AM
    I need a help on java conditional compilation. Any input or reference document regarding the below requirement is greatly appreciated.Requirement:A provision need to be provided to the build team where they can prepare a build of a project by either removing or including certain dependent libraries of this project. When the build team chooses to remove the libraries from the class path of the project, the code snippets that depends on these libraries need to be removed from java compilation.

    Java doesn't have 'conditional compilation'.

    If you don't want libraries included don't include them when you build the project.

    If you don't want source code files includes then don't include them when you build the project.

    You need to control both of those as part of your build process. Builds are often controlled using utilities like 'Ant'.

  • 1324063
    1324063 Member Posts: 2
    edited Jun 27, 2017 6:53AM

    The current code is tightly coupled with dependent libraries API. It requires a lot of refactoring, I need to know whether there is any way like conditional compilation where providing some input/indication to compiler to skip certain code from compilation.

    Thanks,

    Nagaraja

  • handat
    handat Member Posts: 4,688 Gold Crown
    edited Jun 27, 2017 9:09AM

    Unlike C, the java compiler does not have the ability to compile conditionally. You will need to refactor your code so that the code that you want only optionally compiled is in separate .java files so the build system can compile them when needed.

    Alternatively, you could also write your code in such a way that is uses custom classloaders to load certain libraries and only if those libraries are found will the code be able to run. This is how some java applications such as IDEs allow you to dynamically add 'plugins' or 'modules'. You might also want to consider OSGi.

  • Unknown
    edited Jun 27, 2017 12:12PM
    The current code is tightly coupled with dependent libraries API. It requires a lot of refactoring, I need to know whether there is any way like conditional compilation where providing some input/indication to compiler to skip certain code from compilation.

    No - as I already answered above.

    You have two choices and can use either, or both, of them:

    1. control what is included at the 'entity' level

    2. control what is included by using a 'preprocessor' to alter source files to include content selected based on configuration settings.

    You can control what code 'entities' (jars, class files, source files, other files) are included in the build process using a tool like Ant to conditionally include different jars, files, etc based on configuration settings you also make in the build file.

    And you can include your own custom 'conditional statements' (perhaps using a custom annotation) directly in the Java source files. Then a simple utility can 'preprocess' those source files to determine what sections of the source file to include based on configuration settings you have made.

    The latter utility is similar to a macro-processor that is used as part of c/c++ compiler processing.

    The BEST solution is to write modular code that allows you to then include the modules you want to use. That is what the OSGI architecture is all about that Oracle uses for Sql Developer, JDeveloper and other tools that it develops.

    https://www.osgi.org/developer/architecture/

This discussion has been closed.