8

我的项目中有一个properties目录:

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

它为不同的环境定义了一些不同的值。并且还有一个template目录,里面包含了一些带有占位符的配置文件模板。

我想要做的是循环所有文件properties,并将每个文件组合到template目录中以生成最终的配置文件,这些文件根据文件名位于不同的目录下。

所以它将是:

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

我现在正在做的是使用, 并为每个文件maven-resources-plugin定义几个s ,并对名称进行硬编码。execution

看起来像:

<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 --->
             ...

您可以看到我正在execution为每个文件复制块,因此会有许多重复的代码,但差异很小。

我想知道有什么解决方案可以让事情变得更简单吗?我试图找到一种方法来循环文件propertiesfilter为每个文件执行任务,但不知道该怎么做。

4

1 回答 1

10

用于此类目的的一个简单解决方案是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_@item@.properties</filter>
                    </filters>
               </configuration>
          </pluginExecutor>
        </pluginExecutors>
      </configuration>
    </execution>
  </executions>
</plugin>

该文档还包含一个与您的需求非常相似的示例。

除此之外,我建议在您的 pom 文件中使用以下属性:

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

比您不需要为 maven-resources-plugin 单独定义编码。

于 2013-08-08T06:08:58.477 回答