In Maven, can a profile override the modules (to not include any)

偶尔善良 提交于 2019-12-19 05:22:23

问题


In maven, once you define your modules in you pom.xml all profiles aggregate the modules defined in them: (relevant part only)

<project>
    <modules>
        <module>module1</module>
    </modules>
    <profiles>
         <profile>
             <id>pr1</id>
             <modules>
                 <moudule>module2</module>
             </modules>

If you perform a mvn clean it will pass the command to module1.

If you issue mvn clean -Ppr1 it will pass along to module1 and module2.

I wonder if in maven 3 it is possible to have a pom.xml with submodules and override this. I mean to execute a profile that instead of add their own modules to the build force those like:

<project>
    <!-- omitted -->
    <modules>
        <!-- modules -->
    </modules>
    <build>
        <!-- build -->
    </build>
    <profiles>
        <profile>
             <!-- This profile with no modules -->
        </profile>
    </profiles>
</project>

The requirement might sound silly, but I just want to know if there is a mechanism like in plugin configuration.

<configuration self.combine="override"

Regards!

ssedano


回答1:


It's not possible. Profile configuration is always merged with main configuration, so you only can add modules in your case.

What you can do instead, however, is to create a some kind of "default" profile (by <activeByDefault>true</activeByDefault> in <activation> section) that is enabled when no other profiles are invoked and put your default modules' list there. Then, when no profile is specified on Maven build call, this "default" profile is used, but when you call explicitly at least one profile, it's not, so you can this way define modules' list from scratch.




回答2:


While the question is old, Google still ranks it highly, so it makes sense to add a new answer.

You can use the activation by absence of a property trick to achieve what you want.

<profiles>
        <!-- By default, include the modules -->
        <profile>
            <id>full-build</id>
            <activation>
                <!-- Activation by absence of a property. Run normally, without -Dfull-build -->
                <property>
                    <name>!skip-modules</name>
                </property>
            </activation>
            <modules>
                <module>module1</module>
                <module>module2</module>
                <module>module3</module>
            </modules>
        </profile>

        <!-- No-modules build -->
        <profile>
            <id>no-modules</id>
            <activation>
                <!-- Activation by a property. Run with -Dskip-modules to activate -->
                <property>
                    <name>skip-modules</name>
                </property>
            </activation>
            <modules>
            </modules>
        </profile>
    </profiles>



回答3:


You can do things like this:

  <profiles>
    <profile>
      <id>run-xyz</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <modules>
        <module>www-ab</module>
        <module>www-cd</module>
      </modules>
    </profile>

    <profile>
      <id>run-its</id>
      <activation>
        <activeByDefault>false</activeByDefault>
      </activation>
      <modules>
        <module>www-db</module>
        <module>www-it</module>
      </modules>
    </profile>
  </profiles>

If you are talking about such things. But i would recommend to be very carefull about such things.

It's only the question how you set activeByDefault. With this it's possible to create more or less any combination.



来源:https://stackoverflow.com/questions/10682186/in-maven-can-a-profile-override-the-modules-to-not-include-any

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!