maven -P 选项不适用于 mvn clean install

maven -P option not working with mvn clean install

当我尝试使用 mvn clean package -P dev,它激活 dev 配置文件并执行构建。但是当我尝试使用 mvn clean install -P dev 时,它会构建所有配置文件。 我的 pom.xml.

中有多个个人资料
<profiles>
    <!-- The Configuration of the development profile -->
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <build.profile.id>dev</build.profile.id>
            <skip.integration.tests>true</skip.integration.tests>
            <skip.unit.tests>false</skip.unit.tests>
        </properties>
    </profile>
    <!-- The Configuration of the integration-test profile -->
    <profile>
        <id>integration-test</id>
        <activation>
            <activeByDefault>false</activeByDefault>
        </activation>
        <properties>
            <build.profile.id>integration-test</build.profile.id>
            <skip.integration.tests>false</skip.integration.tests>
            <skip.unit.tests>true</skip.unit.tests>
        </properties>
    </profile>
</profiles>

也尝试了 mvn clean install --activate-profiles=dev,但结果相同。即使 -D 选项也不起作用。

========================更新==================== =================
请在此处找到完整的 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">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.myservice</groupId>
        <artifactId>myservicename</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.9.RELEASE</version>
            <relativePath /> <!-- lookup parent from repository -->
        </parent>
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
            <!-- Below property indicates the pattern of the test suite -->
            <runSuite>**/*Test.class</runSuite>
        </properties>
        <profiles>
            <!-- The Configuration of the development profile -->
            <profile>
                <id>dev</id>
                <activation>
                    <activeByDefault>true</activeByDefault>
                </activation>
                <properties>
                    <build.profile.id>dev</build.profile.id>
                    <skip.integration.tests>true</skip.integration.tests>
                    <skip.unit.tests>false</skip.unit.tests>
                </properties>
            </profile>
            <!-- The Configuration of the integration-test profile -->
            <profile>
                <id>integration-test</id>
                <properties>
                    <build.profile.id>integration-test</build.profile.id>
                    <skip.integration.tests>false</skip.integration.tests>
                    <skip.unit.tests>true</skip.unit.tests>
                </properties>
            </profile>
        </profiles>
        <dependencies>
            <!-- All Dependencies -->
        </dependencies>
        <build>
            <plugins>
                <!-- Adds source and resource directories to build -->
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>build-helper-maven-plugin</artifactId>
                    <version>1.10</version>
                    <executions>
                        <!-- Add a new source directory to our build -->
                        <execution>
                            <id>add-integration-test-sources</id>
                            <phase>generate-test-sources</phase>
                            <goals>
                                <goal>add-test-source</goal>
                            </goals>
                            <configuration>
                                <!-- Configures the source directory of our integration tests -->
                                <sources>
                                    <source>src/integration-test/java</source>
                                </sources>
                            </configuration>
                        </execution>
                        <!-- Add a new resource directory to our build -->
                        <execution>
                            <id>add-integration-test-resources</id>
                            <phase>generate-test-resources</phase>
                            <goals>
                                <goal>add-test-resource</goal>
                            </goals>
                            <configuration>
                                <!-- Configures the resource directory of our integration tests -->
                                <resources>
                                    <!-- Placeholders that are found from the files located in the configured 
                                        resource directories are replaced with the property values found from the 
                                        profile specific configuration file. -->
                                    <resource>
                                        <filtering>true</filtering>
                                        <directory>src/integration-test/resources</directory>
                                    </resource>
                                </resources>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                <!-- Below plugin ensures the execution of test cases during maven build -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                        <!-- Skips unit tests if the value of skip.unit.tests property is true -->
                        <skipTests>${skip.unit.tests}</skipTests>
                        <includes>
                            <include>${runSuite}</include>
                        </includes>
                        <excludes>
                            <exclude>com.myservice.testsuite.service.*</exclude>
                        </excludes>
                    </configuration>
                </plugin>
                <!-- Runs integration tests -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-failsafe-plugin</artifactId>
                    <version>2.18</version>
                    <executions>
                        <!-- Invokes both the integration-test and the verify goals of the Failsafe Maven plugin -->
                        <execution>
                            <id>integration-tests</id>
                            <goals>
                                <goal>integration-test</goal>
                                <goal>verify</goal>
                            </goals>
                            <configuration>
                                <!-- Skips integration tests if the value of skip.integration.tests property is true -->
                                <skipTests>${skip.integration.tests}</skipTests>
                                <includes>
                                    <include>**/*IntegrationTest.class</include>
                                </includes>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </pluginManagement>
        </build>
    </project>

还有 mvn clean install -P dev 的控制台输出:

c:\path\to\gitrepo\service>mvn clean install -P dev
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building TwelveFactorAnswers 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.6.1:clean (default-clean) @ myservicename ---
[INFO] Deleting c:\path\to\gitrepo\service\target
[INFO]
[INFO] --- jacoco-maven-plugin:0.7.9:prepare-agent (jacoco-initialize) @ myservicename ---
[INFO] 
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ myservicename ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ myservicename ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 16 source files to c:\path\to\gitrepo\service\target\classes
[INFO]
[INFO] --- build-helper-maven-plugin:1.10:add-test-source (add-integration-test-sources) @ myservicename ---
[INFO] Test Source directory: c:\path\to\gitrepo\service\src\integration-test\java added.
[INFO]
[INFO] --- build-helper-maven-plugin:1.10:add-test-resource (add-integration-test-resources) @ myservicename ---
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ myservicename ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory c:\path\to\gitrepo\service\src\test\resources
[INFO] Copying 3 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ myservicename ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 4 source files to c:\path\to\gitrepo\service\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ myservicename ---
[INFO] Surefire report directory: c:\path\to\gitrepo\service\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
    ||
    ||
    || All the JUnit Test runs - sample Junit Test Class - <ServiceName>Test.java
    ||
    ||  
