Maven Manifold 条件编译

Maven 配置

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

<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>

代码

#if JDBC
    System.out.println("JDBC");
#else
    System.out.println("other");
#endif

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

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

官方仓库

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值