How to activate profile based on goal being execut

2019-02-17 13:18发布

When I execute certain goals from the command line I would like to activate a profile 'automatically'.

E.g. what I'm doing now:

mvn appengine:devserver -Pdevelopment

mvn appengine:update -Pproduction

Basically I want to activate the development profile automatically as I run the devserver goal. The same for the production profile which I want to activate when I run the update goal (unless explicitly overridden using -P option).

Is this possible?

2条回答
我想做一个坏孩纸
2楼-- · 2019-02-17 13:34

The Maven Model told us as the profile consists of various elements, including with build. as the following example: -

<profile>
    <id></id>
    ...
    <build>
        <plugins>
           <plugin></plugin>
           ...
        </plugins>
    </build>
</profile>

As we have seen above, the profile controls the plugin. Then the answer to your question is no. We cannot let the plugin to activate the profile.

I hope this may help.

查看更多
孤傲高冷的网名
3楼-- · 2019-02-17 13:57

I agree with the previous conclusion that you cannot do this. The reasoning I saw before may be slightly backward.

The plugin is activated by defining the goal on the command-line (in the specific case of this question), not by the plugin being in the profile.

It is probably plugin default configuration (plugin.configuration) that you're trying to override this way, rather than the configuration for a specific execution (plugin.executions.execution.configuration). And that fine by me. But the pickle is to get the profile activated.

If all you're having on your command-line is the goal (e.g. appengine:update) there's no way that's gonna activate a profile.

When you read discussions about profile activation (as I stubbornly did a lot in the past) you'll find that there is a lot still desired by many people in terms of profile activation. Activating profiles on basis of what goals are executed is not even a desire that I yet came across — just to say: don't get your hopes up.

You may also wonder whether what you're trying to do is really appropriate. Do you really need to run goals, or could you somehow still benefit from the Maven lifecycle?


The Maven lifecycle is really a (straight) path to completion, traversing through phases. If you want to divert this path based on certain conditions, profiles are the way to go. If appengine:update and appengine:devserver are "either/or" (which I believe they are) you're diverting the path, should use profiles, really, and will still end up with having at least two things on your command-line, i.e. the phase and the desired profile. If there's one typical case and one exceptional case you could express that by default profile activation (activeByDefault) and explicitly choosing another profile (-P) respectively:

mvn deploy // choose default profiles
mvn deploy -Pproduction // ignore default profiles

If you want to do both (e.g. devserver during integration test phase and update during deploy) the lifecycle can be configured to do so through plugin executions that are tied to a phase subsequently. You will simply have one thing on your command-line, i.e. the phase.

查看更多
登录 后发表回答