Getting rid of derby.log

前端 未结 8 785
萌比男神i
萌比男神i 2020-12-23 12:19

I\'m using the Apache Derby embedded database for unit testing in a Maven project. Unfortunately whenever I run the test I end up with the derby.log file in the

相关标签:
8条回答
  • 2020-12-23 12:28

    This is not a solution to your derby.log file problem, (which numerous people have already shown how to resolve), but rather -- a suggestion. Why not use the derby-maven-plugin for your tests? It places the derby.log file under target/derby, hence not leaving any litter.

    As described in my answer here, you can use Derby as your database via the derby-maven-plugin which I wrote and is available on GitHub and via Maven Central.

    0 讨论(0)
  • 2020-12-23 12:37

    Include the following in your derby.properties file:

    derby.stream.error.file=/dev/null
    

    ( or

    derby.stream.error.file=\\Device\\Null
    

    on Windows)

    0 讨论(0)
  • 2020-12-23 12:41

    For integration tests the situation might be a bit more tricky than the simple surefire property. Specifying the property derby.stream.error.file in the maven-failsafe-plugin will not work as the server environment does not inherit from that plugin (obviously using maven-surefire-plugin makes no differences).

    Instead you need to modify the actual server start plugin. The following example is for the maven-jetty-plugin:

    <plugin>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <configuration>
            <systemProperties>
                <!-- Get rid of that missplaced derby.log. -->
                <systemProperty>
                    <name>derby.stream.error.file</name>
                    <value>${project.build.directory}/derby.log</value>
                </systemProperty>
            </systemProperties>
        </configuration>
    </plugin>
    

    Note that for some reason we use systemProperty and not just property as in the surefire solution.

    0 讨论(0)
  • 2020-12-23 12:41

    If you don't have access to the configuration, you can execute this before making the connection:

    System.setProperty("derby.stream.error.field", "MyApp.DEV_NULL");
    
    0 讨论(0)
  • 2020-12-23 12:46

    You can get rid of derby.log file by creating the following class

    public class DerbyUtil {
        public static final OutputStream DEV_NULL = new OutputStream() {
            public void write(int b) {}
        };
    }
    

    and setting the JVM system property derby.stream.error.field, for example, using the following JVM command-line argument:

    -Dderby.stream.error.field=DerbyUtil.DEV_NULL
    

    Credit to whom it is due.

    0 讨论(0)
  • 2020-12-23 12:46

    Derby lets you specify the name of the file to which the error log messages are written using the Java system property derby.stream.error.file. The default value is 'derby.log'.

    To get rid of derby.log during the Maven surefire testing phase, I just add the property definition in the plugin configuration as follows:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
            <systemProperties>
                <property>
                    <name>derby.stream.error.file</name>
                    <value>target/derby.log</value>
                </property>
            </systemProperties>
        </configuration>
    </plugin>
    
    0 讨论(0)
提交回复
热议问题