Uruchamianie serwera bazy danych H2 od Maven?
Załóżmy, że chcę utworzyć i użyć bazy danych H2 do moich testów integracyjnych.
Maven posiada polecenie do uruchamiania testów: mvn test
.
Czy istnieje sposób, aby powiedzieć mavenowi, aby uruchomił serwer bazy danych H2 do testów i zatrzymał go po zakończeniu?
Wyobrażam sobie, że działa to podobnie do tego, jak mogę uruchomić tomcat za pomocą polecenia Maven (mvn tomcat:run
).
przepraszam, jeśli to pytanie jest bezsensowne, wciąż owijam w głowie nowe koncepcje.
9 answers
Udało mi się uruchomić go bez użycia zewnętrznego serwera, dodając zależność do H2 przez Mavena, a następnie używając tej fasoli:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.h2.Driver"/>
<property name="url" value="jdbc:h2:file:h2\db"/>
<property name="username" value="sa"/>
<property name="password" value=""/>
</bean>
Z drugiej strony, wymagało to użycia pliku DB zamiast w pamięci. Ale to działa.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-02-08 06:53:57
Możesz utworzyć 2 małe klasy z głównymi metodami, które uruchamiają i zatrzymują bazę danych. chodzi o uruchomienie klasy StartServer przed uruchomieniem testów integracyjnych, a następnie klasy StopServer po uruchomieniu testów.
Powinieneś zrobić to samo dla swojego serwera DB, jak opisano gdzieś w ten dokument (Opis służy do uruchamiania i zatrzymywania Jetty w testach integracyjnych)
W Twoim pom.xml należy zdefiniować maven-exec-plugin, aby uruchomić Exec: java cel i utwórz 2 egzekucje (1 dla wywołania StartServer i 1 dla StopServer):
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1.1</version>
<executions>
<execution>
<!-- start server before integration tests -->
<id>start</id>
<phase>pre-integration-test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>com.foo.StartServer</mainClass>
</configuration>
</execution>
<execution>
<!-- stop server after integration tests -->
<id>stop</id>
<phase>post-integration-test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>com.foo.StopServer</mainClass>
</configuration>
</execution>
</executions>
</plugin>
Hope that ' s what you want
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-02-05 13:24:27
Ta wtyczka działa poprawnie, aby wywołać nowy H2 DB z trybem tcp przed testami Integracyjnymi (domyślna Faza wtyczki): H2-maven-plugin na github
Nie jest to dobrze udokumentowane, ale możesz sprawdzić Źródła Mojo, aby poznać opcje konfiguracji. Jest publikowany na maven central.
Zasadniczo, do testów integracyjnych, możesz chcieć, aby Maven:
- zarezerwować losowo dostępne porty sieciowe dla serwera Tomcat i H2 (aby uniknąć portu konflikty)
- uruchom serwer H2
- uruchom serwer Tomcat
- Uruchom testy integracyjne
- Zatrzymaj serwer Tomcat
- Zatrzymaj Serwer H2
Można to osiągnąć, gdy konfiguracja Mavena wygląda tak. Zakładając, że testy integracyjne są przypisane do niestandardowego interfejsu JUnit Category:
@Category(IntegrationTest.class)
Ta konfiguracja Mavena działa mi dobrze:
<profile>
<id>it</id>
<build>
<plugins>
<!-- Reserve randomly available network ports -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>reserve-network-port</id>
<goals>
<goal>reserve-network-port</goal>
</goals>
<phase>process-resources</phase>
<configuration>
<portNames>
<portName>tomcat.test.http.port</portName>
<portName>h2.test.tcp.port</portName>
</portNames>
</configuration>
</execution>
</executions>
</plugin>
<!-- Start H2 before integration tests, accepting tcp connections on the randomly selected port -->
<plugin>
<groupId>com.edugility</groupId>
<artifactId>h2-maven-plugin</artifactId>
<version>1.0</version>
<configuration>
<port>${h2.test.tcp.port}</port>
</configuration>
<executions>
<execution>
<id>Spawn a new H2 TCP server</id>
<goals>
<goal>spawn</goal>
</goals>
</execution>
<execution>
<id>Stop a spawned H2 TCP server</id>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Start Tomcat before integration tests on the -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<systemProperties>
<spring.profiles.active>integration_tests</spring.profiles.active>
<httpPort>${http.test.http.port}</httpPort>
<h2Port>${h2.test.tcp.port}</h2Port>
</systemProperties>
<port>${http.test.http.port}</port>
<contextFile>src/main/java/META-INF/tomcat/webapp-test-context-using-h2.xml</contextFile>
<fork>true</fork>
</configuration>
<executions>
<execution>
<id>run-tomcat</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
<execution>
<id>stop-tomcat</id>
<phase>post-integration-test</phase>
<goals>
<goal>shutdown</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
</dependency>
</dependencies>
</plugin>
<!-- Run the integration tests annotated with @Category(IntegrationTest.class) -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<!-- Bug in 2.12.x -->
<version>2.11</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>2.12.4</version>
</dependency>
</dependencies>
<configuration>
<groups>com.mycompany.junit.IntegrationTest</groups>
<failIfNoTests>false</failIfNoTests>
<junitArtifactName>junit:junit-dep</junitArtifactName>
<systemPropertyVariables>
<httpPort>${tomcat.test.http.port}</httpPort>
<h2Port>${h2.test.tcp.port}</h2Port>
</systemPropertyVariables>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
Możesz użyć filtrów Mavena w pliku kontekstowym tomcat, aby że port został zastąpiony:
<contextFile>src/main/java/META-INF/tomcat/webapp-test-context-using-h2.xml</contextFile>
Z zawartością pliku:
<Resource name="jdbc/dataSource"
auth="Container"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="30"
maxWait="10000"
username=""
password=""
driverClassName="org.h2.Driver"
url="jdbc:h2:tcp://localhost:${h2.test.tcp.port}/mem:db;DB_CLOSE_ON_EXIT=FALSE;MODE=MySQL"/>
Lub jeśli nie chcesz źródła danych JNDI, możesz użyć zadeklarowanego źródła danych Springa, używając tej samej właściwości...
Jedna dodatkowa podróż, jeśli chcesz skonfigurować testy integracyjne tomcat i uruchomić testy integracyjne z twojego IDE:
Możesz użyć właściwości użyj do rozwidlenia lub nie serwera Tomcat:
<fork>${integrationTestsForkTomcatJvm}</fork>
Gdy ustawisz fork = false, serwer zablokuje i maven nie Kontynuuj, więc testy integracyjne nie zostaną uruchomione, ale będziesz mógł je uruchomić z twojego ide.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-04-19 09:05:26
Właśnie rozpocząłem projekt wtyczki H2 dla maven @ bitbucket. Będę wdzięczny za każdą pomoc.
Https://bitbucket.org/dohque/maven-h2-plugin
Mam nadzieję, że to będzie pomocne.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-02-24 19:29:03
W moim projekcie, do testów jednostkowych, poprosiłem Springa, aby zajął się tworzeniem i inicjalizacją bazy danych. Jak podano w dokumentacji H2 , możesz utworzyć dla tego bean:
<bean id = "org.h2.tools.Server"
class="org.h2.tools.Server"
factory-method="createTcpServer"
init-method="start"
destroy-method="stop">
<constructor-arg value="-tcp,-tcpAllowOthers,true,-tcpPort,8043" />
</bean>
Po prostu musisz uruchomić kontekst Springa z tą konfiguracją podczas uruchamiania testów jednostkowych.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-02-05 07:13:59
Przed uruchomieniem testów jednostkowych tworzę bazę danych H2 opartą na plikach. Plik znajduje się w katalogu target
i może zostać usunięty w dowolnym momencie za pomocą mvn clean
.
Używam maven-SQL-plugin w następujący sposób:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sql-maven-plugin</artifactId>
<version>1.5</version>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.3.166</version>
</dependency>
</dependencies>
<configuration>
<driver>org.h2.Driver</driver>
<url>jdbc:h2:file:target/db/testdb</url>
<username>sa</username>
<password></password>
<autocommit>true</autocommit>
<skip>${maven.test.skip}</skip>
</configuration>
<executions>
<execution>
<id>create-db</id>
<phase>process-test-resources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<srcFiles>
<srcFile>${sql.dir}/drop_db.sql</srcFile>
<srcFile>${sql.dir}/tables.sql</srcFile>
<srcFile>${sql.dir}/constraints.sql</srcFile>
... etc ...
</srcFiles>
</configuration>
</execution>
</executions>
</plugin>
Bazę danych można utworzyć uruchamiając mvn process-test-resources
. Po uruchomieniu testów upewnij się, że łączysz się z bazą danych w target/db/testdb
za pomocą właściwości hibernate.
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close"
p:driverClassName="org.h2.Driver"
p:url="jdbc:h2:file:target/db/testdb"
p:username="sa"
p:password="" />
Będziesz również musiał uzależnić się od com.h2database.h2 w zależnościach Mavena.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-04-17 10:38:17
Jeśli chcesz zapisać go w pamięci, po prostu użyj innego adresu URL:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.h2.Driver"/>
<property name="url" value="jdbc:h2:mem:db"/>
<property name="username" value="sa"/>
<property name="password" value=""/>
</bean>
Możesz podać dodatkowe opcje, takie jak:; DB_CLOSE_DELAY=-1
Zobacz: http://www.h2database.com/html/features.html#in_memory_databases
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-12-01 15:26:51
Ponieważ H2 nie dostarcza wtyczki Maven, powinieneś uruchomić ją za pomocą maven-antrun-plugin. Napisz kod dla start and stop H2 engine W Ant task i wywołaj go, gdy test integracyjny rozpocznie się i zakończy.
Zobacz szczegóły na http://docs.codehaus.org/display/MAVENUSER/Maven+and + Integration + Testing
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-02-05 05:02:36
Following does the job for me (just using h2
dependency and the exec-maven-plugin
):
<build>
<plugins>
<!-- start/stop H2 DB as a server -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<id>start-h2</id>
<phase>pre-integration-test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>org.h2.tools.Server</mainClass>
<arguments>
<argument>-tcp</argument>
<argument>-tcpDaemon</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>stop-h2</id>
<phase>post-integration-test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>org.h2.tools.Server</mainClass>
<arguments>
<argument>-tcpShutdown</argument>
<argument>tcp://localhost:9092</argument>
</arguments>
</configuration>
</execution>
</executions>
<configuration>
<includeProjectDependencies>true</includeProjectDependencies>
<includePluginDependencies>true</includePluginDependencies>
<executableDependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</executableDependency>
</configuration>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.3.173</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
Proszę zauważyć, że w moim pom.xml
com.h2database:h2
nie było zależnością od projektu.
W przypadku, gdy masz go może nie trzeba wyraźnie nazwać go jako zależność wtyczki.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-04-07 23:12:40