Maven Profile reads data from Properties file - java

I have a Selenium project which uses Maven as a build tool and I want to read different environment details (protocol, domain, subdomain, etc) from a .properties file. Would it be possible to use Maven profiles in order to run my tests on different environments such as dev, staging, prod based on the profile which I am specifying when triggering the mvn command?
dev.properties:
protocol=http
domain=domain.com
subdomain=www
pom.xml:
<profiles>
<profile>
<id>prod</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>pre_prod</id>
</profile>
<profile>
<id>dev</id>
</profile>
</profiles>
mvn:
mvn clean test -Pdev
Data should then be retrieved in the java code using
System.getProperty("protocol");
System.getProperty("domain");
System.getProperty("subdomain");
Any help would be very much appreciated.
Thanks!

If you just want to read different properties files based on a command line argument, could you not just pass in a string for example -Denv=dev
Then in your properties reader class have it init the properties file based on System.getProperty("env");
Properties generalProperties = new Properties();
String generalPropertiesFileName = "data/"+
System.getProperty("env") + ".properties";
initProperties(generalProperties, generalPropertiesFileName);
Alternatively you can also pass properties from the command line to your POM in the same way -
<properties>
<property>
<protocol></protocol>
<domain></domain>
<subdomain></subdomain>
<property>
<properties>
And then these can be passed in from the command line as -Dprotocol=foo etc
Hope that helps?

Related

'gpg2' is not recognized as an internal or external command

I use the maven-gpg-plugin in linux all is okay I can install the gpg2 for sign but in windows there is a problem with deploying.
There is log 'gpg2' is not recognized as an internal or external command,
operable program or batch file. (The same I get for gpg2 --version)
And
Failed to execute goal org.apache.maven.plugins:maven-gpg-plugin:1.5:sign (sign-artifacts) on project.
How can I install gpg2 for windows?
In you settings.xml file, change the gpg.executable property. Example.
<profiles>
<profile>
<id>ossrh</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<gpg.executable>gpg</gpg.executable>
<gpg.passphrase>PASSPHRASE</gpg.passphrase>
</properties>
</profile>
</profiles>

Can't start with profiles in spring + maven + tomcat

I need to use profiles with spring. I use local Tomcat.
There is maven project, so, in pom.xml I added:
<profiles>
<profile>
<id>dev</id>
<activation>
<property>
<name>spring.profiles.active</name>
<value>dev</value>
</property>
</activation>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
</dependencies>
</profile>
<profile>
<id>at1</id>
<activation>
<activeByDefault>true</activeByDefault>
<property>
<name>spring.profiles.active</name>
<value>at1</value>
</property>
</activation>
</profile>
</profiles>
and in application.properties added spring.profiles.active=${activatedProperties}
note: spring.profiles.active=#activatedProperties# tried already too
and there is two files application-at1.properties and application-dev.properties
When build war with -Dspring.profiles.active=dev there is error message - params from this files not found.
My tomcat customizations are:
Can't tell exactly where you are but seems to me that you're using a property place holder, Spring Boot is not picking up a profile because the placeholder actually has no value.
You can configure this in the following way:
Using Property Placeholders:
application.properties
spring.profiles.active=${activatedProperties}
pom.xml
<property>
<name>activatedProperties</name>
<value>dev</value>
</property>
Just specify the runtime argument
Remove property from pom.xml and adjust your application.properties with some default value or don't specify it at all
spring.profiles.active=at1 #you can remove this line if you want.
Then run-war with argument -Dspring.profiles.active=dev
Working with Maven Profiles
You can run maven with a -P dev to make sure that the goal is executed with the correct profile.

Maven profile activation from windows system variable not working

