简体   繁体   English

从项目中更改 maven 配置文件

[英]Change maven profile from within project

I want to change maven project's active profile from within the maven project.我想从 maven 项目中更改 maven 项目的活动配置文件。

The java project has 3 classes having main methods and they are executed one by one, All of these require different set of dependencies. java 项目有 3 个具有主要方法的类,它们一个一个地执行,所有这些都需要不同的依赖集。

I would like to run these classes with different maven profiles.我想用不同的 maven 配置文件运行这些类。

For example my project project1 is a maven project and is getting invoked by mvn clean package spring-boot:run command.例如,我的项目 project1 是 maven 项目,并被 mvn clean package spring-boot:run 命令调用。

When the control comes to the project1 it executes the java classes one by one.当控件到达 project1 时,它会一个一个地执行 java 类。

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).我的要求是,当 ClassA 运行时,它应该将当前的 maven 配置文件更改为 profile1(重新编译同一个项目是可以的,性能不是优先级)。 Similarly when ClassB gets invoked it should change the current maven profile to Profile2.同样,当 ClassB 被调用时,它应该将当前的 maven 配置文件更改为 Profile2。 (Note these classes are dynamically picked from directory using reflection and only the class will know what profile it supports) (注意这些类是使用反射从目录中动态选择的,只有 class 会知道它支持什么配置文件)

Is there any way maven allows this at runtime.有什么办法 maven 在运行时允许这样做。 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: maven 配置文件不是为此类任务设计的,您实际需要做的是为每个 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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