External Profiles

If you start making extensive use of Maven profiles, you may want to separate your profiles from your POM in a separate file named profiles.xml. You can mix and match profiles defined in the pom.xml with profiles defined in the external profiles.xml file. Just place the profiles element into profiles.xml in ${basedir} and run Maven as you normally would. This profiles.xml file would look something like Example 11-6.

Example 11-6. Placing profiles in a profiles.xml file

 <profiles>
    <profile>
      <id>development</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
              <debug>true</debug>
              <optimize>false</optimize>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
    <profile>
      <id>production</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
              <debug>false</debug>
              <optimize>true</optimize>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>

You might find that your profiles have grown so large that you are having trouble managing the pom.xml, or you might just find separating the pom.xml from the profiles.xml file is a cleaner approach to putting everything into a single file. You can invoke profiles stored in profiles.xml the same way you would invoke them if they were defined in the pom.xml.

Get Maven: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.