gpt4 book ai didi

java - 通过 maven a.k.a 发布时禁用 lombok 日志记录作为 maven 驱动的功能翻转

转载 作者:行者123 更新时间:2023-11-30 11:27:10 26 4
gpt4 key购买 nike

如何在通过 maven 发布项目时优雅地禁用项目中的所有日志?

我有一个使用 Lombok 的库,是通过 maven 构建的。我使用 @log(java.util.Logger 的快捷方式)注解并希望在正常构建期间获取日志并在 lib 的发行版本中删除日志(理想情况下甚至不编译)。

提前致谢。

这个问题确实更大,它都是关于通过 maven 驱动功能翻转:根据 maven 构建周期在代码中启用或不启用功能。

最佳答案

有一种相对简单且非常有效的方法可以通过 maven 启用功能翻转:

在您的代码中,使用常量激活或不激活日志。记录调用,将在编译时删除:

static {
log.getParent().setLevel(FeatureFlip.LOG_LEVEL);
}

现在有趣的部分是根据其构建周期通过 Maven 生成此常量的值。

把这个文件放在src/main/java-templates里面

import java.util.logging.Level;

//This file will be filtered with maven properties.
public class FeatureFlip {

public static final Level LOG_LEVEL = ${log.level};
}

它将使用 templating-maven-plugin 注入(inject) maven 属性:

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>templating-maven-plugin</artifactId>
<version>1.0-alpha-3</version>
<executions>
<execution>
<id>filter-src</id>
<goals>
<goal>filter-sources</goal>
</goals>
<phase>process-sources</phase>
</execution>
</executions>
</plugin>

定义正常构建的默认值:

<log.level>Level.ALL</log.level>

在发布配置文件中为此属性定义一个新值:

<profile>
<id>release</id>
<properties>
<log.level>Level.OFF</log.level>
</properties>
</profile>

现在在 Release模式下激活此配置文件:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<configuration>
<arguments>-Prelease</arguments>
</configuration>
</plugin>

这种激活发布配置文件的方式适用于 release:prepare 和 release:perform 目标。另一种方法是仅在发布期间获取它:执行是通过以下方式激活发布配置文件:

    <activation>
<property>
<name>performRelease</name>
<value>true</value>
</property>
</activation>

但它无法通过 release:prepare -DdryRun=true 进行测试。


要获得有效的 Eclipse 配置,请添加:

    <pluginManagement>
<plugins>
<!--This plugin's configuration is used to store Eclipse m2e settings
only. It has no influence on the Maven build itself. -->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>
org.codehaus.mojo
</groupId>
<artifactId>
templating-maven-plugin
</artifactId>
<versionRange>
[1.0-alpha-3,)
</versionRange>
<goals>
<goal>filter-sources</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore></ignore>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>

完整的工作示例 here .

关于java - 通过 maven a.k.a 发布时禁用 lombok 日志记录作为 maven 驱动的功能翻转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19456107/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com