1

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.

Community
  • 1
  • 1
Impulse The Fox
  • 2,638
  • 2
  • 27
  • 52

2 Answers2

2

Things are much simpler.
Maven separate sources/resources for the packaged application and sources/resources for the execution tests.
Simply move logback-test.xml in the src/test/resources folder. And place logback.xml in src/main/resources.

In this way, logback-test.xml will be available during the tests of your build.
And as the file is located in src/test/resources, it will never be included it in your application.
While the packaged application will contain and use only logback.xml as defined in src/main/resources.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • Okay, that's good to know. But when the test files are only in `resources/test/` they are not available in normal run. I want the Application Tests to use exactly the same resources as a "normal" program run, but when I build with maven, I want none of the test files to appear in the jar. Or am I fundamentally doing something wrong here? – Impulse The Fox Mar 29 '18 at 07:57
  • 1
    I have the feeling that what you call "tests" are not unit tests in `src/test/java` but manual tests in `src/main/java`. Is it the case ? – davidxxx Mar 29 '18 at 18:09
  • No, my tests are in `src/test/java/[package]/[Name]ApplicationTests.java` – Impulse The Fox Mar 31 '18 at 07:48
  • Clarification: I do have automated Tests in `src/test/java/[package]/[Name]ApplicationTests.java` but yes, I **also** do test manually sometimes. Is this a bad thing to do? – Impulse The Fox Apr 20 '18 at 12:26
0

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>
Impulse The Fox
  • 2,638
  • 2
  • 27
  • 52