将外部文件中的值加载到 pom.xml
Load values from external file into pom.xml
我正在尝试从外部文件中读取 pom.xml 中的一些参数。我正在使用 properties-maven-plugin,但我真的不介意任何其他解决方案从外部文件读取值作为 pom 中的变量。
这是我的插件:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<goals>
<goal>read-project-properties</goal>
</goals>
<phase>initialize</phase>
<configuration>
<!--<files>
<file>${apps.basedir}/apps/flywayvariables.properties</file>
<file>/home/gokul/git/sampleproject/apps/flywayvariables.properties</file>
</files>-->
<urls>
<url>file:///${apps.basedir}/apps/flywayvariables.properties</url>
</urls>
</configuration>
</execution>
</executions>
</plugin>
这是我尝试使用它的地方:
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>7.0.1</version>
<configuration>
<sqlMigrationSeparator>__</sqlMigrationSeparator>
<locations>
<location>filesystem:${apps.basedir}/apps/flyway</location>
</locations>
<url>jdbc:postgresql://localhost:9000/postgres</url>
<user>${dbuser}</user>
<flyway.user>${dbuser}</flyway.user>
<flyway.password>${dbpassword}</flyway.password>
<password>${dbpassword}</password>
</configuration>
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.16</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>migrate</id>
<goals>
<goal>migrate</goal>
</goals>
<phase>initialize</phase>
</execution>
</executions>
</plugin>
这是我的属性文件:
<properties>
<dbuser>postgres</dbuser>
<dbpassword>test1234</dbpassword>
</properties>
当我运行 mvn -X 初始化时,出现以下错误:
[DEBUG] Configuring mojo org.codehaus.mojo:properties-maven-plugin:1.0.0:read-project-properties from plugin realm ClassRealm[plugin>org.codehaus.mojo:properties-maven-plugin:1.0.0, parent: sun.misc.Launcher$AppClassLoader@5c647e05]
[DEBUG] Configuring mojo 'org.codehaus.mojo:properties-maven-plugin:1.0.0:read-project-properties' with basic configurator -->
[DEBUG] (f) project = MavenProject: com.example.company:sampleapplication @ /home/gokul/git/sampleproject/apps/pom.xml
[DEBUG] (f) quiet = false
[DEBUG] (s) urls = [file:////home/gokul/git/sampleproject/apps/flywayvariables.properties]
[DEBUG] -- end configuration --
[DEBUG] Loading properties from URL file:////home/gokul/git/sampleproject/apps/flywayvariables.properties
[INFO]
[INFO] --- flyway-maven-plugin:7.0.1:migrate (migrate) @ apps ---
...
...
...
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.422 s (Wall Clock)
[INFO] Finished at: 2020-10-13T01:18:40+02:00
[INFO] Final Memory: 33M/1237M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.flywaydb:flyway-maven-plugin:7.0.1:migrate (migrate) on project apps: org.flywaydb.core.internal.exception.FlywaySqlException:
[ERROR] Unable to obtain connection from database (jdbc:postgresql://localhost:9000/postgres) for user 'null': The server requested password-based authentication, but no password was provided.
[ERROR] ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[ERROR] SQL State : 08004
[ERROR] Error Code : 0
[ERROR] Message : The server requested password-based authentication, but no password was provided.
我可以看到插件按照我想要的顺序执行,但 属性 没有加载。文件路径和权限应该不是问题,因为我在执行 Maven 项目时使用相同的用户帐户创建了属性文件。 未提供密码错误更改为身份验证失败错误当我手动输入密码并且只保留$dbuser 作为一个变量。我也试过更改变量的名称,但没有成功。在 properties-maven-plugin 配置中,我尝试提供 files 而不是 urls,但它对 maven 没有任何影响。
不幸的是 问题中的 none 解决方案对我有帮助。
尝试了以下 Maven 目标:
- 初始化
- 验证
- 验证
- 安装
- 属性:读取项目属性初始化
我很确定您需要以传统方式编写属性文件,例如
dbuser=postgres
dbpassword=test1234
我不知道为什么属性插件不起作用,但 Flyway 本身支持多种方法将这些设置存储在 pom 之外。有关详细信息,请参阅 Flyway maven documentation。
以下是 link 的一些片段:
配置文件
Flyway 将搜索并自动加载 <user-home>/flyway.conf
配置文件(如果存在)。
也可以将 Flyway 指向一个或多个额外的配置文件。这是通过如下提供系统 属性 flyway.configFiles
来实现的:
mvn -Dflyway.configFiles=path/to/myAlternativeConfig.conf flyway:migrate
有关详细信息,请参阅 https://flywaydb.org/documentation/maven/#config-files。
Maven 设置
也可以使用 Maven settings.xml
文件存储数据库用户和密码:
<settings>
<servers>
<server>
<!-- By default Flyway will look for the server with the id 'flyway-db' -->
<!-- This can be customized by configuring the 'serverId' property -->
<id>flyway-db</id>
<username>myUser</username>
<password>mySecretPwd</password>
</server>
</servers>
</settings>
我正在尝试从外部文件中读取 pom.xml 中的一些参数。我正在使用 properties-maven-plugin,但我真的不介意任何其他解决方案从外部文件读取值作为 pom 中的变量。
这是我的插件:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<goals>
<goal>read-project-properties</goal>
</goals>
<phase>initialize</phase>
<configuration>
<!--<files>
<file>${apps.basedir}/apps/flywayvariables.properties</file>
<file>/home/gokul/git/sampleproject/apps/flywayvariables.properties</file>
</files>-->
<urls>
<url>file:///${apps.basedir}/apps/flywayvariables.properties</url>
</urls>
</configuration>
</execution>
</executions>
</plugin>
这是我尝试使用它的地方:
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>7.0.1</version>
<configuration>
<sqlMigrationSeparator>__</sqlMigrationSeparator>
<locations>
<location>filesystem:${apps.basedir}/apps/flyway</location>
</locations>
<url>jdbc:postgresql://localhost:9000/postgres</url>
<user>${dbuser}</user>
<flyway.user>${dbuser}</flyway.user>
<flyway.password>${dbpassword}</flyway.password>
<password>${dbpassword}</password>
</configuration>
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.16</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>migrate</id>
<goals>
<goal>migrate</goal>
</goals>
<phase>initialize</phase>
</execution>
</executions>
</plugin>
这是我的属性文件:
<properties>
<dbuser>postgres</dbuser>
<dbpassword>test1234</dbpassword>
</properties>
当我运行 mvn -X 初始化时,出现以下错误:
[DEBUG] Configuring mojo org.codehaus.mojo:properties-maven-plugin:1.0.0:read-project-properties from plugin realm ClassRealm[plugin>org.codehaus.mojo:properties-maven-plugin:1.0.0, parent: sun.misc.Launcher$AppClassLoader@5c647e05]
[DEBUG] Configuring mojo 'org.codehaus.mojo:properties-maven-plugin:1.0.0:read-project-properties' with basic configurator -->
[DEBUG] (f) project = MavenProject: com.example.company:sampleapplication @ /home/gokul/git/sampleproject/apps/pom.xml
[DEBUG] (f) quiet = false
[DEBUG] (s) urls = [file:////home/gokul/git/sampleproject/apps/flywayvariables.properties]
[DEBUG] -- end configuration --
[DEBUG] Loading properties from URL file:////home/gokul/git/sampleproject/apps/flywayvariables.properties
[INFO]
[INFO] --- flyway-maven-plugin:7.0.1:migrate (migrate) @ apps ---
... ... ...
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.422 s (Wall Clock)
[INFO] Finished at: 2020-10-13T01:18:40+02:00
[INFO] Final Memory: 33M/1237M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.flywaydb:flyway-maven-plugin:7.0.1:migrate (migrate) on project apps: org.flywaydb.core.internal.exception.FlywaySqlException:
[ERROR] Unable to obtain connection from database (jdbc:postgresql://localhost:9000/postgres) for user 'null': The server requested password-based authentication, but no password was provided.
[ERROR] ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[ERROR] SQL State : 08004
[ERROR] Error Code : 0
[ERROR] Message : The server requested password-based authentication, but no password was provided.
我可以看到插件按照我想要的顺序执行,但 属性 没有加载。文件路径和权限应该不是问题,因为我在执行 Maven 项目时使用相同的用户帐户创建了属性文件。 未提供密码错误更改为身份验证失败错误当我手动输入密码并且只保留$dbuser 作为一个变量。我也试过更改变量的名称,但没有成功。在 properties-maven-plugin 配置中,我尝试提供 files 而不是 urls,但它对 maven 没有任何影响。
不幸的是
尝试了以下 Maven 目标:
- 初始化
- 验证
- 验证
- 安装
- 属性:读取项目属性初始化
我很确定您需要以传统方式编写属性文件,例如
dbuser=postgres
dbpassword=test1234
我不知道为什么属性插件不起作用,但 Flyway 本身支持多种方法将这些设置存储在 pom 之外。有关详细信息,请参阅 Flyway maven documentation。
以下是 link 的一些片段:
配置文件
Flyway 将搜索并自动加载 <user-home>/flyway.conf
配置文件(如果存在)。
也可以将 Flyway 指向一个或多个额外的配置文件。这是通过如下提供系统 属性 flyway.configFiles
来实现的:
mvn -Dflyway.configFiles=path/to/myAlternativeConfig.conf flyway:migrate
有关详细信息,请参阅 https://flywaydb.org/documentation/maven/#config-files。
Maven 设置
也可以使用 Maven settings.xml
文件存储数据库用户和密码:
<settings>
<servers>
<server>
<!-- By default Flyway will look for the server with the id 'flyway-db' -->
<!-- This can be customized by configuring the 'serverId' property -->
<id>flyway-db</id>
<username>myUser</username>
<password>mySecretPwd</password>
</server>
</servers>
</settings>