Uploaded image for project: 'Maven'
  1. Maven
  2. MNG-6885

Profile-scope properties

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Open
    • Minor
    • Resolution: Unresolved
    • 3.6.3
    • None
    • Profiles

    Description

      If a property is set in one profile, then another profile cannot change it. It would be good to have profile-scope properties.
      Usage scenario: cross-compilation for different Scala versions (2.11 and 2.12).
      POM file:

      <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.example</groupId>
          <artifactId>scala-example</artifactId>
          <packaging>jar</packaging>
          <version>1.0-SNAPSHOT</version>
      
          <properties>
              <scala.version>UNDEFINED</scala.version>
          </properties>
      
          <profiles>
      
              <profile>
                  <id>scala-common</id>
                  <activation>
                      <activeByDefault>true</activeByDefault>
                  </activation>
                  <build>
                      <plugins>
                          <plugin>
                              <groupId>org.apache.maven.plugins</groupId>
                              <artifactId>maven-antrun-plugin</artifactId>
                              <version>1.8</version>
                              <executions>
                                  <execution>
                                      <id>tasks-scala-${scala.version}</id>
                                      <phase>generate-resources</phase>
                                      <goals>
                                          <goal>run</goal>
                                      </goals>
                                      <configuration>
                                          <target>
                                              <exec executable="echo">
                                                  <arg value="Scala version: ${scala.version}"/>
                                              </exec>
                                          </target>
                                      </configuration>
                                  </execution>
                              </executions>
                          </plugin>
                      </plugins>
                  </build>
              </profile>
      
              <profile>
                  <id>scala-2.11</id>
                  <activation>
                      <activeByDefault>true</activeByDefault>
                  </activation>
                  <properties>
                      <scala.version>2.11</scala.version>
                  </properties>
      
              </profile>
      
              <profile>
                  <id>scala-2.12</id>
                  <activation>
                      <activeByDefault>true</activeByDefault>
                  </activation>
                  <properties>
                      <scala.version>2.12</scala.version>
                  </properties>
              </profile>
      
          </profiles>
      
      </project>
      

      In this POM file we have one common task declaration for both Scala versions: 2.11 and 2.12. Two specific profiles define the 'scala.version' property. The execution id of the plugin includes the property: 'tasks-scala-${scala.version}', hence unique. So theoretically we could build our project in one run for both Scala versions.
      However, in the output of 'mvn package' we see only one plugin execution:

      [INFO] --- maven-antrun-plugin:1.8:run (tasks-scala-2.12) @ profile-example ---
      [INFO] Executing tasks
      
      main:
           [exec] Scala version: 2.12
      

      This is because the property 'scala.version' is globally set to '2.12'. It does not take different values for two different profiles. Therefore we need to run separately:

      mvn -P scala-2.11 goals...
      mvn -P scala-2.12 goals...
      

      This feature could give same user experience as with Sbt cross builds.

      Attachments

        Activity

          People

            Unassigned Unassigned
            tashoyan Arseniy Tashoyan
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated: