Maven Manifold 条件编译

Maven 配置

通过 Maven 的不同 profile 实现不同环境传递不同符号。另外 lombok 可以 manifold 一同使用,见下方配置。

XML 复制代码
<properties>
    <maven.compiler.source>8</maven.compiler.source>
    <maven.compiler.target>8</maven.compiler.target>
    <lombok.version>1.18.24</lombok.version>
    <manifold.version>2023.1.11</manifold.version>
</properties>

<profiles>
    <!-- dev -->
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <profile.name>dev</profile.name>
        </properties>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.11.0</version>
                    <configuration>
                        <source>${maven.compiler.source}</source>
                        <target>${maven.compiler.target}</target>
                        <encoding>UTF-8</encoding>
                        <compilerArgs>
                            <!-- Configure manifold plugin-->
                            <arg>-Xplugin:Manifold</arg>
                        </compilerArgs>
                        <!-- Add the processor path for the plugin -->
                        <annotationProcessorPaths>
                            <path>
                                <groupId>org.projectlombok</groupId>
                                <artifactId>lombok</artifactId>
                                <version>${lombok.version}</version>
                            </path>
                            <path>
                                <groupId>systems.manifold</groupId>
                                <artifactId>manifold-preprocessor</artifactId>
                                <version>${manifold.version}</version>
                            </path>
                        </annotationProcessorPaths>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>

    <!-- jdbc -->
    <profile>
        <id>jdbc</id>
        <properties>
            <profile.name>jdbc</profile.name>
        </properties>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.11.0</version>
                    <configuration>
                        <source>${maven.compiler.source}</source>
                        <target>${maven.compiler.target}</target>
                        <encoding>UTF-8</encoding>
                        <compilerArgs>
                            <!-- Configure manifold plugin -->
                            <arg>-Xplugin:Manifold</arg>
                            <!-- 条件编译用到的符号在此处定义 -->
                            <arg>-AJDBC</arg>
                        </compilerArgs>
                        <!-- Add the processor path for the plugin -->
                        <annotationProcessorPaths>
                            <path>
                                <groupId>org.projectlombok</groupId>
                                <artifactId>lombok</artifactId>
                                <version>${lombok.version}</version>
                            </path>
                            <path>
                                <groupId>systems.manifold</groupId>
                                <artifactId>manifold-preprocessor</artifactId>
                                <version>${manifold.version}</version>
                            </path>
                        </annotationProcessorPaths>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

代码

java 复制代码
#if JDBC
    System.out.println("JDBC");
#else
    System.out.println("other");
#endif

manifold 是极其强大的,远不止条件编译这么简单,有兴趣可以阅读以下两篇文章:

  1. Java 缺失的特性:扩展方法
  2. Java 缺失的特性:操作符重载

官方仓库

相关推荐
EleganceJiaBao2 个月前
【C语言】宏定义详解
linux·c语言·c++·算法·条件编译·宏定义
丿寒风3 个月前
在 TS 中使用 Manifold 建模
3d建模·计算几何·manifold
跳坑程序员6 个月前
RUST Rover 条件编译 异常处理
条件编译·rust rover
儒雅的烤地瓜1 年前
ifndef是什么,如何使用?
uni-app·条件编译·ifndef·预处理功能·#ifndef·#endif