【问题标题】:How to avoid maven profile duplication如何避免 Maven 配置文件重复
【发布时间】:2013-04-10 08:16:45
【问题描述】:

我有一个 webapp 的 maven 项目,它使用覆盖重新打包战争依赖项。对于两个配置文件,testprod,它应该排除 demo.jsp 文件,但对于其他配置文件,例如 local,这个文件应该保留。有没有办法让两个配置文件只有一个配置?我不想为两个配置文件重复一种配置。 我目前的解决方案:

<profiles>
    <profile>
        <id>test</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.3</version>
                    <configuration>
                        <overlays>
                            <overlay>
                                <groupId>mygroup</groupId>
                                <artifactId>mywebapp</artifactId>
                                <excludes>
                                    <exclude>demo.jsp</exclude>
                                </excludes>
                            </overlay>
                        </overlays>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
    <profile>
        <id>prod</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.3</version>
                    <configuration>
                        <overlays>
                            <overlay>
                                <groupId>mygroup</groupId>
                                <artifactId>mywebapp</artifactId>
                                <excludes>
                                    <exclude>demo.jsp</exclude>
                                </excludes>
                            </overlay>
                        </overlays>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

编辑:testprod 配置文件是相同的

【问题讨论】:

  • test 和 prod 有什么区别吗?它们在您的 sn-p 中看起来很相似。也许你应该检查功能切换 - martinfowler.com/bliki/FeatureToggle.html
  • @SpaceTrucker 测试和产品配置文件相同

标签: maven dry


【解决方案1】:

Plugin Management 可以为我们提供以下帮助:-

<build>
    <pluginManagement>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <overlays>
                    <overlay>
                        <groupId>mygroup</groupId>
                        <artifactId>mywebapp</artifactId>
                        <excludes>
                            <exclude>demo.jsp</exclude>
                        </excludes>
                    </overlay>
                </overlays>
            </configuration>
        </plugin>
    </pluginManagement>
</build>

那么profiles应该如下:-

<profiles>
    <profile>
        <id>test</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
        ...
    </profile>
    <profile>
        <id>prod</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
        ...
    </profile>
</profiles>    

我希望这会有所帮助。

【讨论】:

  • 但随后必须定义 local 配置文件以某种方式覆盖来自 pluginManagement 的配置。 demo.jsp 应该保留在 local 配置文件中。
  • 如果我们将排除的 jsp 定义为属性,例如 ${my.excluded.jsp} 会怎样?并让每个配置文件定义自己?
  • 这会很好,但不是理想的解决方案:-)。有没有办法为具有两个不同 ID 的两个配置文件配置一个配置文件?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-30
  • 2016-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多