Results :

Tests run: 5, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- jacoco-maven-plugin:0.7.9:check (jacoco-check) @ myservicename ---
[INFO] Loading execution data file c:\path\to\gitrepo\service\target\jacoco.exec
[INFO] Analyzed bundle 'myservicename' with 16 classes
[INFO] All coverage checks have been met.
[INFO]
[INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ myservicename ---
[INFO] Building jar: c:\path\to\gitrepo\service\target\myservicename-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:1.5.9.RELEASE:repackage (default) @ myservicename ---
[INFO]
[INFO] --- jacoco-maven-plugin:0.7.9:report (jacoco-site) @ myservicename ---
[INFO] Loading execution data file c:\path\to\gitrepo\service\target\jacoco.exec
[INFO] Analyzed bundle 'TwelveFactorAnswers' with 16 classes
[INFO]
[INFO] --- maven-failsafe-plugin:2.18:integration-test (default) @ myservicename ---
[INFO] Failsafe report directory: c:\path\to\gitrepo\service\target\failsafe-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
    ||
    ||
    || All the Integration Test runs - sample Integration Test Class - <ServiceName>IT.java
    ||
    ||  
Tests run: 2, Failures: 1, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-failsafe-plugin:2.18:integration-test (integration-tests) @ myservicename ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-failsafe-plugin:2.18:verify (default) @ myservicename ---
[INFO] Failsafe report directory: c:\path\to\gitrepo\service\target\failsafe-reports
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:15 min
[INFO] Finished at: 2018-02-16T14:28:05+05:30
[INFO] Final Memory: 38M/269M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-failsafe-plugin:2.18:verify (default) on project myservicename: There are test failures.
[ERROR]
[ERROR] Please refer to c:\path\to\gitrepo\service\target\failsafe-reports for the individual test results.
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

我想我可能已经找到了上述行为的原因。 Reference

在解释我的发现之前,让我澄清几件事:

  • 我所有的综合测试class都是post-fixed和IT(示例:ServiceIT.java )
  • 所有 Integration Test 相关文件(源和资源)都保存在一个名为 integartion-tests 的文件夹中 src 文件夹(示例:"src/integration-tests/java" & "src/integration-tests/resources"

现在我学到的是:

  1. integration-test阶段包含在install,verify&部署 目标。
  2. 默认情况下,maven 会考虑任何以 -IT 结尾且在 test 内的 class 名称integration-test 文件夹(最终在 test-classes 文件夹中)作为 Integration Test Class。

现在得到我的支持:
如果您仔细查看我粘贴的控制台输出,您会发现 integration-test 被执行了两次 - 一次:

[INFO] --- maven-failsafe-plugin:2.18:integration-test (default) @ myservicename ---
[INFO] Failsafe report directory: c:\path\to\gitrepo\service\target\failsafe-reports

两次:

[INFO] --- maven-failsafe-plugin:2.18:integration-test (integration-tests) @ myservicename ---
[INFO] Tests are skipped.

第一次作为默认阶段执行(作为 install 目标的一部分)。第二次被跳过,因为 dev 配置文件被激活,其中 integration-test 被跳过。

为什么我这么确定?
我将 Integration Test classes 从 ServiceIT.java 更改为 ServiceIntegrationTest.java 并在 pom.xml 中相应地更新了 plugin

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.18</version>
    <executions>
        <!-- Invokes both the integration-test and the verify goals of the Failsafe Maven plugin -->
        <execution>
            <id>integration-tests</id>
            <goals>
                <goal>integration-test</goal>
                <goal>verify</goal>
            </goals>
            <configuration>
                <!-- Skips integration tests if the value of skip.integration.tests property is true -->
                <skipTests>${skip.integration.tests}</skipTests>
                <includes>
                    <include>**/*IntegrationTest.class</include>
                </includes>
            </configuration>
        </execution>
    </executions>
</plugin>

现在,对于 maven,没有 default class 用于集成测试和 integration-tests配置文件未激活。因此,maven 没有找到 运行 的任何默认集成测试。控制台输出如下:

[INFO] --- maven-failsafe-plugin:2.18:integration-test (default) @ twelvefactorquestion ---
[INFO]
[INFO] --- maven-failsafe-plugin:2.18:integration-test (integration-tests) @ twelvefactorquestion ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-failsafe-plugin:2.18:verify (default) @ twelvefactorquestion ---
[INFO] Failsafe report directory: c:\WorkFactor\git\twelveFactorQuestion\target\failsafe-reports
[INFO]
[INFO] --- maven-failsafe-plugin:2.18:verify (integration-tests) @ twelvefactorquestion ---
[INFO] Tests are skipped.