简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM