How to ignore maven profiles in child module?

后端 未结 1 540
孤独总比滥情好
孤独总比滥情好 2021-01-25 05:43

I want to run the simple flow, I have 6 profiles:

generate-schema,unpack-war,run-jetty,test,stop-jetty,start-stop-app

the test profile will run

1条回答
  •  伪装坚强ぢ
    2021-01-25 06:02

    Please let me clarify few important points which could also potentially solve your issue since I see some good practices may be missing in your approach:

    • Profiles should be used for optional/additional behaviors, while default build should always be SUCCESSFUL regardless of the declared profiles
    • If a module is supposed to execute tests and tests are only located in that module, then the profile should only be declared in that specific module and not in its parent. Moreover, in such a case a profile would also not be required since it would be the default module behavior. However, having a profile defined only in a module would allow you to switch it off even when executing the whole build from the parent. To switch off a profile simply use the ! or the - annotation: mvn clean install -P!profile-name as explained in the official documentation here. That will allow to ignore maven profiles in child module (to come to your questions) if the profile was only declared in that module.

    You can apply the same concept to the generate-schema action: is it required in all modules? Probably not, then you can declare the profile only on the specific module requiring it and then switch it on/off as above (if by default should be on, then declare the profile as active by default).

    Concerning jetty, integration tests requiring jetty should also be part of the same module, so that the module's build would follow the classic flow: start/test/stop. If you really need to have start, test and stop in different modules, then you can follow explanation on our previous question, here.

    I would also suggest to go through the Profile Pitfalls of the Maven official documentation, again, here for a better understanding of profile usage and misuses.

    Additionally, if of any help, you can even skip an entire module build as part of a multimodule maven build, using the command line -pl option. Check mvn -help for further details. As such you could supply the list of modules to build. Since maven 3.2.1 you can also supply the list of modules to skip as following: mvn -pl !module-to-skip install.

    When using the ! notation for skipping profiles or modules, beware that it is also a special character for bash in linux, hence put it between single quotes (mvn -pl'!module-to-skip' install). The same doesn't work on Windows however, where double quotes should be used instead (mvn -pl "!module-to-skip" install`).

    Additionally, to skip modules as part of the parent build, you could also define a profile in your parent POM which re-declares the modules section and omit the modules you want to skip, as explained in this other answer.

        ...
       
          module1
          module2  
          ...
      
      ...
      
         
           skip-some-modules-profile
              
                module1
                ...
                module-integration-test
               
          
      
     ...
    

    As such, you could craft a special multimodule build depending on the profile you want to execute.

    All in all, beware that playing too much with profiles and modules which don't deliver any real artefact may affect your build readability and maintenance.

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