1

I'm using the properties-maven-plugin to load properties from a custom properties file. The properties file name depends on the selected build profile.

       <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>properties-maven-plugin</artifactId>
            <version>1.0.0</version>
            <executions>
                <execution>
                    <phase>initialize</phase>
                    <goals>
                        <goal>read-project-properties</goal>
                    </goals>
                    <configuration>
                        <files>
                            <file>src/main/vpn/vpn-${environment}.properties</file>
                        </files>
                    </configuration>
                </execution>
            </executions>
        </plugin>

Each vpn-${environment}.properties file contains two properties, e.g. for dev environment:

  • vpn.username=dev_user
  • vpn.password=dev_password

Two profiles default and dev have been defined:

 <profiles>
    <profile>
        <id>default</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>                
            <environment>default</environment>              
        </properties>
    </profile>
    <profile>
        <id>dev</id>
        <properties>                
            <environment>dev</environment>              
        </properties>
    </profile>
</profiles> 

What I'm trying to do now is to use the maven-resources-plugin to replace the vpn property placeholders used within a unix template file with the property values of the corresponding active profile and to copy this file to another location.

    <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <executions> <execution>
                    <id>copy-script</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>src/main/vpn/bin</outputDirectory>
                        <resources>
                            <resource>
                                <directory>${basedir}/unix-template</directory>
                                <filtering>true</filtering>
                                <includes>
                                    <include>customUnixTemplate</include>
                                </includes>
                            </resource>
                        </resources>
                    </configuration>
                </execution>

The customUnixTemplate file contains something like vpn-connect ${vpn.username} ${vpn.password}.

After I build the project with mvn clean install -Pdev and open the output file in the src/main/vpn/bin directory I can see for a split second that the properties were taken from the correct properties file, i.e. vpn-dev.properties, but the file gets overwritten by the default properties file and I can't figure out why. I suspect that it has something to do with the build phases/orders in which the plugins are executed. Can anybody help?

Edit in response to Andrei's comment: The maven resource plugin uses a second execution:

             <execution>
                    <id>copy-resources</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/${project.artifactId}/conf</outputDirectory>
                        <resources>
                            <resource>
                                <directory>src/main/resources</directory>
                                <filtering>true</filtering>
                                <includes>
                                    <include>log4j2.xml</include>
                                </includes>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
user35934
  • 374
  • 2
  • 13
  • The part that bothers me is that you're running the `maven-resources-plugin` very late (i.e. `prepare-package`) and still something overwrites your resources; if any overwriting happens with your config, it should be `prepare-package` that should do it, not the other way around. Do you have other executions that you haven't shown, such as another `resources` execution or another property loader execution? (please check parent poms as well) – Andrei Jun 18 '17 at 18:30
  • @Andrei thx for replying. I've updated my post – user35934 Jun 19 '17 at 04:39
  • Looks like it should work. Strange that the behavior is as if those properties co-exist (i.e. in the same run you have also `dev` and `default` properties). Are you sure you're not running two phases such as `mvn compile package`? (`clean` doesn't count). Perhaps you ca figure out which phase is responsible for overwriting. Can you run `mvn clean test -P dev` and see wbat you get? I want to figure out if `prepare-package` is to blame and `test` is the phase before it. – Andrei Jun 19 '17 at 05:46

0 Answers0