2

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

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

提前谢谢。

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

4

1 回答 1

1

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

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

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

它将使用 template-maven-plugin 注入 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>

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

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

这种激活发布配置文件的方式适用于发布:准备和发布:执行目标。另一种方法是仅在发布期间获取它:执行是使用以下命令激活发布配置文件:

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

完整的工作示例在这里

于 2013-10-18T21:46:41.413 回答