将外部文件中的值加载到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 initialize时,出现以下错误

[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配置中,我尝试提供文件而不是 urls ,但是这对maven没有任何影响。

不幸的是,this问题中的解决方案都没有帮助我。

尝试了以下Maven目标:

  • 初始化
  • 验证
  • 验证
  • 安装
  • properties:读取项目属性初始化

解决方法

我很确定您需要以传统方式编写属性文件,例如

dbuser=postgres
dbpassword=test1234
,

我不知道为什么属性插件不起作用,但是Flyway本身支持多种将这些设置存储在pom之外的方法。有关更多详细信息,请参见Flyway maven documentation

下面是链接中的一些片段:

配置文件

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>