Getting rid of derby.log

最后都变了- 提交于 2019-11-29 20:27:37
stevedbrown

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.

Laurent Fournié

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

Include the following in your derby.properties file:

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

( or

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

on Windows)

Fabian

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.

You can also just set derby home to target/derby or targetvia:

System.setProperty("derby.system.home", new File("target/derby").getAbsolutePath());

and then use the JDBC URL jdbc:derby:unittest-db;create=true. Then derby.log appears in the right folder.

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");
yuvraj

I have came up with another solution. Try this out; it worked for me. What I am doing here is I have changed the System.stream.error.file path and set it to one of the properties present under my property file. Just adding the below given code to your applicationContext.xml file will work.

<bean id="setDerbyLog" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
  <property name="targetClass"><value>java.lang.System</value></property>
  <property name="targetMethod"><value>setProperty</value></property>
  <property name="arguments">
    <list>
      <value>derby.stream.error.file</value>
      <value>${derby.stream.error.file}</value>
    </list>
  </property>
</bean>
carlspring

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!