简体   繁体   中英

What is Maven's counterpart of Ant's <property> “prefix” attribute?

I am currently migrating Java Web Project build process from Ant to Maven. Current process uses several environment-specific properties files, which are inserted into main application properties during WAR file generation, based on the environment parameter provided to Ant.

One of the functionality I could not easily replicate in Maven is the properties file prefix, ie:

<property name="some.env.file" value="someEnv.properties"/>
<property file="${some.env.file}" prefix="some.env"/>

This prefixes all properties from file with "some.env", and all placeholders in application properties contains this prefix, ie:

some.property=${some.env.propertyOne}

Maven provides pom.xml filter and filtering elements to allow for properties replacement, but I could not find a way to prepend all properties names in a file with an environment-specific prefix set as build parameter, which forces me duplicate properties files for Maven build process. Is there a way to replicate this Ant's original functionality in Maven?

I suggest you to use the maven-antrun-plugin to load the property file with a simple ant task using the prefix. And also enable exportAntProperties so that properties defined by ant become available in maven.

Something like this will brings all properties defined by ant available in your maven build during the validate phase (and all following phases).

 <plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.7</version>
    <executions>
      <execution>
        <phase>validate</phase>
        <configuration>
          <exportAntProperties>true</exportAntProperties>
          <target>
            <property file="${some.env.file}" prefix="some.env"/>
          </target>
        </configuration>
        <goals>
          <goal>run</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM