Question

I'm looking for the best possible approach so that I can import a .properties file in a maven pom file that will set properties found within the properties file as System properties.

For example I have a dev.properties file, which contains System properties that are developer specific, which will mean that the dev.properties file will not be persisted in a source code repository.

I've come across the maven properties plugin, but this doesnt set the properties found in the file as system properties, here is the declaration of this plugin in my pom file:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>properties-maven-plugin</artifactId>
    <version>1.0-alpha-2</version>
    <executions>
        <execution>
            <phase>initialize</phase>
            <goals>
                <goal>read-project-properties</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
       <files>
           <file>${basedir}/dev.properties</file>
       </files>
    </configuration>
</plugin>

But within my java code, when I run either a test goal, or a jacoco reporting goal, where I use System.getProperties("prop1");, for example I am always being returned null, which is causing my tests to fail. Is this possible in maven? and am I on the right track?

Était-ce utile?

La solution

I would suggest to use the goal set-system-properties of the same plugin or you can also use the usual way of setting system-properties via using the properties after reading them via your configuration like this for your unit tests:

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.22.0</version>
        <configuration>
          <systemPropertyVariables>
            <propertyName>propertyValue</propertyName>
            <buildDirectory>${project.build.directory}</buildDirectory>
            [...]
          </systemPropertyVariables>
        </configuration>
      </plugin>
    </plugins>
  </build>
  [...]
</project>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top