टी एल; डॉ संस्करण: मैं Maven मोजो एसक्यूएल प्लगइन का उपयोग करने के बनाने में सक्षम होना चाहता हूँ/होगा पर मेरी DB स्कीमा (या उन तालिकाओं के लिए डेटा लोड) में किसी भी तालिका ड्रॉप के माध्यम से mvn
कमांड लाइन। कैसे?Maven मोजो एसक्यूएल का उपयोग कर त्रुटियाँ: निष्पादित
मैं एक लंबे समय जावा डेवलपर हूँ, लेकिन मैं एक ant
आधारित दुनिया में रह रहे हैं अधिकांश भाग के लिए। मुझे ant
की शक्ति और स्पष्टीकरण पसंद है, और यह नियंत्रण कि यह मुझे सबकुछ पर देता है। हालांकि, मेरे नए काम में, maven
का उपयोग करने के लिए एक धक्का है। तो मैंने घर पर काम कर रहे एक परियोजना का उपयोग करके इसे सीखने का फैसला किया है।
एक अलग व्यक्तिगत परियोजना पर स्थापित की गई चीज़ों में से एक है कमांड लाइन पर ant
से मेरे पोस्टग्रेस डेटाबेस को पूरी तरह से स्थापित करने और फाड़ने की क्षमता है। मैं अलग-अलग या संगीत कार्यक्रम में कृपया किसी भी तालिका, अनुक्रम, और एकीकरण परीक्षण डेटा को टुकड़ा और पासा कर सकता हूं। निश्चित रूप से, इसका मतलब है कि मेरे पास gajillion ant
लक्ष्य हैं, लेकिन यह बहुत अच्छी तरह से काम करता है। मुझे यह पसंद है; इसने वर्षों से मुझे काफी अच्छी तरह से सेवा दी है।
कैसे सप्ताहांत में Maven में यह पूरा करने के शोध में, मैं Mojo SQL Maven Plugin पाया। usage page को देख (और मुझे लगता है कि अवधि शिथिल उपयोग करते हैं, यह वास्तव में कोई स्पष्टीकरण के साथ सिर्फ एक अर्द्ध उदाहरण है के रूप में) और example page के बाद, मैं अपने pom.xml
फ़ाइल के लिए कुछ परिवर्तन के साथ आने में सक्षम था। मैंने उदाहरण (postgressql) में कुछ स्पष्ट टाइपो तय किए हैं, और यह सुनिश्चित करने के लिए PostgreSQL JDBC page का संदर्भ दिया है कि मेरे पास जेडीबीसी कनेक्शन स्ट्रिंग सही है।
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.myapp</groupId>
<artifactId>myapp</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>myapp</name>
<url>http://maven.apache.org</url>
<repositories>
<repository>
<id>JBoss</id>
<url>https://repository.jboss.org/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.0.0.CR7</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sql-maven-plugin</artifactId>
<version>1.5</version>
<dependencies>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>8.3-606.jdbc4</version>
</dependency>
</dependencies>
<configuration>
<driver>org.postgresql.Driver</driver>
<url>jdbc:postgresql://localhost:5432/myapp</url>
<settingsKey>myapp</settingsKey>
<!--all executions are ignored if -Dmaven.test.skip=true-->
<skip>${maven.test.skip}</skip>
</configuration>
<executions>
<execution>
<id>drop-db-before-test-if-any</id>
<phase>process-test-resources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<!-- need another database to drop the targeted one -->
<url>jdbc:postgresql://localhost:5432/template1</url>
<autocommit>true</autocommit>
<sqlCommand>drop database myapp</sqlCommand>
<!-- ignore error when database is not avaiable -->
<onError>continue</onError>
</configuration>
</execution>
<execution>
<id>create-db</id>
<phase>process-test-resources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<url>jdbc:postgresql://localhost:5432/template1</url>
<!-- no transaction -->
<autocommit>true</autocommit>
<sqlCommand>create database myapp</sqlCommand>
</configuration>
</execution>
<execution>
<id>create-schema</id>
<phase>process-test-resources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<autocommit>true</autocommit>
<srcFiles>
<srcFile>src/main/sql/create_person.sql</srcFile>
</srcFiles>
</configuration>
</execution>
<execution>
<id>create-data</id>
<phase>process-test-resources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<orderFile>ascending</orderFile>
<fileset>
<basedir>${basedir}</basedir>
<includes>
<include>src/test/sql/person_data.sql</include>
</includes>
</fileset>
</configuration>
</execution>
<!-- drop db after test -->
<execution>
<id>drop-db-after-test</id>
<phase>test</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<url>jdbc:postgresql://localhost:5432/template1</url>
<autocommit>true</autocommit>
<sqlCommand>drop database myapp</sqlCommand>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
अब, के बाद से मैं डेटाबेस नहीं बनाया है, यह पीजी कमांड लाइन पर एक \l
में दिखाई नहीं करता है: मैं pom.xml
(दोषी को बचाने के लिए संशोधित) नीचे के सभी चस्पा करेंगे :
[[email protected] myapp]$ psql template1
Welcome to psql 8.3.5, the PostgreSQL interactive terminal.
Type: \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit
template1=# \l
List of databases
Name | Owner | Encoding
-----------+----------+----------
postgres | postgres | UTF8
template0 | postgres | UTF8
template1 | postgres | UTF8
(3 rows)
इस प्रकार, जब मैं mvn sql:execute
चलाने के लिए, मैं अपने डेटाबेस बनाया पाने के लिए ... या कम से कम drop-db-before-test-if-any
कार्य पर विफल नहीं के बाद से उस त्रुटि पर जारी रखने के लिए सेट कर दिया जाता उम्मीद है। लेकिन बेशक:
[[email protected] myapp]$ mvn sql:execute
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building myapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- sql-maven-plugin:1.5:execute (default-cli) @ myapp ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.667s
[INFO] Finished at: Mon Dec 05 20:22:17 CST 2011
[INFO] Final Memory: 3M/81M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:sql-maven-plugin:1.5:execute (default-cli) on project myapp: FATAL: database "myapp" does not exist -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
अंतिम पंक्ति पर उल्लिखित त्रुटि पृष्ठ सहायक नहीं है; यह सिर्फ मुझे बताता है कि एक प्लगइन ने त्रुटि उत्पन्न की, मैवेन स्वयं ही नहीं।
तो चलिए इसे -X
स्विच के साथ चलाएं। मैं सिर्फ त्रुटि के दिलचस्प हिस्सा पोस्ट करेंगे:
[ERROR] Failed to execute goal org.codehaus.mojo:sql-maven-plugin:1.5:execute (default-cli) on project myapp: FATAL: database "myapp" does not exist -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.codehaus.mojo:sql-maven-plugin:1.5:execute (default-cli) on project myapp: FATAL: database "myapp" does not exist
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:217)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:319)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
Caused by: org.apache.maven.plugin.MojoExecutionException: FATAL: database "myapp" does not exist
at org.codehaus.mojo.sql.SqlExecMojo.execute(SqlExecMojo.java:618)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
... 19 more
Caused by: org.postgresql.util.PSQLException: FATAL: database "myapp" does not exist
at org.postgresql.core.v3.ConnectionFactoryImpl.readStartupMessages(ConnectionFactoryImpl.java:444)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:99)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:66)
at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:124)
at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:30)
at org.postgresql.jdbc4.AbstractJdbc4Connection.<init>(AbstractJdbc4Connection.java:29)
at org.postgresql.jdbc4.Jdbc4Connection.<init>(Jdbc4Connection.java:24)
at org.postgresql.Driver.makeConnection(Driver.java:386)
at org.postgresql.Driver.connect(Driver.java:260)
at org.codehaus.mojo.sql.SqlExecMojo.getConnection(SqlExecMojo.java:899)
at org.codehaus.mojo.sql.SqlExecMojo.execute(SqlExecMojo.java:612)
... 21 more
लेकिन, लेकिन, लेकिन ... <onError>continue</onError>
!
तो, सवाल:
- क्या मैं गलत कर रहा हूँ? क्या यह मेरी उम्मीद है, या मेरा कोड?
- आप देखेंगे मैं एक
create-person.sql
फ़ाइल है। मुझे उदाहरणों से पता है कि मेरे पास कई फाइलें हो सकती हैं, जैसे किcreate-address.sql
। लेकिनant
में, मैं इतने लंबे समय के रूप में मैं चींटी को ध्यान में रेफेरेंन्शिअल सत्यनिष्ठा के आदेश रखने कार्य करनेperson
मेज से अलगaddress
तालिका बनाने के लिए, की क्षमता है।maven
के साथ ऐसा कुछ संभव है? यदि हां, तो कैसे?
शब्दावली के लिए खेद है, और किसी भी सहायता के लिए अग्रिम धन्यवाद।
मुझे पता है यह एक पुराने धागा है, लेकिन आप मुझे उपयोगी लिखना शुरू किया इस प्लगइन मिल सकती है: https://github.com/adrianboimvaser/postgresql-maven-plugin। यह अभी भी शुरुआती चरण में है और दस्तावेज की कमी है, लेकिन ज्यादातर काम करता है। मैंने पहले ही मैवेन सेंट्रल में संस्करण 0.1 जारी किया है। चीयर्स! – adrianboimvaser