如何正确设置多配置文件 Maven 项目?

发布于 2024-11-14 06:25:30 字数 6708 浏览 3 评论 0原文

嘿,我正在谈论项目的 pom.xml 中的配置文件。请有人向我解释一下,为什么如果我在 pom 定义中有 2 个配置文件,并且我从其中一个配置文件运行测试阶段,那么 Main 方法都会执行,并且所有测试都由 Surefire 插件运行?我的意思是,即使 Surefire 插件也运行所有测试,即使它位于不同的配置文件中?

mvn test -Pcode-generator

第一个,代码生成器,仅用于 Main 方法执行,第二个用于项目的其余部分。

<profiles>
    <profile>
        <id>code-generator</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <version>1.1.1</version>
                    <executions>
                        <execution>
                            <phase>test</phase>
                            <goals>
                                <goal>java</goal>
                            </goals>
                            <configuration>
                                <mainClass>cz.instance.transl.Main</mainClass>
                                <arguments>
                                    <argument>arg0</argument>
                                    <argument>arg1</argument>
                                </arguments>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>

    <profile>
        <id>default</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.1</version>
                    <configuration>
                        <warName>${war.file.name}</warName>
                    </configuration>
                </plugin>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.7</version>
                    <configuration>
                        <useFile>false</useFile>
                        <argLine>-Dportal.test=generic
                            -Dwebdriver.chrome.driver="/opt/google/chrome/chromedriver"
                            -Dwebdriver.development=true
                            -Dwebdriver.firefox.useExisting=true
                            -Dwebdriver.firefox.profile=webdriver
                            -Dwebdriver.reap_profile=true
                            -Dsurefire.useFile=false
                            -Xmx2048M
                            -XX:MaxPermSize=1048M
                            -XX:+CMSClassUnloadingEnabled
                </argLine>
                        <skipTests>false</skipTests>

                        <suiteXmlFiles>
                            <suiteXmlFile>${basedir}/src/test/resources/testng.xml</suiteXmlFile>
                        </suiteXmlFiles>

                        <systemProperties>
                            <property>
                                <name>log4j.configuration</name>
                                <value>META-INF/log4j.xml</value>
                            </property>
                        </systemProperties>

                        <includes>
                            <include>cz/instance/transl/tests/selenium/*Test.java</include>
                        </includes>
                        <excludes>
                            <exclude>cz/instance/transl/tests/sample/*Test.java</exclude>
                        </excludes>
                    </configuration>
                </plugin>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-report-plugin</artifactId>
                    <version>2.7.1</version>
                    <!-- <configuration> <useFile>false</useFile> </configuration> -->
                </plugin>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>2.3.2</version>
                    <configuration>
                        <encoding>UTF-8</encoding>
                        <source>1.5</source>
                        <target>1.5</target>
                    </configuration>
                </plugin>
            </plugins>

            <testResources>
                <testResource>
                    <directory>${project.basedir}/src/test/java</directory>
                    <includes>
                        <include>**/*.*</include>
                    </includes>
                </testResource>
                <testResource>
                    <directory>${project.basedir}/src/test/resources</directory>
                    <includes>
                        <include>**/*</include>
                    </includes>
                </testResource>
            </testResources>

            <resources>
                <resource>
                    <directory>${project.basedir}/src/main/java</directory>
                    <includes>
                        <include>**/*.java</include>
                        <include>service.properties</include>
                    </includes>
                </resource>
                <resource>
                    <directory>${project.basedir}/src/main/resources</directory>
                    <includes>
                        <include>**/*.xml</include>
                        <include>**/*.properties</include>
                        <include>profiles/*</include>
                    </includes>
                </resource>
            </resources>
        </build>
    </profile>
</profiles>

Hey, I'm talking about profiles within pom.xml of a project. Could please anybody explain to me, why if I have 2 profiles in pom definition and I run test phase from one of the profiles, both the Main method is executed and all the tests are run by surefire plugin ? I mean, even the surefire plugin runs all the tests, even though it is within a different profile ?

mvn test -Pcode-generator

the first one,code-generator, is just for Main methods execution and the second one for the rest of the project.

<profiles>
    <profile>
        <id>code-generator</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <version>1.1.1</version>
                    <executions>
                        <execution>
                            <phase>test</phase>
                            <goals>
                                <goal>java</goal>
                            </goals>
                            <configuration>
                                <mainClass>cz.instance.transl.Main</mainClass>
                                <arguments>
                                    <argument>arg0</argument>
                                    <argument>arg1</argument>
                                </arguments>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>

    <profile>
        <id>default</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.1</version>
                    <configuration>
                        <warName>${war.file.name}</warName>
                    </configuration>
                </plugin>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.7</version>
                    <configuration>
                        <useFile>false</useFile>
                        <argLine>-Dportal.test=generic
                            -Dwebdriver.chrome.driver="/opt/google/chrome/chromedriver"
                            -Dwebdriver.development=true
                            -Dwebdriver.firefox.useExisting=true
                            -Dwebdriver.firefox.profile=webdriver
                            -Dwebdriver.reap_profile=true
                            -Dsurefire.useFile=false
                            -Xmx2048M
                            -XX:MaxPermSize=1048M
                            -XX:+CMSClassUnloadingEnabled
                </argLine>
                        <skipTests>false</skipTests>

                        <suiteXmlFiles>
                            <suiteXmlFile>${basedir}/src/test/resources/testng.xml</suiteXmlFile>
                        </suiteXmlFiles>

                        <systemProperties>
                            <property>
                                <name>log4j.configuration</name>
                                <value>META-INF/log4j.xml</value>
                            </property>
                        </systemProperties>

                        <includes>
                            <include>cz/instance/transl/tests/selenium/*Test.java</include>
                        </includes>
                        <excludes>
                            <exclude>cz/instance/transl/tests/sample/*Test.java</exclude>
                        </excludes>
                    </configuration>
                </plugin>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-report-plugin</artifactId>
                    <version>2.7.1</version>
                    <!-- <configuration> <useFile>false</useFile> </configuration> -->
                </plugin>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>2.3.2</version>
                    <configuration>
                        <encoding>UTF-8</encoding>
                        <source>1.5</source>
                        <target>1.5</target>
                    </configuration>
                </plugin>
            </plugins>

            <testResources>
                <testResource>
                    <directory>${project.basedir}/src/test/java</directory>
                    <includes>
                        <include>**/*.*</include>
                    </includes>
                </testResource>
                <testResource>
                    <directory>${project.basedir}/src/test/resources</directory>
                    <includes>
                        <include>**/*</include>
                    </includes>
                </testResource>
            </testResources>

            <resources>
                <resource>
                    <directory>${project.basedir}/src/main/java</directory>
                    <includes>
                        <include>**/*.java</include>
                        <include>service.properties</include>
                    </includes>
                </resource>
                <resource>
                    <directory>${project.basedir}/src/main/resources</directory>
                    <includes>
                        <include>**/*.xml</include>
                        <include>**/*.properties</include>
                        <include>profiles/*</include>
                    </includes>
                </resource>
            </resources>
        </build>
    </profile>
</profiles>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清旖 2024-11-21 06:25:30

Surefire 插件是 Java 项目默认运行的插件。您有多种选择:

  • 将 Surefire 插件添加到您的 code-generator 配置文件中并覆盖它以使其不运行。
  • 使用 -DskipTests 运行。
  • 仅指定您要运行的目标,而不是运行完整的 Maven 构建。

The surefire plugin is one that runs by default for a java project. You have a number of options:

  • Add the surefire plugin to your code-generator profile and override it not to run.
  • Run with -DskipTests.
  • Specify only the goal you want to run, instead of running a full maven build.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文