0

I included the maven property plugin like proposed in a response to this question Specify system property to Maven project. In order to be able to set my database properties from file (and override it with maven parameters in testing environment). However if I try to access one of the properties via System.getProperty("mysql.url") for example null is returned. How can I access the properties set from set properties goal.

private void initializeDatabaseConfiguration() {
    url = System.getProperty("mysql.url");
    con = DriverManager.getConnection(url, username, password);
}

And my maven plugin is defined like this:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>properties-maven-plugin</artifactId>
    <version>1.0-alpha-2</version>
    <executions>
        <execution>
            <phase>initialize</phase>
            <goals>
                <goal>set-system-properties</goal>
            </goals>
            <configuration>
                <files>                          
                    <file>src/main/resources/config.properties</file>
                </files>
                <properties>
                    <property>
                        <name>mysql.url</name>
                        <value>${mysql.url}</value>
                    </property>
                </properties>
            </configuration>
        </execution>
    </executions>
</plugin>
zforgo
  • 2,508
  • 2
  • 14
  • 22
PKuhn
  • 1,338
  • 1
  • 14
  • 30

2 Answers2

0

Try to add phase:

<execution>
<phase>initialize</phase>
 <goals>
   <goal>set-system-properties</goal>
 </goals>

If you use the phase initialize, you can use the propriets after phase 'initialize'

question_maven_com
  • 2,457
  • 16
  • 21
0

There is a goal, called read-project-properties. You need to use this goal to read properties from the specific file/files e.t.c. Check please the docs. This way you will be able to load maven properties into build process.

This is the first thing. The second thing is - System.getProperty() will not pick up the properties you have defined for maven build. This properties are just for maven build, they will not be passed right to your JVM. So properties-maven-plugin is not really what you want here. To be able to access parameters with System.getProperty("some-parameter"), you need to pass -Dsome-parameter=value to java command.

starball
  • 20,030
  • 7
  • 43
  • 238
Mikhail2048
  • 1,715
  • 1
  • 9
  • 26