Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using @project.version@ with Liquibase in a multimodule Maven project

It was a little bit hard to come up with a meaningful title, hope it will become clear enough after the explanation. I have searched through a number of Qs and As on SO, and they were all very close to the problem I am experiencing, but still not close enough.

In general, what I want to accomplish is to store project version in DB by accessing the maven property @project.version@ from a .csv file which is loaded by a Liquibase script. My maven project structure looks like this:

parentModule
pom.xml
|
---moduleA
   |__pom.xml
---moduleB
   |__pom.xml
---moduleC
   |__pom.xml
...

Pom.xml are defined as:

**PARENT POM**
<project ...>

  <groupId>com.parent</groupId>
  <artifactId>parent</artifactId>
  <version>1.0</version>
  <packaging>pom</packaging>
  <name>parent</name>

    <parent>
        <artifactId>spring-boot-starter-parent</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.2.1.RELEASE</version>
        <relativePath />
    </parent>

    <properties>
         <java.version>8</java.version>
    </properties>


    <modules>
        <module>moduleA</module>
        <module>moduleB</module>
        <module>moduleC</module>
        ...
  </modules>

  <build>
        <defaultGoal>package</defaultGoal>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
----------------------------------------------------------------------

**CHILD POM**
<project ...>
    <artifactId>moduleC</artifactId>
    <name>moduleC</name>

    <parent>
        <groupId>com.parent</groupId>
        <artifactId>parent</artifactId>
        <version>1.0</version>
    </parent>

<dependencies>
    <dependency>
      ...
    </dependency>
</dependencies>
 <build>
   <resources>
        <resource>
            <directory>moduleC/src/main/resources/db/changelog/</directory>
            <filtering>true</filtering>
            <includes>
                <include>**/app_version.csv/</include>
            </includes>
        </resource>
    </resources>
 </build>
</project>

Liquibase scripts are defined in moduleC/src/main/resources/db/changelog/changelog-master.xml etc., while the .csv files with initial values are located in moduleC/src/main/resources/db/users.csv etc. In one of those csv files, I want to push @project.version@ value, like this:

id;app_key;app_value;created_by;last_modified_by
1;app-version;@project.version@;system;system

Since that file is located in moduleC, I used maven resource filtering even inparentModule <build/> to filter that file so it can resolve @project.version@ property, but with no luck:

<build>
    <resources>
        <resource>
            <directory>moduleC/src/main/resources/db/changelog/</directory>
            <filtering>true</filtering>
            <includes>
                <include>**/app_version.csv/</include>
            </includes>
        </resource>
    </resources>
    <defaultGoal>package</defaultGoal>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <skip>true</skip>
            </configuration>
        </plugin>
    </plugins>
</build>

There are errors, one that says that master changelog cannot be found, while in other cases just string value @project.version@ is stored. Seems to me I should include app_version.csv and its location (moduleC) as resource inside <build> tag withing parentModule pom.xml, but every combination of referencing it fails. Is there a solution to reference it properly (either from parentModule or moduleC pom.xml) or there might be an easier way to store @project.version@ with liquibase?

like image 791
borgmater Avatar asked Jan 10 '20 12:01

borgmater


3 Answers

I am extremely sorry for not replying on time, was temporarily removed from the project after posting the question and was not able to access the git repository due to change of location. So far, I have tried all of the proposed actions, but with no result. In the end, what I found to work was an accepted answer posted here. I have added the build block inside mavenC module pom.xml and it worked. Nevertheless, thank you all immensely for posting and helping.

like image 159
borgmater Avatar answered Sep 30 '22 08:09

borgmater


I think you need to use the maven-replacer-plugin in your build cycle. It will be configured to process the 'app_version.csv' file and output the substituted file content to the 'target/classes' folder. The subsequent packaging phase will ensure the csv file with the current pom version will be bundled into the artifact that the liquidbase tool then handles.

like image 20
emeraldjava Avatar answered Sep 30 '22 08:09

emeraldjava


Looks like you're using the wrong syntax for filtering in the CSV-file. Instead of using @project.version@, try ${project.version} instead:

Check the <directory>moduleC/src/main/resources/...</directory>, if the resources plugin lives in the child POM of moduleC then there is no need for the prefix.

Try replacing with <directory>src/main/resources/..</directory>

like image 31
blagerweij Avatar answered Sep 30 '22 08:09

blagerweij