maven -P option not working with mvn clean install

3.4k Views Asked by At

When I try to build the project using mvn clean package -P dev, it activates the dev profile and performs the build. But when I try with mvn clean install -P dev, it builds all the profiles. I have more than one profiles in my 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>

Also tried with mvn clean install --activate-profiles=dev, but with same results. Even -D option also did not work.

========================Update=====================================
Please find the complete POM here:

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

And also the console output for 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

There are 1 best solutions below

0
On BEST ANSWER

I think I may have have found the reason for the above behavior. Reference

Before explaining my findings, let me clarify few things:

  • All my Integration Test class are post-fixed with IT (Example: ServiceIT.java)
  • All the Integration Test related files (sources & resources) are kept in a folder called integartion-tests inside src folder (example: "src/integration-tests/java" & "src/integration-tests/resources")

Now what I've learnt is:

  1. integration-test phase is included in install, verify & deploy goals.
  2. By default, maven considers any class name ending with -IT and inside test or integration-test folder (eventually in test-classes folder) as Integration Test Class.

Now in my support:
If you look closely in the console output I've pasted, you'll find that the integration-test is being executed twice - Once:

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

Twice:

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

The first time is being executed as default phase (as a part of install goal). The second time it is being skipped, since the dev profile is being activated, in which the integration-test are skipped.

Why am I so sure?
I changed the Integration Test classes from ServiceIT.java to ServiceIntegrationTest.java and updated the plugin accordingly in 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>

Now, for maven, there is no default class for Integration Test and the integration-tests profile is not activated. Hence, maven did not find any default integration test to run. And the console output came as:

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