如何忽略子模块中的maven配置文件?

时间:2015-11-28 23:02:10

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

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

生成图式,解压缩战,运行码头,测试停止,码头,启动 - 停止 - 应用

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

如何让子模块仅运行测试并跳过其余的配置文件(generate-schema等)?

父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 Server 13:03:13 [INFO]

     

13:03:13 [INFO] --- maven-surefire-plugin:2.16:test(integration-test)

     

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

     

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

     

(stop-jetty)@ 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 [INFO]

     

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

     

12.50.9999-SNAPSHOT 13:03:14 [INFO] ------------

1 个答案:

答案 0 :(得分:1)

请让我澄清一些可能解决您问题的重点,因为我发现您的方法可能缺少一些好的做法:

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

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

关于jetty,需要jetty的集成测试也应该是同一模块的一部分,因此模块的构建将遵循经典流程:start / test / stop。 如果您确实需要在不同的模块中启动,测试和停止,那么您可以按照我们之前的问题here进行解释。

我还建议再次查看Maven官方文档的Profile Trafalls 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-跳过“安装”。

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

    ...
   <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>
 ...

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

总而言之,请注意,使用无法提供任何真实伪像的配置文件和模块进行过多操作可能会影响您的构建可读性和维护。