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 JDBCSystem.out.println("JDBC");
#elseSystem.out.println("other");
#endif
manifold 是极其强大的,远不止条件编译这么简单,有兴趣可以阅读以下两篇文章:
- Java 缺失的特性:扩展方法
- Java 缺失的特性:操作符重载
官方仓库