betawave92
betawave92

Reputation: 113

Specify profile for dependency with 'provided' scope in Maven

I am developing a multi-module build that requires one of the artifacts produced by another module in the build as a dependency when I run integration tests for the build. I am using the 'provided' scope so it is not included in the artifact produced for the module we are building for, but to insure that it is compiled so we can pick it up for our tests. However, the component we are listing as a dependency has its own set of integration tests that it runs. I would like to be able to specify in the definition of the dependency the profile Maven should use when resolving the dependency (e.g., package, and not verify). Is there a way to specify in the definition of the dependency which Maven phase / profile we want to resolve the dependency with?

The current setup:

 <dependency>
        <groupId>groupId</groupId>
        <artifactId>artifactId</artifactId>
        <version>version</version>
        <scope>provided</scope>
 </dependency>

The desired setup:

 <dependency>
        <groupId>groupId</groupId>
        <artifactId>artifactId</artifactId>
        <version>version</version>
        <scope>provided</scope>
        <!-- only go up until package phase for dependency artifact --> 
        <phase>package</phase>
        <!-- use dependency's skip-tests profile to compile it -->
        <profile>skip-tests</profile>
 </dependency>

Upvotes: 1

Views: 1676

Answers (1)

Coralie B
Coralie B

Reputation: 603

Let's say that B is the dependency, A the project you want to build with full tests, and C the parent of A and B.

A depends on B, with this inside its pom:

<dependency>
        <groupId>BgroupId</groupId>
        <artifactId>BartifactId</artifactId>
        <version>Bversion</version>
        <scope>provided</scope>
 </dependency>

B has a no-test profile, not active by default. It is defined at the end of its pom:

<profiles>
    <profile>
      <id>no-test</id>
      <active>false</active>
      <build>
       <plugins>
        <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-surefire-plugin</artifactId>
         <configuration>
           <skipTests>true</skipTests>
         </configuration>
        </plugin>
      </plugins>
     </build>        
    </profile>
</profiles>

Then you can build C with the no-test profile. You must execute the following command at the root of C project:

mvn clean install -Pno-test

As a result, C will build B first (because A needs B) with the no-test profile. So B will be built with the "skip-tests" property.

Then C will build A. As no specific behaviour is defined in the pom of A project for the "no-test" profile, A will be build with full tests.

Upvotes: 3

Related Questions