4

I would like to define database url, username, password in one place. Currently I have

application.properties with

spring.datasource.url=....
spring.datasource.username=sa
spring.datasource.password=00

And pom.xml with

  <plugin>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-maven-plugin</artifactId>
    <version>4.1.2</version>
    <configuration>
      <url>....</url>
      <user>sa</user>
      <password>00</password>
    </configuration>
  </plugin>

So probably I need to reuse property values defined in application.properties.

This <password>${spring.datasource.password}</password> doesn't work. Also I tried

 <systemProperties>
    <systemProperty>
      <name>url</name>
      <value>....</value>
    </systemProperty>
    ...
 </systemProperties>

Neither approach is working.

ieXcept
  • 1,088
  • 18
  • 37
  • Possible duplicate of [How to read an external properties file in Maven](http://stackoverflow.com/questions/849389/how-to-read-an-external-properties-file-in-maven) – rmlan Apr 02 '17 at 14:47

2 Answers2

1

You can do the reverse by building a properties file from your pom file. In your properties file you'd use something like:

password=${pom.password}

And your pom file would have something like:

<password>your_db_password</password>

Then at:

mvn clean package

Maven will build your properties file.

Here's a simple tutorial: Add Maven Build Information ...

Ollie in PGH
  • 2,559
  • 2
  • 16
  • 19
0

As the top two answers from this question (as suggested in rmlan's comment) suggest, use the properties-maven-plugin like this:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>properties-maven-plugin</artifactId>
    <version>1.0.0</version>
    <executions>
        <execution>
            <phase>initialize</phase>
            <goals>
                <goal>read-project-properties</goal>
            </goals>
            <configuration>
                <files>
                    <file>${basedir}/src/main/resources/application.properties</file>
                </files>
            </configuration>
        </execution>
    </executions>
</plugin>

Then with your application.properties do the following:

<plugin>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-maven-plugin</artifactId>
    <version>4.1.2</version>
    <configuration>
        <url>${spring.datasource.url}</url>
        <user>${spring.datasource.username}</user>
        <password>${spring.datasource.password}</password>
    </configuration>
</plugin>
Kikkomann
  • 386
  • 5
  • 20