Когда я пытаюсь построить проект, используя 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
0
indranil9286 14 Фев 2018 в 11:26

1 ответ

Лучший ответ

Думаю, я нашел причину такого поведения. Справка

Прежде чем объяснять свои выводы, позвольте мне прояснить несколько вещей:

  • Все мои классы Integration Test пост-исправлены с помощью IT (пример: ServiceIT.java)
  • Все файлы, связанные с Integration Test (источники и ресурсы), хранятся в папке integartion-tests внутри папки src (пример: "src / integration-tests / java" и "src / integration-tests / resources" )

Теперь я узнал следующее:

  1. Этап integration-test включен в цели установки , проверки и развертывания .
  2. По умолчанию maven рассматривает любое имя класса, заканчивающееся на -IT и находящееся внутри папки test или integration-test (в конечном итоге в test-classes ) как класс Integration Test .

Теперь в моей поддержке:
Если вы внимательно посмотрите на вывод консоли, который я вставил, вы обнаружите, что 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.

Первый раз выполняется как этап по умолчанию (как часть цели установки ). Во второй раз он пропускается, так как активируется профиль dev , в котором пропускаются integration-test .

Почему я так уверен?
Я изменил классы Integration Test с Service IT .java на Service 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 нет класса default для Integration Test, а профиль 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:\Work\12Factor\git\twelveFactorQuestion\target\failsafe-reports
[INFO]
[INFO] --- maven-failsafe-plugin:2.18:verify (integration-tests) @ twelvefactorquestion ---
[INFO] Tests are skipped.
0
indranil9286 16 Фев 2018 в 15:27