问题
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 root of the project. The database itself is created in the target
directory (jdbc:derby:target/unittest-db;create=true
) so that is not a problem. After consulting the reference guide I tried setting the logDevice
parameter on the JDBC url (jdbc:derby:target/unittest-db;create=true;logDevice=/mylogs
) but that seems to be for a different log, hence derby.log
still appears.
Any help is much appreciated.
回答1:
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.
回答2:
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>
回答3:
Include the following in your derby.properties file:
derby.stream.error.file=/dev/null
( or
derby.stream.error.file=\\Device\\Null
on Windows)
回答4:
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.
回答5:
You can also just set derby home to target/derby
or target
via:
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.
回答6:
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");
回答7:
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>
回答8:
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.
来源:https://stackoverflow.com/questions/1004327/getting-rid-of-derby-log