I have the following maven profile configuration
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
<property>
<name>spring.profiles.active</name>
<value>dev</value>
</property>
</activation>
</profile>
<profile>
<id>test</id>
<activation>
<property>
<name>spring.profiles.active</name>
<value>test</value>
</property>
</activation>
</profile>
</profiles>
I have this in application.properties
spring.profiles.active=dev
So dev is my default for both spring and maven profiles.
I then have Windows system variable SPRING_PROFILES_ACTIVE set with a value of test to explicitly use the test profile.
which according to the documentation this should override the dev value in my properties file
http://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html
69.5 Set the active Spring profiles
The Spring Environment has an API for this, but normally you would set
a System property (spring.profiles.active) or an OS environment
variable (SPRING_PROFILES_ACTIVE). E.g. launch your application with a
-D argument (remember to put it before the main class or jar archive):
$ java -jar -Dspring.profiles.active=production
demo-0.0.1-SNAPSHOT.jar
In Spring Boot you can also set the active profile in
application.properties, e.g.
spring.profiles.active=production
A value set this way is replaced by the System property or environment
variable setting, but not by the SpringApplicationBuilder.profiles()
method. Thus the latter Java API can be used to augment the profiles
without changing the defaults.
See Chapter 25, Profiles in the ‘Spring Boot features’ section for
more information.
Now when I run my Spring application it IS using the test profile in Spring, however the maven profile activation does not pick up on this and is still using the dev profile.
I have tried setting SPRING_PROFILES_ACTIVE as the name in the <property> element to make sure it wasn't a issue with the lowercase . version and the uppercase _ version but that didn't help.
It DOES work if I supply the variable when I run maven with -Dspring.profiles.active=test.
Any help is greatly appreciated.
edit: apparently this also doesn't work when I deploy a war to tomcat where catalina.properties contains spring.profiles.active=test. The same thing, Spring uses test but Maven is still stuck on dev.
How about activating the profile by using environment variable ?
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
<property>
<name>spring.profiles.active</name>
<value>dev</value>
</property>
</activation>
</profile>
<profile>
<id>test</id>
<activation>
<property>
<name>env.SPRING_PROFILES_ACTIVE</name>
<value>test</value>
</property>
</activation>
</profile>
</profiles>
Unfortunately, what you want to do is not possible. Profiles can only be activated from the command line or in some cases from the settings.xml. See this answer. This one includes links to JIRA issues discussing the challenges and what others have tried to make it work.
System properties are set on the command line.

IntelliJ: activate Maven profile when running Junit tests

I have declared some properties that are specific to Maven profiles. A part of my pom.xml:
<profiles>
<profile>
<id>release</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<my.properties.file>foo.xml</my.properties.file>
</properties>
</profile>
<profile>
<id>ci</id>
<properties>
<my.properties.file>bar.xml</my.properties.file>
</properties>
</profile>
</profiles>
I encounter some problem to use the "ci" Maven profile when I start Junit tests via IntelliJ IDEA 2016.
I activate my profile via the "Maven Projects" panel, then I start tests. The problem is the "my.properties.file" property value is equal to "foo.xml", not "bar.xml".
I have no problem with command-line (I can use the "-Pci" flag). How can I tell IntelliJ to use the "ci" profile? Thx.
You should add the profiles to the Maven setting.xml file (you should find it in the path ${YOUR_MAVEN_HOME}\apache-maven-3.1.1\conf\setting.xml).
Then, you have to open intellij, click on View > Tool Windows > Maven Projects. There, you should see your profiles (ci and release) and select the correct one.
Hope this can help you.
Just finally solved it.
<profile>
<id>profile-to-be-activated-on-build</id>
<activation>
<activeByDefault>false</activeByDefault><!-- on your flavor -->
<property>
<name>mvn-profile-env-var-trigger</name>
</property>
</activation>
</profile>
Goto JUnit default profile (aka configuration template). Add into JVM args:
-Dmvn-profile-env-var-trigger
You may need to manually reload maven profiles in IDE.
Also make sure on [Settings > Build Tools > Maven > Running tests] envVars is checked (or better everything).

Is it possible to have multiple POM and TESTNG files in a project?

I have the following setup:
Jenkins points to -> Pom.xml which points to -> Testng.xml
My goal is to have multiple jobs in Jenkins, each pointing to a different environment. Those environments are currently separated by multiple TestNG files. I have parameters setup in each testng.xml file which point the different environments.
Is there a way to have multiple POM files, each pointing to a different testng file?
Thanks
You should use a single pom file which defines a "profile" for each environment. The profile for an environment will customize all the configuration that is required for that environment (including pointing to the proper testng file).
In your pom, you will include
<profiles>
<profile>
<id>foo</id>
<properties>
...
</properties>
<build>
...
</build>
</profile>
<profile>
<id>bar</id>
<properties>
...
</properties>
<build>
...
</build>
</profile>
</profiles>
and invoke it with
mvn -Pfoo
or
mvn -Pbar
As you say, you will want to have a Jenkins job per environment (with the -Pfoo in the configuration for that job) or you can have a single Jenkins job and pass the profile as a parameter to the job.

Categories

Resources