I'm using Spring with maven with an Elasticsearch repository. I would like to setup a synonyms dictionary.

So I have a settings.json in src/main/resources which declare my synonyms.

{
  "synonym" : {
    "type" : "synonym",
    "synonyms" : ["finally,all", "slap,done", ...]
  }
) 

This is working fine. But my dictionary is going to enlarge very fast. So I would like to externalize the json array to another file and use Maven filtering to replace with content file. For example :

{
  "synonym" : {
    "type" : "synonym",
    "synonyms" : @[email protected]
  }
) 

I can't figure it out a proper solution.

Maven doesn't seems to work with json properties file for filtering. I tried to use a yaml properties file but it doesn't work with multi line unless I explicit use \to break line but it's not convenient for maintainability.

  • So does anyone already encountered this issue ?
  • Is there a way to import file content in another file ?
  • Can I declare a maven property and set value from file content ?
  • Is there another solution that I didn't think about ?
  • Or maybe someone know how to upload synonyms to Elasticsearch with Spring?

Thank you in advance.

2 Answers

1
Peter Palaga On

I do not think you can do this with the built-in Maven resource filtering. But it can be done with a simple inline Groovy script that you invoke via groovy-maven-plugin. Something like the following:

<plugin>
  <groupId>org.codehaus.gmaven</groupId>
  <artifactId>groovy-maven-plugin</artifactId>
  <executions>
    <execution>
      <id>replace-synonyms</id>
      <goals>
        <goal>execute</goal>
      </goals>
      <phase>process-resources</phase>
      <configuration>
        <source>
          def synonyms = new File("${basedir}/src/main/resources/synonyms.txt").getText('UTF-8')
          def template = new File("${basedir}/src/main/resources/settings.json").getText('UTF-8')
          def result = template.replace("@[email protected]", synonyms)
          new File("${basedir}/target/settings.json").write(result,'UTF-8')
        </source>
      </configuration>
    </execution>
  </executions>
</plugin>
0
Martin Choraine On

I found a plugin that handle yaml properties with multiline value :

<plugin>
     <groupId>it.ozimov</groupId>
     <artifactId>yaml-properties-maven-plugin</artifactId>
     <executions>
         <execution>
             <phase>initialize</phase>
              <goals>
                 <goal>read-project-properties</goal>
              </goals>
              <configuration>
                  <files>
                <file>${project.basedir}/src/main/resources/synonyms.yml</file>
                  </files>
               </configuration>
          </execution>
      </executions>
</plugin>

And my configuration file :

synonyms: >
  [
    "finally,all",
    "slap,done",
     ...
  ]