Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to loop the files in a directory, and do some tasks based on each filename?

Tags:

loops

maven

There is a properties directory in my project:

properties
    - project_dev.properties
    - project_test.properties
    - project_prod.properties
    - project_other.properties

Which defines some different values for different enviroments. And there is also a template directory, which contains some configuration file templates with placeholders.

What I want to do is to loop all the files in properties, and combine each one to the template directory to generate the final configuration files, which are under different directories based the file name.

So it will be:

target
   - configurations
       - dev
            - ... some files
       - test
            - ... some files
       - prod
            - ... some files
       - other
            - ... some files

What I'm doing now is to use the maven-resources-plugin, and define several executions for each of the files, and hard-coded the names.

It looks like:

<plugin>
    <artifactId>maven-resources-plugin</artifactId>
        <version>2.6</version>
        <executions>
            <!--- for dev ---->
            <execution>
                <id>dev-filter</id>
                <phase>validate</phase>
                <goals>
                    <goal>copy-resources</goal>
                </goals>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <outputDirectory>${basedir}/target/conf/dev</outputDirectory>
                    <useDefaultDelimiters>false</useDefaultDelimiters>
                    <resources>
                        <resource>
                        <directory>${basedir}/conf/template</directory>
                        <filtering>true</filtering>
                        <includes>
                            <include>channel/*</include>
                            <include>rule-config/*</include>
                            <include>server/*.properties</include>
                        </includes>
                        </resource>
                    </resources>
                    <delimiters>
                        <delimiter>${*}</delimiter>
                    </delimiters>
                    <filters>
                        <filter>${basedir}/properties/project_dev.properties</filter>
                    </filters>
                </configuration>
            </execution>
            <!--- for prod ---->
            <execution>
                <id>prod-filter</id>
                <phase>validate</phase>
                <goals>
                    <goal>copy-resources</goal>
                </goals>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <outputDirectory>${basedir}/target/conf/prod</outputDirectory>
                    <useDefaultDelimiters>false</useDefaultDelimiters>
                    <resources>
                        <resource>
                        <directory>${basedir}/conf/template</directory>
                        <filtering>true</filtering>
                        <includes>
                            <include>channel/*</include>
                            <include>rule-config/*</include>
                            <include>server/*.properties</include>
                        </includes>
                        </resource>
                    </resources>
                    <delimiters>
                        <delimiter>${*}</delimiter>
                    </delimiters>
                    <filters>
                        <filter>${basedir}/properties/project_prod.properties</filter>
                    </filters>
                </configuration>
            </execution>
            <!--- for test --->
             ...

You can see I'm copying the execution block for each file, so there will be many duplicated code with small differences.

I wonder is there any solution to make things simpler? I tried to find a way to loop the files under properties and do the filter task for each of them, but not sure how to do it.

like image 763
Freewind Avatar asked Aug 08 '13 04:08

Freewind


1 Answers

A simple solution for such purposes is the Iterator-maven-plugin

<plugin>
  <groupId>com.soebes.maven.plugins</groupId>
  <artifactId>iterator-maven-plugin</artifactId>
  <version>0.1.0</version>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>executor</goal>
      </goals>
      <configuration>
        <items>
          <item>dev</item>
          <item>test</item>
          <item>prod</item>
        </items>

        <pluginExecutors>
          <pluginExecutor>
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-resources-plugin</artifactId>
              <version>2.6</version>
            </plugin>
            <goal>copy-resources</goal>
            <configuration>
                 <outputDirectory>${basedir}/target/conf/@item@</outputDirectory>
                 <useDefaultDelimiters>false</useDefaultDelimiters>
                 <resources>
                   <resource>
                     <directory>${basedir}/conf/template</directory>
                     <filtering>true</filtering>
                     <includes>
                        <include>channel/*</include>
                        <include>rule-config/*</include>
                        <include>server/*.properties</include>
                      </includes>
                     </resource>
                   </resources>
                    <delimiters>
                        <delimiter>${*}</delimiter>
                    </delimiters>
                    <filters>
                        <filter>${basedir}/properties/project_@[email protected]</filter>
                    </filters>
               </configuration>
          </pluginExecutor>
        </pluginExecutors>
      </configuration>
    </execution>
  </executions>
</plugin>

The documentation contains also an example which is very similar to your needs.

Apart from that i would suggest to use the following properties in your pom file:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

than you don't need to define the encoding separately for the maven-resources-plugin.

like image 168
khmarbaise Avatar answered Oct 08 '22 01:10

khmarbaise