防止在没有激活配置文件的情况下构建 Maven 模块

Prevent maven module from being built with no activated profile

我有一个带有父 POM 的多模块 Maven 项目。项目的一个子项目是通过配置文件为不同的环境定制的,父 pom 也根据所选环境(配置文件)构建所需的模块。

我想阻止构建(在子模块中读取为 "to run mvn package")未激活配置文件的自定义子项目,因为环境没有 "generic" 或 "default" 版本。换句话说,我想强制开发人员使用依赖于环境的配置文件。

可以吗?

Maven Enforcer就是为了这个需求而制作的。只需添加所需的配置文件。

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-enforcer-plugin</artifactId>
        <version>1.4</version>
        <executions>
          <execution>
            <id>enforce-all-profiles-are-activated</id>
            <goals>
              <goal>enforce</goal>
            </goals>
            <configuration>
              <rules>
                <requireActiveProfile>
                  <profiles>first,second</profiles>
                </requireActiveProfile>
              </rules>
              <fail>true</fail>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  [...]
</project>