Pregunta

How can I elegantly disable all logs from my project when releasing it via maven ?

I got a library using Lombok, built via maven. I use the @log (shortcut for java.util.Logger) annotation and want to get logs during normal builds and get the logs removed (ideally even not compiled in) in the release version of the lib.

Thx in advance.

This question is indeed larger, it's all about driving feature flipping via maven : enabling or not features in your code based on maven build cycle.

¿Fue útil?

Solución

There is a relatively simple and very efficient way to enable feature flipping via maven :

In your code, activate or not the logs using a contant. Logs invocation, will be erased at compile time :

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

The funny part is now to generate the value of this constant via maven depending on its build cycle.

Put this file inside in 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};
}

It will get injected with the maven property using templating-maven-plugin :

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

Define the default value for normal builds :

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

Define a new value for this property in a release profile :

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

Now activate this profile in release mode :

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

This way of activating the release profile works for both release:prepare and release:perform goal. An alternative, to get it only during release:perform is to activate the release profile with :

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

But it will not be testable via release:prepare -DdryRun=true.


To get a working Eclipse configuration, add :

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

Full working example here.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top