Passing the selected maven profile/value to java

Reading Time: < 1 minute

in this post we will pass the selected env value to the java file.  when you start the project as follows:

-Denv=asseco-test

pom.xml

<profiles>
 		<profile> 
 			<id>asseco-test</id> 
 			<activation> 
 				<activeByDefault>true</activeByDefault> 
 				<property> 
 					<name>asseco-env</name> 
 					<value>asseco-test</value> 
 				</property> 
 			</activation> 
 			<properties> 
 				<profile-id>asseco-test</profile-id> 
 			</properties> 
 		</profile> 
 		<profile> 
 			<id>asseco-prod</id> 
 			<activation> 
 				<activeByDefault>false</activeByDefault> 
 				<property> 
 					<name>asseco-env</name> 
 					<value>asseco-prod</value> 
 				</property> 
 			</activation> 
 			<properties> 
 				<profile-id>asseco-prod</profile-id> 
 			</properties> 
 		</profile>
</profiles>

java file

private Properties props = new Properties();
String selectedPaymentProfile = System.getProperty("env");
		final String fileName = selectedPaymentProfile.concat(".properties");
		try {
			props.load(new InputStreamReader(getClass().getClassLoader().getResourceAsStream(paymentProperties)));
			
			URI = props.getProperty("URI");
			
		} catch (FileNotFoundException e) {
			logger.error("Exception in providerDetails: " + e);
		} catch (IOException e) {
			logger.error("Exception in providerDetails: " + e);
		}