Search code examples
javamavenmaven-pluginlogbackmaven-resources-plugin

Maven delete resources after testing


I recently read you can have a logback.xml and a logback-test.xml in your classpath, where the test file has higher priority.

  1. Logback tries to find a file called logback-test.xml in the classpath.

  2. If no such file is found, logback tries to find a file called logback.groovy in the classpath.

  3. If no such file is found, it checks for the file logback.xml in the classpath..

Source

So I thought it would be a great idea letting logging happen in the console while testing and log to a file after buildung with maven (without having to change the output manually). I found the maven-resources-plugin, which can <exclude> some resources. I specified test files (like logback-test.xml) like this in the plugin:

pom.xml

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <configuration>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <excludes>
                    <exclude>test/**</exclude>
                    <exclude>*test*</exclude>
                </excludes>
            </resource>
        </resources>
    </configuration>
</plugin>

Which works great, but has one problem. I definitely need access to the *test* files (yes, also logback-test.xml, so I cannot just exclude only it instead of the wildcard *test*) and the test/** directory during tests. I only want to exclude/delete them after testing is complete. With this configuration the excluded resources are never copied, but I want them to the copied first (to make them accessible by tests) and then (after tests run successfully), delete them.

How can I achieve this? I've been lookung for a "maven delete plugin" but couldn't find any.


Solution

  • I found my own solution.

    Delete the maven-resource-plugin. Instead, based on your packaging, use maven-jar-plugin or maven-war-plugin which is responsible for building the jar or war file (and runs later, after the tests).


    jar

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.0.2</version>
        <configuration>
            <excludes>
                <exclude>test/**</exclude>
                <exclude>*test*</exclude>
            </excludes>
        </configuration>
    </plugin>
    

    war

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.2.0</version>
        <configuration>
            <packagingExcludes>${webdir}/test/**,${webdir}/*test*</packagingExcludes>
        </configuration>
    </plugin>
    

    where webdir is the path to your classpath root from the built war file. You can add it to your <properties> tag (directly under the <project> tag). In my case it is WEB-INF/classes.

    pom.xml

    <properties>
        <webdir>WEB-INF/classes</webdir>
    </properties>