【问题标题】:How to ignore maven profiles in child module?如何忽略子模块中的 Maven 配置文件?
【发布时间】:2016-03-02 20:10:13
【问题描述】:

我想运行简单的流程,我有 6 个配置文件:

generate-schema,unpack-war,run-jetty,test,stop-jetty,start-stop-app

当我在 mvn 目标/属性中声明测试配置文件时,它将在不同的子模块上运行: * clean --activate-profiles generate-schema,unpack-war,start-stop-app,test --projects apm-tests,apm-tests\apm-adapter-tests verify.*

如何让子模块只运行测试并跳过其余的配置文件(生成模式等)?

父 pom 示例:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <project>

        <parent>
            <artifactId>apm-root</artifactId>
            <groupId>com.apm.platform</groupId>
            <version>12.50.9999-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>

        <artifactId>apm-tests</artifactId>

        <packaging>pom</packaging>

        <modules>
            <module>alm-coverage-report</module>
        </modules>

            <profile>
                <id>test</id>
                <build>
                    <plugins>
                        <plugin>
                            <artifactId>maven-surefire-plugin</artifactId>
                            <configuration>
                                <skipTests>true</skipTests>
                            </configuration>
                        </plugin>
                    </plugins>
                </build>
            </profile>

    <profile>
                <id>generate-schema</id>
                <activation>
                    <activeByDefault>false</activeByDefault>
                </activation>

                <build>
                    <plugins>
                        <plugin>
                            <groupId>org.codehaus.mojo</groupId>
                            <artifactId>exec-maven-plugin</artifactId>
                            <executions>
                                <execution>
                                    <id>generate-schema</id>
                                    <phase>pre-integration-test</phase>
                                    <goals>
                                        <goal>java</goal>
                                    </goals>
                                    <configuration>
                                        <mainClass>com.apm.platform.siteadmin.setup.Setup</mainClass>
                                        <classpathScope>test</classpathScope>
                                        <systemProperties>
                                            <systemProperty>
                                                <key>InstallationFolder</key>
                                                <value>${tests.runtime}</value>
                                            </systemProperty>
                                            <systemProperty>
                                                <key>RepositoryFolder</key>
                                                <value>${tests.runtime}/repository</value>
                                            </systemProperty>
                                            <systemProperty>
                                                <key>LogFolder</key>
                                                <value>${tests.runtime}/log</value>
                                            </systemProperty>
                                            <systemProperty>
                                                <key>mercury.td.sa_config_dir</key>
                                                <value>${tests.runtime}</value>
                                            </systemProperty>
                                            <systemProperty>
                                                <key>CreateLabProject</key>
                                                <value>${create.lab.project}</value>
                                            </systemProperty>
                                        </systemProperties>
                                        <arguments>
                                            <argument>--auto</argument>
                                            <argument>${db.schema}</argument>
                                        </arguments>
                                    </configuration>
                                </execution>
                            </executions>
                        </plugin>
                    </plugins>
                </build>
            </profile>
            <profile>
                <id>start-jetty</id>
                <activation>
                    <activeByDefault>false</activeByDefault>
                </activation>
                <build>
                    <plugins>
                        <plugin>
                            <groupId>org.eclipse.jetty</groupId>
                            <artifactId>jetty-maven-plugin</artifactId>
                            <configuration>
                                <war>${unpacked.war.directory}</war>
                                <contextXml>${unpacked.war.directory}/WEB-INF/jetty-web.xml</contextXml>
                                <webApp>
                                    <contextPath>/qcbin</contextPath>
                                </webApp>
                                <systemProperties>
                                    <systemProperty>
                                        <name>mercury.td.sa_config_dir</name>
                                        <value>${tests.runtime}</value>
                                    </systemProperty>
                                    <systemProperty>
                                        <name>jetty.port</name>
                                        <value>${jetty.start.port}</value>
                                    </systemProperty>
                                </systemProperties>
                                <stopPort>${jetty.stop.port}</stopPort>
                                <stopKey>STOP</stopKey>
                            </configuration>
                            <executions>
                                <execution>
                                    <id>start-jetty</id>
                                    <phase>pre-integration-test</phase>
                                    <goals>
                                        <goal>deploy-war</goal>
                                    </goals>
                                    <configuration>
                                        <scanIntervalSeconds>0</scanIntervalSeconds>
                                        <daemon>false</daemon>
                                    </configuration>
                                </execution>
                            </executions>
                        </plugin>
                    </plugins>
                </build>
            </profile>

          <profile>
            <id>start-stop-app</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.eclipse.jetty</groupId>
                        <artifactId>jetty-maven-plugin</artifactId>
                        <configuration>
                            <war>${unpacked.war.directory}</war>
                            <contextXml>${unpacked.war.directory}/WEB-INF/jetty-web.xml</contextXml>
                            <webApp>
                                <contextPath>/qcbin</contextPath>
                            </webApp>
                            <systemProperties>
                                <systemProperty>
                                    <name>mercury.td.sa_config_dir</name>
                                    <value>${tests.runtime}</value>
                                </systemProperty>
                                <systemProperty>
                                    <name>jetty.port</name>
                                    <value>${jetty.start.port}</value>
                                </systemProperty>
                            </systemProperties>
                            <stopPort>${jetty.stop.port}</stopPort>
                            <stopKey>STOP</stopKey>
                        </configuration>
                        <executions>
                            <execution>
                                <id>start-jetty</id>
                                <phase>pre-integration-test</phase>
                                <goals>
                                    <goal>deploy-war</goal>
                                </goals>
                                <configuration>
                                    <scanIntervalSeconds>0</scanIntervalSeconds>
                                    <daemon>true</daemon>
                                </configuration>
                            </execution>
                            <execution>
                                <id>stop-jetty</id>
                                <phase>post-integration-test</phase>
                                <goals>
                                    <goal>stop</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>

            <profile>
                <id>stop-jetty</id>
                <activation>
                    <activeByDefault>false</activeByDefault>
                </activation>
                <build>
                    <plugins>
                        <plugin>
                            <groupId>org.eclipse.jetty</groupId>
                            <artifactId>jetty-maven-plugin</artifactId>
                            <configuration>
                                <stopPort>${jetty.stop.port}</stopPort>
                                <stopKey>STOP</stopKey>
                            </configuration>
                            <executions>
                                <execution>
                                    <id>stop-jetty</id>
                                    <phase>post-integration-test</phase>
                                    <goals>
                                        <goal>stop</goal>
                                    </goals>
                                </execution>
                            </executions>
                        </plugin>
                    </plugins>
                </build>
            </profile>


            <!-- create project profile -->
            <profile>
                <id>create-project</id>
                <activation>
                    <activeByDefault>false</activeByDefault>
                </activation>
                <properties>
                    <serverUrl>http://localhost:${env.JETTY_PORT}/qcbin</serverUrl>
                    <saUser>sa</saUser>
                    <projectUserName>restuser</projectUserName>
                    <domain>UNITEST</domain>
                    <project>resttest</project>
                    <overwrite>true</overwrite>
                </properties>
                <build>
                    <plugins>
                        <plugin>
                            <groupId>com.apm.maven.plugins.alm</groupId>
                            <artifactId>rest-create-project</artifactId>
                            <version>1.0.8.2</version>
                            <executions>
                                <execution>
                                    <id>clean-create</id>
                                    <phase>pre-integration-test</phase>
                                    <goals>
                                        <goal>create-project</goal>
                                    </goals>
                                    <configuration>
                                        <serverUrl>${serverUrl}</serverUrl>
                                        <saUser>${saUser}</saUser>
                                        <projectUserName>${projectUserName}</projectUserName>
                                        <projectUserPassword>${projectUserPassword}</projectUserPassword>
                                        <domain>${domain}</domain>
                                        <project>${project}</project>
                                        <overwrite>${overwrite}</overwrite>
                                        <extensions>${extensions}</extensions>
                                    </configuration>
                                </execution>
                            </executions>
                        </plugin>
                    </plugins>
                </build>
            </profile>

        </profiles>
    </project>

