Amit Mahajan
Amit Mahajan

Reputation: 915

Change maven profile from within project

I want to change maven project's active profile from within the maven project.

The java project has 3 classes having main methods and they are executed one by one, All of these require different set of dependencies.

I would like to run these classes with different maven profiles.

For example my project project1 is a maven project and is getting invoked by mvn clean package spring-boot:run command.

When the control comes to the project1 it executes the java classes one by one.

My requirement is that when ClassA is run it should change the current maven profile to profile1 (recompiling the same project is ok, performance is not priority). Similarly when ClassB gets invoked it should change the current maven profile to Profile2. (Note these classes are dynamically picked from directory using reflection and only the class will know what profile it supports)

Is there any way maven allows this at runtime. If not what is the best way to achieve this.

Upvotes: 0

Views: 63

Answers (1)

Andrey B. Panfilov
Andrey B. Panfilov

Reputation: 6063

maven profiles are not designed for such tasks, what you are actually need to do is to setup different executions for each class:

...
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
    <execution>
        <id>run-clsa</id>
        <goals>
            <goal>run</goal>
        </goals>
        <phase>none</phase>
        <configuration>
            <mainClass>ClassA</mainClass>
        </configuration>
    </execution>
    <execution>
        <id>run-clsb</id>
        <goals>
            <goal>run</goal>
        </goals>
        <phase>none</phase>
        <configuration>
            <mainClass>ClassB</mainClass>
        </configuration>
    </execution>
</executions>
...

and run that in following way:

mvn clean package spring-boot:run@run-clsa
mvn clean package spring-boot:run@run-clsb

Upvotes: 1

Related Questions