JHipster 和数据库连接
JHipster and database connection
我正在使用 JHipster,当我 运行 sudo mvn liquibase:diff
我得到以下错误
[INFO] Settings
----------------------------
[INFO] driver: org.postgresql.Driver
[INFO] url: jdbc:postgresql://localhost/gastos8
[INFO] username: gastos8
[INFO] password: *****
[INFO] use empty password: false
[INFO] properties file: null
[INFO] properties file will override? false
[INFO] prompt on non-local database? true
[INFO] clear checksums? false
[INFO] changeLogFile: src/main/resources/config/liquibase/master.xml
[INFO] context(s): null
[INFO] label(s): null
[INFO] referenceDriver: null
[INFO] referenceUrl: hibernate:spring:com.cboujon.domain?dialect=org.hibernate.dialect.PostgreSQL82Dialect
[INFO] referenceUsername: null
[INFO] referencePassword: null
[INFO] referenceDefaultSchema: null
[INFO] diffChangeLogFile: src/main/resources/config/liquibase/changelog/20150807132702_changelog.xml
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 9.554s
[INFO] Finished at: Fri Aug 07 13:27:12 ART 2015
[INFO] Final Memory: 18M/179M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.liquibase:liquibase-maven-plugin:3.3.2:diff (default-cli) on project gastos8: Error setting up or running Liquibase: liquibase.exception.DatabaseException: org.postgresql.util.PSQLException: FATAL: password authentication failed for user "gastos8" -> [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
我不知道为什么 [INFO] username: gastos8
。
这是我的配置文件:
申请-dev.yml
spring:
profiles:
active: dev
datasource:
dataSourceClassName: org.postgresql.ds.PGSimpleDataSource
url:
databaseName: gastos8
serverName: localhost
username: postgres
password: ---
jpa:
database-platform: org.hibernate.dialect.PostgreSQL9Dialect
database: POSTGRESQL
openInView: false
show_sql: true
generate-ddl: false
hibernate:
ddl-auto: none
naming-strategy: org.hibernate.cfg.EJB3NamingStrategy
properties:
hibernate.cache.use_second_level_cache: true
hibernate.cache.use_query_cache: false
hibernate.generate_statistics: true
hibernate.cache.region.factory_class: org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
我做错了什么?
Liquibase maven 插件不会读取 application.yml 以了解如何连接到您的数据库,它在您的 pom.xml 中有自己的配置。所以你必须把它放在那里。
为什么要用 sudo 执行 maven?
现在,您可能拥有 root 拥有的项目文件,这通常是个坏主意。
在文件 src/main/resources/config/application-dev.yml
中
datasource:
driver-class-name: com.mysql.jdbc.jdbc2.optional.MysqlDataSource
url: jdbc:mysql://localhost:3306/DBNAME?useUnicode=true characterEncoding=utf8
name: DBNAME
username: USERNAME
password: PWD
你必须改变:
DBNAME
USERNAME
PWD
不要忘记生产变更
src/main/resources/config/application-prod
and localhost
in url: jdbc:mysql://localhost
:3306 到你的 url
datasource:
driver-class-name: com.mysql.jdbc.jdbc2.optional.MysqlDataSource
url: jdbc:mysql://database hosting address:3306/database Name?useUnicode=true characterEncoding=utf8
name: database Name
username: database user name
password: and put here database user password
如已接受的答案所述,maven 不读取 application.yml。
但是如果您对在 maven liquibase 插件定义中进行数据库配置感到不舒服,您可以使用 maven 属性和 liquibase 配置文件。
1 - 在您的 liquibase-maven-plugin
配置中,添加一个 propertyFile
:
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>${liquibase.version}</version>
<configuration>
<propertyFileWillOverride>true</propertyFileWillOverride>
<propertyFile>src/main/resources/liquibase.properties</propertyFile>
</configuration>
(...)
</plugin>
2 - 在 src/main/resources 下的任意位置创建 liquibase 属性文件。例如,src/main/resources/config/liquibase :
contexts:${liquibase.contexts}
changeLogFile:src/main/resources/config/liquibase/master.xml
driver:${dataSource.project.driverClass}
url:${dataSource.project.jdbcURL}
username:${dataSource.project.user}
password:${dataSource.project.password}
verbose:true
dropFirst:false
3 - 告诉 maven 过滤您的 liquibase 属性文件以通过 maven 属性替换值。在 <build>
中,要求 maven 过滤您的 liquibase 配置文件:
<build>
<resources>
<resource>
<directory>src/main/resources/config/liquibase</directory>
<filtering>true</filtering>
</resource>
</resources>
(...)
</build>
4 - 为允许启动 liquibase 目标的每个配置文件添加 Maven 属性:
<profile>
<id>dev</id>
(...)
<properties>
<liquibase.contexts></liquibase.contexts>
<dataSource.project.driverClass>org.mariadb.jdbc.Driver</dataSource.project.driverClass>
<dataSource.project.jdbcURL>jdbc:mariadb://localhost:3306/yourDatabaseName</dataSource.project.jdbcURL>
<dataSource.project.user>${datasource.username}</dataSource.project.user>
<dataSource.project.password>${datasource.password}</dataSource.project.password>
</properties>
</profile>
5 - 使用 maven 启动 liquibase 目标并在命令行中设置凭据:
mvn resources:resources liquibase:update -Pdev -Ddatasource.username=root -Ddatasource.password=root
我正在使用 JHipster,当我 运行 sudo mvn liquibase:diff
我得到以下错误
[INFO] Settings
----------------------------
[INFO] driver: org.postgresql.Driver
[INFO] url: jdbc:postgresql://localhost/gastos8
[INFO] username: gastos8
[INFO] password: *****
[INFO] use empty password: false
[INFO] properties file: null
[INFO] properties file will override? false
[INFO] prompt on non-local database? true
[INFO] clear checksums? false
[INFO] changeLogFile: src/main/resources/config/liquibase/master.xml
[INFO] context(s): null
[INFO] label(s): null
[INFO] referenceDriver: null
[INFO] referenceUrl: hibernate:spring:com.cboujon.domain?dialect=org.hibernate.dialect.PostgreSQL82Dialect
[INFO] referenceUsername: null
[INFO] referencePassword: null
[INFO] referenceDefaultSchema: null
[INFO] diffChangeLogFile: src/main/resources/config/liquibase/changelog/20150807132702_changelog.xml
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 9.554s
[INFO] Finished at: Fri Aug 07 13:27:12 ART 2015
[INFO] Final Memory: 18M/179M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.liquibase:liquibase-maven-plugin:3.3.2:diff (default-cli) on project gastos8: Error setting up or running Liquibase: liquibase.exception.DatabaseException: org.postgresql.util.PSQLException: FATAL: password authentication failed for user "gastos8" -> [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
我不知道为什么 [INFO] username: gastos8
。
这是我的配置文件:
申请-dev.yml
spring:
profiles:
active: dev
datasource:
dataSourceClassName: org.postgresql.ds.PGSimpleDataSource
url:
databaseName: gastos8
serverName: localhost
username: postgres
password: ---
jpa:
database-platform: org.hibernate.dialect.PostgreSQL9Dialect
database: POSTGRESQL
openInView: false
show_sql: true
generate-ddl: false
hibernate:
ddl-auto: none
naming-strategy: org.hibernate.cfg.EJB3NamingStrategy
properties:
hibernate.cache.use_second_level_cache: true
hibernate.cache.use_query_cache: false
hibernate.generate_statistics: true
hibernate.cache.region.factory_class: org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
我做错了什么?
Liquibase maven 插件不会读取 application.yml 以了解如何连接到您的数据库,它在您的 pom.xml 中有自己的配置。所以你必须把它放在那里。
为什么要用 sudo 执行 maven? 现在,您可能拥有 root 拥有的项目文件,这通常是个坏主意。
在文件 src/main/resources/config/application-dev.yml
中datasource:
driver-class-name: com.mysql.jdbc.jdbc2.optional.MysqlDataSource
url: jdbc:mysql://localhost:3306/DBNAME?useUnicode=true characterEncoding=utf8
name: DBNAME
username: USERNAME
password: PWD
你必须改变:
DBNAME
USERNAME
PWD
不要忘记生产变更
src/main/resources/config/application-prod
and localhost
in url: jdbc:mysql://localhost
:3306 到你的 url
datasource:
driver-class-name: com.mysql.jdbc.jdbc2.optional.MysqlDataSource
url: jdbc:mysql://database hosting address:3306/database Name?useUnicode=true characterEncoding=utf8
name: database Name
username: database user name
password: and put here database user password
如已接受的答案所述,maven 不读取 application.yml。
但是如果您对在 maven liquibase 插件定义中进行数据库配置感到不舒服,您可以使用 maven 属性和 liquibase 配置文件。
1 - 在您的 liquibase-maven-plugin
配置中,添加一个 propertyFile
:
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>${liquibase.version}</version>
<configuration>
<propertyFileWillOverride>true</propertyFileWillOverride>
<propertyFile>src/main/resources/liquibase.properties</propertyFile>
</configuration>
(...)
</plugin>
2 - 在 src/main/resources 下的任意位置创建 liquibase 属性文件。例如,src/main/resources/config/liquibase :
contexts:${liquibase.contexts}
changeLogFile:src/main/resources/config/liquibase/master.xml
driver:${dataSource.project.driverClass}
url:${dataSource.project.jdbcURL}
username:${dataSource.project.user}
password:${dataSource.project.password}
verbose:true
dropFirst:false
3 - 告诉 maven 过滤您的 liquibase 属性文件以通过 maven 属性替换值。在 <build>
中,要求 maven 过滤您的 liquibase 配置文件:
<build>
<resources>
<resource>
<directory>src/main/resources/config/liquibase</directory>
<filtering>true</filtering>
</resource>
</resources>
(...)
</build>
4 - 为允许启动 liquibase 目标的每个配置文件添加 Maven 属性:
<profile>
<id>dev</id>
(...)
<properties>
<liquibase.contexts></liquibase.contexts>
<dataSource.project.driverClass>org.mariadb.jdbc.Driver</dataSource.project.driverClass>
<dataSource.project.jdbcURL>jdbc:mariadb://localhost:3306/yourDatabaseName</dataSource.project.jdbcURL>
<dataSource.project.user>${datasource.username}</dataSource.project.user>
<dataSource.project.password>${datasource.password}</dataSource.project.password>
</properties>
</profile>
5 - 使用 maven 启动 liquibase 目标并在命令行中设置凭据:
mvn resources:resources liquibase:update -Pdev -Ddatasource.username=root -Ddatasource.password=root