1

I wanted to know if it was possible to change the profile used by Maven during its execution.

Let me explain: I configured the exec-maven-plugin to run a script. This script will generate the maven.config file (in the .mvn folder) according to the branch we are on and put the right profile (for example: -Pdevelopment). This properties file will be automatically read by Maven during its execution. The final goal is that the profile "developement" is used when we are on master, "stable" when we are on the stable branch, etc.

The problem is that the file is read directly when the mvn command is launched. Since my script is only executed later (during the "initialize" phase) the profile is non-existent. So I wanted to know if it is possible to run my script before the profile is loaded? A second way would be to have my script automatically restart the mvn command once the file is generated. This way, the right profile would be loaded.

Here is the relevant part of the pom.xml :

<?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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>ch.unifr.se</groupId>
        <artifactId>UnifrBuild</artifactId>
        <version>4.X-SNAPSHOT</version>
    </parent>
    <groupId>ch.unifr.se.app.admintools</groupId>
    <artifactId>admintools-app-build</artifactId>
    <packaging>pom</packaging>
    <version>${revision}</version>
    <name>${project.artifactId}</name>    
     <profiles>
            <profile>
                <id>developement</id>
                <activation>
                  <activeByDefault>true</activeByDefault>
                </activation>
                <properties>
                    <vaadin.productionMode>false</vaadin.productionMode>
                    <UnifrCommonBuild>9.1.X-SNAPSHOT</UnifrCommonBuild>
                    <revision>3.X-SNAPSHOT</revision>
                </properties>
        </profile>
        <profile>
            <id>release</id>
            <properties>
                <vaadin.productionMode>true</vaadin.productionMode>
                <UnifrCommonBuild>9.1.0.1</UnifrCommonBuild>
                <revision>3.X-SNAPSHOT</revision>
            </properties>
        </profile>
        <profile>
            <id>stable</id>
            <properties>
                <vaadin.productionMode>false</vaadin.productionMode>
                <UnifrCommonBuild>9.1.X-PROD-SNAPSHOT</UnifrCommonBuild>
                <revision>3.X-PROD-SNAPSHOT</revision>
            </properties>
        </profile>
        <profile>
            <id>testing</id>
            <properties>
                <vaadin.productionMode>false</vaadin.productionMode>
                <UnifrCommonBuild>9.1.X-SNAPSHOT</UnifrCommonBuild>
                <revision>3.X-SNAPSHOT</revision>
            </properties>
        </profile>
    </profiles>

The goal of the profiles is to set the right version of the artefacts/dependencies depending on the branch. I do that to keep all the versions in one pom. For example if I want to merge stable into master then the value of the version will be overwrite, and I don't want that. The goal is to have something like that : Best practice - Git + Build automation - Keeping configs separate. I already tried that solution but it doesn't work as I expected unfortunately (I've write another question, maybe it can help : How to have config file branch dependant on Git).

Thank you in advance for your help!

  • To be honest that sounds strange. Why do you need the profile ? What does the profile control? Can you give a pom file with the information in it? – khmarbaise Jan 28 '22 at 13:19
  • I edited the question with more informations, I hope it can help you! – Dylan Montandon Jan 28 '22 at 13:41
  • 1
    I would a branch in my versions control to change the version of a dependency and test my code if it works...after this has been done and a release of the used deps is done changing to a release versions and merge back to main... no profiles for dependencies... – khmarbaise Jan 28 '22 at 20:43

1 Answers1

0

No, you cannot change profiles while Maven is running.

You need two separate Maven runs.

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
  • Thanks a lot for your answer! Is it possible to rerun automatically the mvn command if it fails or a mechanism like that? Maybe with the help of the shell script I write? Thank a lot! – Dylan Montandon Jan 28 '22 at 14:12
  • Why rerunning ? If it fails there is a problem which should be fixed upfront. rerunning will not help it fill another time.... – khmarbaise Jan 28 '22 at 20:44