子模块 pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>com.apm.platform</groupId>
        <artifactId>apm-tests</artifactId>
        <version>12.50.9999-SNAPSHOT</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>

    <artifactId>apm-adapter-tests</artifactId>
    <packaging>pom</packaging>


    <properties>
        <tests.version>${project.version}</tests.version>
    </properties>

    <dependencies>
       <dependency>
                      <groupId>com.apm.platform</groupId>
                      <artifactId>apm-synchronizer-adapter-testing</artifactId>
                      <version>1.10.9999-SNAPSHOT</version>
                      <classifier>test-jar-with-dependencies</classifier>

       </dependency>
    </dependencies>

    <profiles>
        <!--run integration tests using surefire plugin -->
        <profile>
            <id>itest</id>

            <build>
                <plugins>
                    <plugin>

                        <artifactId>maven-surefire-plugin</artifactId>
                        <version>${surefire.version}</version>
                        <dependencies>
                            <dependency>
                                <groupId>org.apache.maven.plugins</groupId>
                                <artifactId>maven-surefire-report-plugin</artifactId>
                                <version>${surefire.version}</version>
                            </dependency>
                        </dependencies>
                        <executions>
                            <execution>
                                <id>integration-test</id>
                                <phase>integration-test</phase>
                                <goals>
                                    <goal>test</goal>
                                </goals>
                                <configuration>
                                    <excludedGroups>com.apm.platform.CAPQIntegrationTestCategory</excludedGroups>
                                    <skipTests>false</skipTests>
                                    <excludes>
                                      <exclude>junit/**/*.java</exclude>
                                      <exclude>org/**/*.java</exclude>
                                    </excludes>
                                    <argLine>-Xmx1536m</argLine>

                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>

        <profile>
            <id>sync-adapter</id>
            <dependencies>
                <dependency>
                      <groupId>com.apm.platform</groupId>
                      <artifactId>apm-synchronizer-adapter-testing</artifactId>
                      <version>1.10.9999-SNAPSHOT</version>
                      <classifier>test-jar-with-dependencies</classifier>
                      <scope>test</scope>
                </dependency>
            </dependencies>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-dependency-plugin</artifactId>
                        <executions>
                            <execution>
                                <!--copies dependent classes from test-jars-->
                                <id>unpack-tests</id>
                                <phase>pre-integration-test</phase>
                                <goals>
                                    <goal>unpack</goal>
                                </goals>
                                <configuration>
                                    <artifactItems>
                                        <artifactItem>
                                            <groupId>com.apm.platform</groupId>
                                            <artifactId>agm-synchronizer-adapter-testing</artifactId>
                                            <version>1.10.9999-SNAPSHOT</version>
                                            <classifier>test-jar-with-dependencies</classifier>
                                            <outputDirectory>${project.build.testOutputDirectory}</outputDirectory>
                                        </artifactItem>
                                    </artifactItems>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

