Alexandr
Alexandr

Reputation: 9515

Using property file in maven

I don't quite understand how it can be used. There is a property defined in the file. I try to use maven property plugin to read it and save. The property is used in the liquibase plugin:

<plugin>
<groupId>org.codehaus.mojo</groupId>
  <artifactId>properties-maven-plugin</artifactId>
  <version>1.0-alpha-1</version>
  <executions>
    <execution>
      <phase>initialize</phase>
      <goals>
        <goal>read-project-properties</goal>
      </goals>
      <configuration>
       <files>
          <file>src/main/resources/properties/app.properties</file>
        </files>
      </configuration>
    </execution>
  </executions>
</plugin>
<plugin>
    <groupId>org.liquibase</groupId>
    <artifactId>liquibase-maven-plugin</artifactId>
    <version>2.0.5</version>
    <configuration>
        <propertyFile>src/main/resources/db/config/${env}-data-access.properties</propertyFile>
        <changeLogFile>src/main/resources/db/changelog/db.changelog-master.xml</changeLogFile>
        <migrationSqlOutputFile>src/main/resources/db/gen/migrate.sql</migrationSqlOutputFile>
        <!--<logging>debug</logging>-->
        <logging>info</logging>
        <promptOnNonLocalDatabase>false</promptOnNonLocalDatabase>
        <!--<verbose>false</verbose>-->
        <dropFirst>true</dropFirst>
    </configuration>
</plugin>
  1. According to the documentation in order to read property and save it I have to run: mvn properties:read-project-properties. But I'm getting the following error in this case:

    [ERROR] Failed to execute goal org.codehaus.mojo:properties-maven-plugin:1.0-alpha-2:read-project-properties (default-cli) on project SpringWebFlow: The parameters 'files' for goal org.codehaus.mojo:properties-maven-plugin:1.0-alpha-2:read-project-properties are missing or invalid -> [Help 1]

I've changed pom.xml, removed the <execution> section and moved the <configuration> section:

<groupId>org.codehaus.mojo</groupId>
  <artifactId>properties-maven-plugin</artifactId>
  <version>1.0-alpha-1</version>
  <configuration>
    <files>
        <file>src/main/resources/properties/app.properties</file>
    </files>
  </configuration>

Ok. now, when I run mvn properties:read-project-properties the error disappeared. But where in this case the property is saved? Cause when I start the following maven goal:

mvn liquibase:update

I can see that the ${env} property is not defined. Liquibase tries to use the src/main/resources/db/config/${env}-data-access.properties file.

What am I doing wrong? How to read a property from the file, so it could be accessible from different maven plugins?

Upvotes: 1

Views: 5914

Answers (1)

mszalbach
mszalbach

Reputation: 11490

The problem is that "mvn liquibase:update" is a special plugin goal and is not part of the maven life cycle. So it never passes the initialize phase and so the property plugin is not executed.

The following will work

mvn initialize liquibase:update

One solution would be to call liquibase:update in one of the maven lifecylce phases like compile, package ..., but then it would be executed on every build.

Or you use the maven-exec plugin to call "initialize liquibase:update" from maven. Or you create a profile were you bind the liquibase:update to the lifecylce phase initialize and the udate is executed when you call

mvn initialize -Pliquibase

I do not know a better solution to this problem and I could not find a suitable solution for this.

For reference: Maven lifecycle

Upvotes: 1

Related Questions