Skip to content

tejas-warake/java-preprocessing-lib

Repository files navigation

Java Preprocessing Library

A lightweight Java library and Maven plugin that brings C++ style preprocessor directives (#if, #ifdef, etc.) to Java. This allows for conditional compilation based on build-time symbols, enabling feature toggles, multi-version builds, and platform-specific code generation.

Features

  • Standard Directives: Support for #if, #ifdef, #ifndef, #else, #endif.
  • Expression Evaluation: Support for complex boolean expressions in #if directives (e.g., #if DEBUG && VERSION > 1).
  • Maven Plugin: Seamless integration into the Maven build lifecycle (generate-sources).
  • Zero Runtime Dependencies: The preprocessor runs at build time; the generated code is pure Java with no extra runtime requirements.

Installation

Maven Central

Add the following dependency to your pom.xml:

<dependency>
    <groupId>io.github.tejas-warake</groupId>
    <artifactId>preprocessor-maven-plugin</artifactId>
    <version>1.0.0</version>
</dependency>

Local Build

If you want to build from source:

git clone https://github.com/tejas-warake/java-preprocessing-lib.git
cd java-preprocessing-lib
mvn install

Usage

Maven Configuration

Add the preprocessor-maven-plugin to your pom.xml. It should be configured to run during the generate-sources phase.

Note: To use the preprocessor effectively, it is recommended to keep your source files with directives in a separate directory (e.g., src/main/preprocess) to prevent the standard Java compiler from trying to compile the unprocessed files.

<build>
    <plugins>
        <plugin>
            <groupId>io.github.tejas-warake</groupId>
            <artifactId>preprocessor-maven-plugin</artifactId>
            <version>1.0.0</version>
            <executions>
                <execution>
                    <id>preprocess-sources</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>process</goal>
                    </goals>
                    <configuration>
                        <!-- Directory containing source files with directives -->
                        <sourceDirectory>${project.basedir}/src/main/preprocess</sourceDirectory>
                        
                        <!-- Define symbols for conditional compilation -->
                        <symbols>
                            <DEBUG>true</DEBUG>
                            <VERSION>2</VERSION>
                            <FEATURE_X>false</FEATURE_X>
                        </symbols>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Supported Directives

#ifdef / #ifndef

Checks if a symbol is defined (or not defined).

#ifdef DEBUG
    System.out.println("Debug logging enabled");
#endif

#ifndef PRODUCTION
    // Test helper code
#endif

#if / #else / #endif

Evaluates a boolean expression. Supports &&, ||, !, >, <, >=, <=, ==, !=, and defined().

#if VERSION > 1 && defined(FEATURE_NEW_UI)
    renderNewUI();
#else
    renderClassicUI();
#endif

#else

Alternative branch for #if, #ifdef, or #ifndef.

Example

Input (src/main/preprocess/com/example/App.java):

package com.example;

public class App {
    public static void main(String[] args) {
        #if DEBUG
        System.out.println("Debugging...");
        #endif
        
        System.out.println("Running App");
    }
}

Configuration (pom.xml):

<symbols>
    <DEBUG>true</DEBUG>
</symbols>

Generated Output (target/generated-sources/preprocessed/com/example/App.java):

package com.example;

public class App {
    public static void main(String[] args) {
        
        System.out.println("Debugging...");
        
        
        System.out.println("Running App");
    }
}

License

MIT License

About

Library to preprocess the java code using directives.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages