Maven profile for single module

前端 未结 2 1721
离开以前
离开以前 2021-02-15 22:18

I have a multi module maven project, which builds successfully, I\'d like to build just one of the modules I have. How would I do that with profiles ? I could d

相关标签:
2条回答
  • 2021-02-15 22:44

    To implement this with profiles, you could use two profiles, one <activeByDefault> with all modules and another one with the wanted module only. Something like this:

    <profiles>
      <profile>
        <id>all</id>
        <activation>
          <activeByDefault>true</activeByDefault>
        </activation>
        <modules>
          <module>module-1</module>
          ...
          <module>module-n</module>
        </modules>
      </profile>
      <profile>
        <id>module-2</id>
        <modules>
          <module>module-2</module>
        </modules>
      </profile>
    <profiles>
    

    And then call it like this:

    mvn -Pmodule-2 package
    

    Two things to note here:

    1. You need to move the <modules> from the POM in a "default" profile (because <modules> from a profile are only additive, they do not hide the modules declared in the POM).
    2. By marking it as <activeByDefault>, the "default" profile will be picked if nothing else is active but deactivated if something else is.

    One could even parametrize the name of the module and pass it as property:

    <profiles>
      ...
      <profile>
        <id>module-x</id>
        <activation>
          <property>
            <name>module-name</name>
          </property>
        </activation>
        <modules>
          <module>${module-name}</module>
        </modules>
      </profile>
    <profiles>
    

    And invoke maven like this:

    mvn -Dmodule-name=module-2 package
    

    But this is a poor implementation IMHO, I prefer the -pl "advanced" reactor options (less xml, much more power and flexibility):

    mvn -pl module-2 package
    
    0 讨论(0)
  • 2021-02-15 22:59

    To overcome additivity nature of maven default <modules> working with <profiles>, you can use reactor with particular profile, e.g.:

    mvn -pl module-2 -Pprofile-name package
    

    This will package module-2 defined in profile-name and not in default profile.

    0 讨论(0)
提交回复
热议问题