Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven Modules optional in pom.xml

Tags:

java

maven

Hi I've parent pom.xml like below.. , say I've 4 modules currently.. But at certain times I might not have all the 4 modules all the time.. Is there any way to make these module(s) (Child projects) optional within root pom.xml. Which means that child project will not be present in the one branch , but will be present in another branch .. I don't want to use multiple root pom.xml's for different branches.. Is it possible?

 <project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.xx.xx.correspondence</groupId>
  <artifactId>xxHudsonTP</artifactId>
  <version>1</version>
  <packaging>pom</packaging>

  <modules>
    <module>xxCastor</module>
    <module>cxxYYYCastor</module>
    <module>xxCommon</module>
    <module>xxxx</module>    
  </modules>
</project> 
like image 755
srinannapa Avatar asked Dec 15 '10 09:12

srinannapa


People also ask

What is optional in POM xml?

Optional dependencies are used when it's not possible (for whatever reason) to split a project into sub-modules. The idea is that some of the dependencies are only used for certain features in the project and will not be needed if that feature isn't used.

How do you exclude a dependency from POM?

Open the dependency POM and find the transitive dependency you want to exclude. Copy groupId and artifactId . In your project POM, underneath your active dependency, enter exclusions and using code completion paste the copied info of the dependency you want to exclude.

Which tag is used to avoid an optional dependency in Maven?

In order to exclude these special dependencies from the main project, we can apply Maven's <optional> tag to them. This forces any user who wants to use those dependencies to declare them explicitly. However, it does not force those dependencies into a project that doesn't need them.


1 Answers

You can use profiles, like they did it for example in flex-mojos plugin project:

...
<profiles>

    <profile>
      <id>minimal</id>
      <modules>
        <module>flexmojos-parent</module>
        <module>flexmojos-sandbox</module>
        <module>flexmojos-generator</module>
        <module>flexmojos-maven-plugin</module>
        <module>flexmojos-super-poms</module>        
        <module>flexmojos-testing</module>        
      </modules>
    </profile>

    <profile>
      <id>release</id>
      <modules>
        <module>flexmojos-parent</module>
        <module>flexmojos-sandbox</module>
        <module>flexmojos-generator</module>
        <module>flexmojos-maven-plugin</module>
        <module>flexmojos-super-poms</module>
        <module>flexmojos-archetypes</module>
        <module>flexmojos-testing</module>
      </modules>
    </profile>

<profiles>
like image 53
Ralph Avatar answered Sep 22 '22 21:09

Ralph