2015-11-30 13:03:13 13:03:14.422:INFO:oejs.Server:main: 开始

@207555ms 13:03:13 [INFO] 启动 Jetty 服务器 13:03:13 [INFO]

13:03:13 [INFO] --- maven-surefire-plugin:2.16:test(集成测试)

@ apm-tests --- 13:03:14 [INFO] 没有要运行的测试。 13:03:14 [INFO]

13:03:14 [INFO] --- jetty-maven-plugin:9.1.3.v20140225:stop

(停止码头)@ apm-tests --- 13:03:14 [INFO] 13:03:14 [INFO] ---

maven-source-plugin:2.2.1:jar-no-fork (attach-sources) @ alm-tests ---

2015-11-30 13:03:14

13:03:14.820:INFO:oejs.ServerConnector:ShutdownMonitor: 已停止

ServerConnector@4c8d3b5c{HTTP/1.1}{0.0.0.0:5729} 13:03:14 [INFO]

13:03:14 [INFO] --- maven-source-plugin:2.2.1:test-jar-no-fork

(attach-test-sources)@alm-tests --- 13:03:14 [INFO]

13:03:14 [信息]

--------------- 13:03:14 [INFO] 构建 agm-synchronizer-adapter-testing

12.50.9999-SNAPSHOT 13:03:14 [信息] ------------

