Whats the purpose of “properties-maven-plugin” and

2019-08-01 15:31发布

问题:

I would like to have an explanation for the maven filters tag and how it corresponds to the files tag from the plugin properties-maven-plugin.

The profile in question:

<profile>
    <id>local-spm-caefeeder-preview</id>
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>properties-maven-plugin</artifactId>
                <version>1.0-alpha-1</version>
                <executions>
                    <execution>
                        <phase>initialize</phase>
                        <goals>
                            <goal>read-project-properties</goal>
                        </goals>
                        <configuration>
                        <quiet>true</quiet>
                        <files>
                            <file>${main.basedir}/src/config/global.properties</file>
                            <file>${main.basedir}/src/config/caefeeder/caefeeder_global.properties</file>
                            <file>${main.basedir}/src/config/caefeeder/caefeeder_preview.properties</file>
                            <file>${main.basedir}/src/config/local.properties</file>
                            <file>${main.basedir}/src/config/${user.name}.properties</file>
                        </files>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <filters>
            <filter>${main.basedir}/src/config/caefeeder/caefeeder_global.properties</filter>
            <filter>${main.basedir}/src/config/caefeeder/caefeeder_preview.properties</filter>
            <filter>${main.basedir}/src/config/local.properties</filter>
            <filter>${main.basedir}/src/config/${user.name}.properties</filter>
        </filters>
    </build>
</profile>

From my research filters defines files which have variables in them which need to be replaced. Does "properties-maven-plugin" provide these variables from the files defined in files tag?

回答1:

First of all, to have Maven filters actually work you need to enable resource filtering for certain directories, e.g.

  <resource>
    <directory>src/main/resources</directory>
    <filtering>true</filtering>
  </resource>

Just listing some filter files does nothing since these read values are not used anywhere.

Now, the explanation of difference here. Goal read-project-properties of properties-maven-plugin read some values from properties files and stores them as Maven variables. It is executed pretty early within Maven lifecycle (<phase>initialize</phase>) so they are available almost from the beginning. So, we can say this is a way to externalize Maven properties by putting them into some regular properties files instead of possibly huge <properties> block in pom.xml. What is important here, Maven variables are used in resource filtering.

Now, the <filters> tag. These files don't provide Maven project variables. They provide variables used just for resource filtering.

So, in context of resource filtering, the actual behaviour is pretty the same. Properties read by properties-maven-plugin will be used for resource filtering as well as properties read from filter files. However, those read by properties-maven-plugin will be also available as Maven project properties so they can be used e.g. in plugins' configuration or any other POM-related stuff.