在默认执行上运行验证阶段时如何向用户显示消息



在我们的 maven 构建中 - 我们使用默认配置文件进行构建,并使用不同的配置文件进行测试。当我们的测试在默认配置文件下运行时,由于各种原因,它们会中断。

例如,我们的团队与我们的构建很好

mvn -Pfoo verify

我们的团队与我们的构建很糟糕

mvn verify

我们希望鼓励我们团队中的人员在"foo"生命周期下运行测试,并在他们不这样做时警告他们。

我目前解决此问题的方法是为默认配置文件创建一个新的万无一失的测试,排除除新DefaultProfileWarningTest之外的所有测试,新其唯一目的是告诉用户在foo配置文件下运行测试。

因此,测试可能如下所示:

public class DefaultProfileWarningTest {
    @Test
    public void displayWarning() {
        System.out.println("The tests aren't running - you should have run **mvn -Pfoo verify**");
    }
}

pom.xml中的执行类似于:

<profiles>
    <profile>
        <id>foo</id>
        ...      
    </profile>
    <profile>
        <id>my-default-profile</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.19.1</version>
                    <configuration>
                        <includes>
                            <include>DefaultProfileWarningTest.java</include>
                        </includes>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
<profiles>

但这似乎是一个过度紧张的笨拙。假设我们无法修复配置文件问题 - 当验证阶段适用于默认配置文件时,是否有一种简单的轻量级方法来向用户显示消息?

我的问题是:在 maven 中 - 当验证阶段在默认执行下运行时,如何向用户显示消息?

一个可能的解决方案是定义一个maven-antrun-plugin的执行,该与echo任务一起回显消息。在foo配置文件下运行时,使用属性 skip 时,将跳过它。可以从设置为true foo配置文件中的 Maven 属性进行切换,默认情况下false

请考虑以下事项:

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
        <execution>
            <phase>test</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <target>
                    <echo>The tests aren't running - you should have run mvn -Pfoo verify</echo>
                </target>
                <skip>${isRightProfile}</skip>
            </configuration>
        </execution>
    </executions>
</plugin>

然后,您可以像这样配置配置文件:

<profile>
    <id>foo</id>
    <properties>
        <isRightProfile>true</isRightProfile>
    </properties>
</profile>
<profile>
    <id>my-default-profile</id>
    <properties>
        <isRightProfile>false</isRightProfile>
    </properties>
    <!-- rest unchanged -->
</profile>

激活 foo 配置文件时,isRightProfile 属性将设置为 true ,因此将跳过 AntRun 插件的执行,并且不会打印任何消息。激活默认配置文件后,此属性将设置为 false,并将回显消息。

您可以简单地使用适当的插件(如 echo-maven-plugin(来打印消息。

<plugin>
  <groupId>com.soebes.maven.plugins</groupId>
  <artifactId>echo-maven-plugin</artifactId>
  <version>0.2</version>
  <executions>
    <execution>
      <phase>initialize</phase>
      <goals>
        <goal>echo</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <echos>
      <echo>This is the Text which will be printed out.</echo>
    </echos>
  </configuration>
</plugin>
如果你想强迫人们使用特定的配置文件,

你应该使用maven-enforcer-plugin,它可以强迫你使用特定的配置文件。

最新更新