【问题标题】:maven include different files for different profilemaven 为不同的配置文件包含不同的文件
【发布时间】:2012-03-11 00:15:54
【问题描述】:

我想为不同的 maven 配置文件提供不同的 spring 配置文件。 Filtering 接近我想要的,但它只更改一个文件中的属性。我想要的是根据配置文件包含/排除和重命名文件。例如,我有文件 profile1-config.xml 和 profile2-config.xml。对于 profile1 构建,profile1-config.xml 被重命名为 config.xml,并且 profile2-config.xml 被排除在构建之外。对于 profile2 构建,profile2-config.xml 被重命名为 config.xml,并且 profile1-config.xml 被排除在构建之外。 这在maven中可行吗?

【问题讨论】:

    标签: maven


    【解决方案1】:

    你的想法不会这样,但如果你修改它,它会如下所示:

    假设您宁愿为每个配置文件创建 conf 文件夹并将这些文件吸进去。

    src/main/conf
      |-/profile1/conf.xml
      |-/profile2/conf.xml
    

    等等。配置您的个人资料以接收这些文件。如果您打算为某些服务器部署不同的配置,最好使用附加模块和战争覆盖,因为您无法在 Nexus 或本地 repo 中一次部署同一模块项目的多个配置。此外,考虑到许多配置文件会使您的 pom 变得混乱并给构建带来更多复杂性。

    【讨论】:

      【解决方案2】:

      总体思路是在maven-resources-plugin中使用copy-resources目标。

      您可以创建一个文件夹来保存您的所有个人资料,例如:

      profiles
          |-profile1
          |-profile2
      

      在你的 pom.xml 中,你可以有这些设置:

      <profiles>
              <profile>
                  <id>profile1</id>
                  <activation>
                      <activeByDefault>true</activeByDefault>
                  </activation>
                  <build>
                      <plugins>
                          <plugin>
                              <groupId>org.apache.maven.plugins</groupId>
                              <artifactId>maven-resources-plugin</artifactId>
                              <executions>
                                  <execution>
                                      <id>copy-resources</id>
                                      <phase>validate</phase>
                                      <goals>
                                          <goal>copy-resources</goal>
                                      </goals>
                                      <configuration>
                                          <outputDirectory>${basedir}/target/classes</outputDirectory>
                                          <resources>
                                              <resource>
                                                  <directory>${basedir}/profiles/profile1</directory>
                                                  <filtering>false</filtering>
                                              </resource>
                                          </resources>
                                      </configuration>
                                  </execution>
                              </executions>
                          </plugin>
                      </plugins>
                  </build>
              </profile>
      
              <profile>
                  <id>profile2</id>
                  <build>
                      <plugins>
                          <plugin>
                              <groupId>org.apache.maven.plugins</groupId>
                              <artifactId>maven-resources-plugin</artifactId>
                              <executions>
                                  <execution>
                                      <id>copy-resources</id>
                                      <phase>validate</phase>
                                      <goals>
                                          <goal>copy-resources</goal>
                                      </goals>
                                      <configuration>
                                          <outputDirectory>${basedir}/target/classes</outputDirectory>
                                          <resources>
                                              <resource>
                                                  <directory>${basedir}/profiles/profile2</directory>
                                                  <filtering>false</filtering>
                                              </resource>
                                          </resources>
                                      </configuration>
                                  </execution>
                              </executions>
                          </plugin>
                      </plugins>
                  </build>
              </profile>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-06-16
        • 2012-11-19
        • 2012-11-04
        • 2014-05-15
        • 2011-09-16
        • 1970-01-01
        • 2020-02-16
        • 2022-10-05
        相关资源
        最近更新 更多