You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@maven.apache.org by Danny Shemesh <da...@microsoft.com.INVALID> on 2019/11/27 19:15:11 UTC

Profile file activation in a reactor pom, using paren't relative dir - odd behavior

Hey everyone,

I’m trying to activate a profile in a multi-module project, with several reactor poms, based on a file
existing (or missing) from the parent directory of the root ‘grand’ parent pom.

Here’s a basic example showing what I didn’t accomplish to achieve:

Dir tree:
.
|   activate_me
|
\---parent
    |   pom.xml
    |
    \---child
            pom.xml

Parent pom:
<?xml version='1.0' encoding='UTF-8'?>
<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">
    <modelVersion>4.0.0</modelVersion>

    <properties>
        <project.parent.relativePath>.</project.parent.relativePath>
        <!— If we don’t declare the above as a prop, the interpolation fails for the parent pom -->
        <activation.file.path>${basedir}/${project.parent.relativePath}/../activate_me</activation.file.path>
        <activated>0</activated>
    </properties>

    <packaging>pom</packaging>
    <groupId>com.fileactivation</groupId>
    <artifactId>parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <modules>
        <module>child</module>
    </modules>

    <profiles>
        <profile>
            <id>test-file-activation</id>
            <activation>
                <file>
                    <exists>${activation.file.path}</exists>
                </file>
            </activation>
            <properties>
                <!—I’m trying alter this flag, based on the existence of activation.file.path -->
                <activated>1</activated>
            </properties>
       </profile>
    </profiles>
</project>

Child pom:

<?xml version='1.0' encoding='UTF-8'?>
<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">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.fileactivation</groupId>
        <artifactId>parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>..</relativePath>
    </parent>

    <packaging>pom</packaging>
    <groupId>com.fileactivation</groupId>
    <artifactId>child</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</project>


Running mvn help:active-profiles, I get:

Active Profiles for Project 'com.fileactivation:parent:pom:0.0.1-SNAPSHOT':
The following profiles are active:
- test-file-activation (source: com.fileactivation:parent:0.0.1-SNAPSHOT)

Active Profiles for Project 'com.fileactivation:child:pom:0.0.1-SNAPSHOT':
The following profiles are active:
<empty>


However, running mvn help:effective-pom, I get:
  <!-- Effective POM for project                                              -->
  <!-- 'com.fileactivation:parent:pom:0.0.1-SNAPSHOT'                         -->
  <!--                                                                        -->
….
    <properties>
      <activated>1</activated>
      <activation.file.path>c:\Work\file-activation\parent/./../activate_me</activation.file.path>
…
  <!-- Effective POM for project                                              -->
  <!-- 'com.fileactivation:child:pom:0.0.1-SNAPSHOT'                          -->
  <!--                                                                        -->
    <properties>
      <activated>0</activated> <-- The file path below is correct, I expected it would be activated here -->
      <activation.file.path>C:\Work\file-activation\parent\child/../../activate_me</activation.file.path>


It seems like the activation file path is expanded correctly in the child module, however, it does not
activate the profile as I would’ve expected;
Sadly, using session.executionRootDirectory, or maven.multiModuleProjectDirectory wouldn’t meet all
Our requirements, as we have several parent poms, and we don’t always run maven from the ‘grand’ pom directory.

The above was tested using maven 3.6.3 on windows.

Would love to have any pointers on why this doesn’t work, and how can I achieve such a behavior irregardless.

Thanks a ton,
Danny

Re: Profile file activation in a reactor pom, using paren't relative dir - odd behavior

Posted by Jörg Schaible <jo...@gmx.de>.
Hi,

Am Mittwoch, 27. November 2019, 20:15:11 CET schrieb Danny Shemesh:
> Hey everyone,
> 
> I’m trying to activate a profile in a multi-module project, with several
> reactor poms, based on a file existing (or missing) from the parent
> directory of the root ‘grand’ parent pom.

[snip]

> It seems like the activation file path is expanded correctly in the child
> module, however, it does not activate the profile as I would’ve expected;
> Sadly, using session.executionRootDirectory, or
> maven.multiModuleProjectDirectory wouldn’t meet all Our requirements, as we
> have several parent poms, and we don’t always run maven from the ‘grand’
> pom directory.
> 
> The above was tested using maven 3.6.3 on windows.
> 
> Would love to have any pointers on why this doesn’t work, and how can I
> achieve such a behavior irregardless.

The first thing Maven does, is generating the effective POM. It will first select 
all active profiles (from the complete hierarchy) and then interpolate all the 
properties. The assumption is simply wrong that a parent POM is completely 
resolved first (incl. properties). You cannot activate a profile in a child POM 
based on the value set in a parent POM. Especially in the activation part of a 
profile there is no property interpolation (or very limited in the meanwhile).

What you can do though: Declare a profile in a parent based on the existence of 
a file or directory with a relative path. If it is present in the context of a 
child project, the profile gets activated. That's how profiles can be predefined 
in a parent and selectively activated. We use typically a directory profiles 
with empty files in each child that should use some special, but generic build.

Cheers,
Jörg



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org