Sydney
Sydney

Reputation: 12222

maven profile activation based on existence of several files

I would like to activate a profile based on the existence of several files. In the following example I want the profile to be activated if both files my.marker and another.marker exists.

    <activation>
        <file>
            <exists>${basedir}/my.marker</exists>
            <exists>${basedir}/another.marker</exists>
        </file>
    </activation>

It does not work since it's not valid against the schema. Is there a way to do such a thing without using command line properties?

Upvotes: 6

Views: 4662

Answers (3)

Konrad Windszus
Konrad Windszus

Reputation: 261

Actually even with MNG-4565 being solved in Maven 3.2.2 it is still not possible to check for the existence of several files. That is because the POM model doesn't allow multiple file nor multiple exists elements (https://issues.apache.org/jira/browse/MNG-5909). Even mixing exists and missing in one file element does not work (because if the exists element is there, it will be considered only and the sibling missing element will be disregarded, https://issues.apache.org/jira/browse/MNG-5910)

Upvotes: 7

Michał Kalinowski
Michał Kalinowski

Reputation: 18013

Unfortunately you can't do this for now. There is a ticket that could solve your case by using multiple activation conditions, but it is still unresolved.

EDIT: There was an issue with Maven, so you could not do that. However now, starting with 3.2.2 version, it should work by using multiple activation conditions.

Thanks to Nicholas Daley for pointing this out.

Upvotes: 0

Raghuram
Raghuram

Reputation: 52665

While not a solution to your problem you could ensure that a profile is run successfully only if two files exist by using maven enforcer plugin.

For instance, the snippet below will fail for any maven goal run with profile requiresTwoFiles (e.g. mvn -P requiresTwoFiles compile) unless both src/main/java and src/test/java exist.

...
<profile>
    <id>requiresTwoFiles</id>           
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-enforcer-plugin</artifactId>
                <version>1.1</version>
                <executions>
                      <execution>
                        <id>enforce-files-exist</id>
                        <goals>
                          <goal>enforce</goal>
                        </goals>
                        <configuration>
                          <rules>
                            <requireFilesExist>
                              <files>
                                  <file>${basedir}/src/main/java</file>
                                  <file>${basedir}/src/test/java</file>
                              </files>
                            </requireFilesExist>
                          </rules>                              
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</profile>
...

Upvotes: -1

Related Questions