【问题讨论】:

  • 一些问题:子模块在执行测试时将 pom 作为包装是否正常? pom 打包模块/项目并不意味着提供任何源代码/测试源代码。而且,所描述的父子之间没有任何关系(父模块中没有子,子没有这个父作为父),这是故意的吗?
  • @A.DiMatteo 好的。所以子模块包装是- pom 但我想将它作为测试运行,但是当我将它声明为测试时出现错误。我希望父母与孩子没有关系(如果可能的话),但孩子父母确实是父母 pom.xml (也修复了)。目的仍然是父亲将运行所有配置文件,而孩子将只运行测试。 ——
  • 完全有可能拥有与模块没有关系的父级,在 Maven 中聚合和继承是两种不同的机制,可以混合在一起或以完全分离的方式使用。也就是说,聚合器项目可以具有可以具有其他父级的模块。进一步的问题:为什么有这么多配置文件?为什么码头的启动/停止在两个不同的配置文件中?你真的需要把它分成两个配置文件吗?
  • 不-我还有一个将这两者与测试结合在一起的配置文件(我已经在问题中更新了它),配置文件名为“start-stop-app”。
  • 但是如果我使用这个配置文件,它不会运行测试,因为测试在子模块中。当他运行时,码头已经关闭。 (我在问题中添加了日志)

标签: maven jetty maven-plugin maven-jetty-plugin


【解决方案1】:

由于我发现您的方法中可能缺少一些好的做法,请让我澄清一些重要的点,它们也可能解决您的问题:

  • 配置文件应用于可选/附加行为,而默认构建应始终为SUCCESSFUL,无论声明的配置文件如何
  • 如果一个模块应该执行测试并且测试只位于该模块中,那么配置文件应该只在该特定模块中声明,而不是在其父模块中声明。此外,在这种情况下,也不需要配置文件,因为它将是默认模块行为。但是,仅在模块中定义配置文件将允许您将其关闭,即使在从父级执行整个构建时也是如此。要关闭配置文件,只需使用! 或- 注释:mvn clean install -P!profile-name,如官方文档here 中所述。如果配置文件仅在该模块中声明,这将允许忽略子模块中的 Maven 配置文件(来回答您的问题)。

您可以将相同的概念应用于 generate-schema 操作:所有模块都需要它吗?可能不是,那么您可以仅在需要它的特定模块上声明配置文件,然后如上所述打开/关闭它(如果默认情况下应该打开,则默认情况下将配置文件声明为活动)。

关于码头,需要码头的集成测试也应该是同一模块的一部分,以便模块的构建将遵循经典流程:启动/测试/停止。 如果你真的需要在不同的模块中进行启动、测试和停止,那么你可以按照我们上一个问题的解释,here。

我还建议再次阅读 Maven 官方文档的 Profile Pitfalls,here,以便更好地了解配置文件的使用和误用。

此外,如果有任何帮助,您甚至可以使用命令行-pl 选项跳过整个模块构建作为多模块 maven 构建的一部分。检查mvn -help 了解更多详情。因此,您可以提供要构建的模块列表。从 maven 3.2.1 开始,您还可以提供要跳过的模块列表,如下所示:mvn -pl !module-to-skip install。

当使用! 符号跳过配置文件或模块时,请注意它也是 linux 中 bash 的特殊字符,因此请将其放在单引号之间 (mvn -pl'!module-to-skip' install). The same doesn't work on Windows however, where double quotes should be used instead (mvn -pl "!module-to-skip"安装`)。

此外,要跳过作为父构建的一部分的模块,您还可以在父 POM 中定义一个配置文件,该配置文件重新声明模块部分并省略您要跳过的模块,如另一个 answer 中所述。

    ...
   <modules>
      <module>module1</module>
      <module>module2</module>  
      ...
  </modules>
  ...
  <profiles>
     <profile>
       <id>skip-some-modules-profile</id>
          <modules>
            <module>module1</module>
            ...
            <module>module-integration-test</module>
          </modules> 
      </profile>
  </profiles>
 ...

因此,您可以根据要执行的配置文件制作特殊的多模块构建。

总而言之,请注意,过多地使用不提供任何真实工件的配置文件和模块可能会影响构建的可读性和维护。

【讨论】:

    猜你喜欢
    • 2017-01-24
    • 2016-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-16
    • 2011-09-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多