Reputation: 33227
In order to connect to an external system, for which each developer has a personal username and password, I need to pull in a username and password to my JUnit test cases:
String username = System.getProperty("ext.username");
The above line would be called inside any class inside my test
folder
How can I best do this via Maven? I think the best scope for the property is inside settings.xml
, but I'm not sure about the best way to transfer it from there into my code.
Upvotes: 1
Views: 614
Reputation: 78011
The problem is best solved using a Maven build profile.
In your POM you declare a default value for the properties (To prevent run-time possible errors).
<project>
..
<properties>
<ext.username>XXXXX</ext.username>
<ext.password>YYYYY</ext.password>
</properties>
Each developer can then override these properies in theor local build using a default profile specified in their settings file: $HOME/.m2/settings.xml
<settings>
..
<profiles>
<profile>
<id>dev</dev>
<activeByDefault>true</activeByDefault>
<properties>
<ext.username>XXXXX</ext.username>
<ext.password>YYYYY</ext.password>
</properties>
</profile>
..
</profiles>
..
</settings>
For those worried about security is also possible to encrypt the password.
Finally, it's possible to have multiple profiles configured. These can be chosen at run-time (using profile id) in order to support multiple build environments:
mvn -Pdev ..
mvn -Pprod ..
Upvotes: 4