title: Does Maven profile activation by property not work for dependent projects?
body: <p>I have a requirement to support multiple types of client builds using the same parent pom file. The challenge for me is that different clients will use slightly different libraries. For example, one client may use SQL server and another may use MySQL. This is a simple example, but other libraries may include those which may require a license that we wouldn't have internally, but our client would have. Not excluding them internally would cause mavens to bit build anymore as the libraries wouldn't be present in our repository. My initial idea was to create a profile in our parent pom for each client and then define any dependencies that are used only for that client within those profiles. These profiles would be activated by either the absence of a property or by a property having a certain value. I'm having a problem with the way that the dependencies are being resolved when multiple modules are involved, specifically when one child is the dependent of another. I believe the crux of the issue revolves around the variable that triggers the profile not being set at some point during the resolution process, but allow me to example with a simple example:</p>
  
  <p><strong>Parent pom:</strong></p>
  
  <pre><code>&lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    &lt;groupId&gt;com.test&lt;/groupId&gt;
    &lt;artifactId&gt;parent&lt;/artifactId&gt;
    &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
    &lt;packaging&gt;pom&lt;/packaging&gt;
  
    &lt;modules&gt;
      &lt;module&gt;../child-a&lt;/module&gt;
      &lt;module&gt;../child-b&lt;/module&gt;
    &lt;/modules&gt;
  
  &lt;profiles&gt;
      &lt;profile&gt;
          &lt;id&gt;client-a&lt;/id&gt;
          &lt;activation&gt;
              &lt;property&gt;
                  &lt;name&gt;!clientbuild&lt;/name&gt;
              &lt;/property&gt;
          &lt;/activation&gt;
          &lt;dependencies&gt;
              &lt;dependency&gt;
                  &lt;groupId&gt;com.microsoft.sqlserver&lt;/groupId&gt;
                  &lt;artifactId&gt;sqljdbc4&lt;/artifactId&gt;
                  &lt;version&gt;4.0&lt;/version&gt;
              &lt;/dependency&gt;
          &lt;/dependencies&gt;
      &lt;/profile&gt;
      &lt;profile&gt;
          &lt;id&gt;client-b&lt;/id&gt;
          &lt;activation&gt;
              &lt;property&gt;
                  &lt;name&gt;clientbuild&lt;/name&gt;
                  &lt;value&gt;clientb&lt;/value&gt;
              &lt;/property&gt;
          &lt;/activation&gt;
          &lt;dependencies&gt;
              &lt;dependency&gt;
                  &lt;groupId&gt;mysql&lt;/groupId&gt;
                  &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
                  &lt;version&gt;5.1.34&lt;/version&gt;
              &lt;/dependency&gt;
          &lt;/dependencies&gt;
      &lt;/profile&gt;
    &lt;/profiles&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>I have defined client-a and client-b profiles within my parent pom. The client-a profile should be activated in the absence of the "clientbuild" property and the "client-b" profile should be activated when the "clientbuild" property is set to "clientb". If I run dependency:tree on parent and specify "clientb" in my maven user "setting.xml" file, I get the following:</p>
  
  <pre><code>--- maven-dependency-plugin:2.8:tree (default-cli) @ parent ---
  com.test:parent:pom:0.0.1-SNAPSHOT
  \- mysql:mysql-connector-java:jar:5.1.34:compile
  
  --- maven-dependency-plugin:2.8:tree (default-cli) @ child-a ---
  com.test:child-a:jar:0.0.1-SNAPSHOT
  \- mysql:mysql-connector-java:jar:5.1.34:compile
  
  --- maven-dependency-plugin:2.8:tree (default-cli) @ child-b ---
  com.test:child-b:jar:0.0.1-SNAPSHOT
  +- com.test:child-a:jar:0.0.1-SNAPSHOT:compile
  |  \- com.microsoft.sqlserver:sqljdbc4:jar:4.0:compile
  \- mysql:mysql-connector-java:jar:5.1.34:compile
  </code></pre>
  
  <p>"parent" and "child-a" projects have the correct dependencies, but "child-b" has the dependencies from both profiles. From the tree I can see that the incorrect dependency is being included because maven believes "child-a" has a dependency of sqljdbc. My belief is that while building, maven is picking up "child-a" and resolving it's dependencies from the parent pom without using the "clientbuild" property that I set in my maven "settings.xml" file. Is there any way to get maven to use the "clientbuild" property during the entirety of the dependency resolution process?</p>
  
  <p>Through various experiments, I have determined that if I was to remove the "!clientbuild" and replace it with a check against a certain value (and then specify that value within my settings.xml), or if I have no activation requirements and manually trigger the profiles with -P I get the desired results. Unfortunately, we cannot mandate that our first client use -P or specify any other arguments during the build process, so using the absence of the "clientbuild" property is the only activation check I can use at this time for "client-a". Additionally, if I replace this check with "activeByDefault", I get the same behavior as the above when adding "clientb" as a property to my "settings.xml" file.</p>
  
  <p>So, given all of this my question is: Is I'm trying to do is even possible? I know my requirements are quite unusual. Ideally I'd like the profile to always be specified during build time, but unfortunately this is not possible for our client.</p>
  
  <p>In a nutshell, I need to activate a profile by default which contains certain dependencies, but these dependencies will be replaced by another set of dependencies from another profile if activated by a property. Additionally, this needs to work when some modules belonging to the same parent depend on each other as shown above. Anyone have experience with this or have any alternative methods that I can try?</p>
  
  <p>As a final footnote, I'm using Eclipse (specifically Spring Tool Suite) and the embedded Maven 3 that comes with it.</p>
  
  <p><strong>Edit (child POMs and settings.xml):</strong>
  As requested, here are the POMs for child-a and child-b. I've also included my user settings.xml file that I have for when I'm trying to apply the "clientbuild" property as "clientb".</p>
  
  <p><strong>child-a POM:</strong></p>
  
  <pre><code>&lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    &lt;parent&gt;
      &lt;groupId&gt;com.test&lt;/groupId&gt;
      &lt;artifactId&gt;parent&lt;/artifactId&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
    &lt;/parent&gt;
    &lt;artifactId&gt;child-a&lt;/artifactId&gt;
    &lt;packaging&gt;jar&lt;/packaging&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p><strong>child-b POM:</strong></p>
  
  <pre><code>&lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    &lt;parent&gt;
      &lt;groupId&gt;com.test&lt;/groupId&gt;
      &lt;artifactId&gt;parent&lt;/artifactId&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
    &lt;/parent&gt;
    &lt;artifactId&gt;child-b&lt;/artifactId&gt;
    &lt;packaging&gt;jar&lt;/packaging&gt;
  
    &lt;dependencies&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;com.test&lt;/groupId&gt;
          &lt;artifactId&gt;child-a&lt;/artifactId&gt;
          &lt;version&gt;${project.version}&lt;/version&gt;
      &lt;/dependency&gt;
    &lt;/dependencies&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p><strong>settings.xml when clientb "clientbuild" property is active:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" 
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
            xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"&gt;
  
    &lt;pluginGroups&gt;
    &lt;/pluginGroups&gt;
  
    &lt;proxies&gt;
    &lt;/proxies&gt;
  
    &lt;servers&gt;
    &lt;/servers&gt;
  
    &lt;mirrors&gt;
    &lt;/mirrors&gt;
  
    &lt;profiles&gt;
     &lt;profile&gt;
      &lt;id&gt;client-profile&lt;/id&gt;
      &lt;activation&gt;
          &lt;activeByDefault&gt;true&lt;/activeByDefault&gt;
      &lt;/activation&gt;
      &lt;properties&gt;
          &lt;clientbuild&gt;clientb&lt;/clientbuild&gt;
      &lt;/properties&gt;
     &lt;/profile&gt;
    &lt;/profiles&gt;
  &lt;/settings&gt;
  </code></pre>
  
  <p>I've also tried setting the  tag to activate the "client-build" profile within my settings.xml instead of using the activeByDefault tag, but this yielded the same results.</p>
  
  <p><strong>Edit 2 (clarification on relationship and using JVM parameter):</strong> To clarify, client-a/client-b profiles have no effect on the relationship between child-a/child-b modules. The relationship, regardless of profile, is that child-b depends on child-a. The only thing that the client-a/client-b profile affects is whether sqljdbc or mysql-connector-java is added as a dependency to parent.</p>
  
  <p>As for the clientbuild variable being used at the command line. Passing -Dclientbuild=clientb will result in the desired dependency tree.</p>
  
  <pre><code>--- maven-dependency-plugin:2.8:tree (default-cli) @ parent ---
  com.test:parent:pom:0.0.1-SNAPSHOT
  \- mysql:mysql-connector-java:jar:5.1.34:compile
  
  --- maven-dependency-plugin:2.8:tree (default-cli) @ child-a ---
  com.test:child-a:jar:0.0.1-SNAPSHOT
  \- mysql:mysql-connector-java:jar:5.1.34:compile
  
  --- maven-dependency-plugin:2.8:tree (default-cli) @ child-b ---
  com.test:child-b:jar:0.0.1-SNAPSHOT
  +- com.test:child-a:jar:0.0.1-SNAPSHOT:compile
  \- mysql:mysql-connector-java:jar:5.1.34:compile
  </code></pre>
  
  <p>However, the reason why I want/need to use settings.xml instead is because using the command line argument, while fixing a manual maven build, does not fix the fact that Eclipse's embedded Maven will still resolve the dependencies as I've shown above in the initial dependency:tree output (child-b will have the dependencies of both profiles instead of just the client-b profile). This in itself could be acceptable (even though the behavior isn't as expected), however we do not have some of the dependencies within the profile being activated that shouldn't be (from the client-a profile) due to licensing and other reasons. If the dependencies of both profiles are present it will result in many missing artifact errors in Eclipse.</p>