maven -P选项不能与mvn clean install

时间:2018-02-14 08:26:11

标签: maven maven-3 maven-profiles maven-install-plugin maven-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

1 个答案:

答案 0 :(得分:0)

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

在解释我的发现之前,让我澄清一些事情:

  • 所有集成测试类都使用 IT 进行了后期修复(例如:ServiceIT.java)
  • 所有 Integration Test 相关文件(来源和资源)都保存在 src 文件夹中名为 integartion-tests 的文件夹中(例如:&#34; src / integration-tests / java&#34; &amp; &#34; src / integration-tests / resources&#34;

现在我所学到的是:

  1. 集成测试阶段包含在安装验证&amp; 部署目标。
  2. 默认情况下,maven会考虑任何以 -IT 结尾的类名称以及 test integration-test 文件夹(最终在中)测试类文件夹)为集成测试类。
  3. 现在在我的支持下:
    如果仔细查看我已粘贴的控制台输出,您会发现集成测试正在执行两次 - 一旦:

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

    第一次作为默认阶段执行(作为安装目标的一部分)。第二次被跳过,因为 dev 配置文件被激活,其中跳过集成测试

    为什么我这么肯定?
    我将集成测试类从服务 IT .java更改为服务 IntegrationTest .java,并在中相应地更新了plugin的pom.xml

    <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,Integration Test没有默认类,并且未激活集成测试配置文件。因此,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:\Work\12Factor\git\twelveFactorQuestion\target\failsafe-reports
    [INFO]
    [INFO] --- maven-failsafe-plugin:2.18:verify (integration-tests) @ twelvefactorquestion ---
    [INFO] Tests are skipped.