Rohit Agarwal
Rohit Agarwal

Reputation: 420

Maven profiles behaviour with plugins

Let's say I have a multi-module maven project. One of the project's POM file is the following:

<?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>
  <parent>
    <groupId>some.group</groupId>
    <artifactId>parent-artifact</artifactId>
    <version>0.14.0-SNAPSHOT</version>
    <relativePath>../../pom.xml</relativePath>
  </parent>

  <artifactId>artifact-x</artifactId>
  <packaging>jar</packaging>
  <name>Artifact X</name>

  <dependencies>
    <dependency>
      <groupId>some.group</groupId>
      <artifactId>artifact-a</artifactId>
      <version>${project.version}</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>some.group</groupId>
      <artifactId>artifact-b</artifactId>
      <version>${project.version}</version>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>some.group</groupId>
      <artifactId>artifact-c</artifactId>
      <version>${project.version}</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>some.group</groupId>
      <artifactId>artifact-d</artifactId>
      <version>${project.version}</version>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>some.group</groupId>
      <artifactId>artifact-e</artifactId>
      <version>${project.version}</version>
      <scope>runtime</scope>
    </dependency>
  </dependencies>

</project>

Let's say for some reason, I want to move some of the dependencies in this POM file to a profile. So, I move artifact-c, artifact-d, artifact-e to only be dependencies when using profile-1. profile-1 is also used in other POMs in the project. My new POM now becomes:

<?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>
  <parent>
    <groupId>some.group</groupId>
    <artifactId>parent-artifact</artifactId>
    <version>0.14.0-SNAPSHOT</version>
    <relativePath>../../pom.xml</relativePath>
  </parent>

  <artifactId>artifact-x</artifactId>
  <packaging>jar</packaging>
  <name>Artifact X</name>

  <dependencies>
    <dependency>
      <groupId>some.group</groupId>
      <artifactId>artifact-a</artifactId>
      <version>${project.version}</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>some.group</groupId>
      <artifactId>artifact-b</artifactId>
      <version>${project.version}</version>
      <scope>runtime</scope>
    </dependency>
  </dependencies>

  <profiles>
    <profile>
      <id>profile-1</id>
      <dependencies>
        <dependency>
          <groupId>some.group</groupId>
          <artifactId>artifact-c</artifactId>
          <version>${project.version}</version>
          <scope>compile</scope>
        </dependency>
        <dependency>
          <groupId>some.group</groupId>
          <artifactId>artifact-d</artifactId>
          <version>${project.version}</version>
          <scope>runtime</scope>
        </dependency>
        <dependency>
          <groupId>some.group</groupId>
          <artifactId>artifact-e</artifactId>
          <version>${project.version}</version>
          <scope>runtime</scope>
        </dependency>
      </dependencies>
    </profile>
  </profiles>

</project>

When building, I run mvn -Pprofile-1 clean package at the top-level POM in both cases. My expectation was that moving dependencies to a profile shouldn't change anything when -Pprofile-1 was specified. But that didn't happen - some of the plugins started behaving erroneously - for ex. the maven-shade-plugin didn't put artifact-c, artifact-d and artifact-e in the shaded jar etc.

Am I doing something wrong? Are profiles not supposed to work like this?

Are these two POM files not equivalent even when used with -Pprofile-1?

Upvotes: 1

Views: 93

Answers (1)

Robert Scholte
Robert Scholte

Reputation: 12335

Even though the pom.xml allows you to define it like this, you shouldn't (call it a design flaw). Most of the times it doesn't make sense to just add dependencies in a profile. Valid constructions are dependencies based on OS or on JDK version. Also remember that the pom.xml also acts as "consumer-pom", so other projects using this artifact will read this pom file to get the transitive dependencies. In such case you can't active a profile anymore.

In conclusion: don't try to solve this with profiles. There must be a better solution.

Upvotes: 1

Related Questions