how to update a property file using maven or pom.xml - java

I have created an automation framework where I am reading values from a property file say "config.properties".
My config.propertioes file contains following :
BrowserName=${browser}
Environment=${env}
I am reading browser value from the property file and passing it to my selenium script to run it.
Now I wants to replace "${browser}" && "${env}" with value "ie" && "IT" using pom.xml. Is there any way/plugin using which I can edit a property file using pom.xml.
Please suggest.
#Keshava
I am putting whole example below as suggested below :
1.Have 2 property files: ◦project.properties: This is the file that we commit in the repository. It consists data as follows: ◾project.connection.username=##DB_USERNAME##
project.connection.password=##DB_PASSWORD##
◦build.properties: This is the file that we do not commit in the repository and is maintained at each of the deployment environments, be it the developers env., UAT env or Production env. The contents of this file are as follows: ◾db.username=mydbuser
db.password=mydbpassword
2.In the project’s pom.xml add the following plugin and a execution:
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>maven-replacer-plugin</artifactId>
<version>1.3.5</version>
<executions>
<execution>
<id>replaceTokens</id>
<phase>prepare-package</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<file>target/classes/project.properties</file>
<replacements>
<replacement>
<token>##DB_USERNAME##</token>
<value>${db.username}</value>
</replacement>
<replacement>
<token>##DB_PASSWORD##</token>
<value>${db.password}</value>
</replacement>
</replacements>
</configuration>
</plugin>
from above, I understand that "##DB_USERNAME##" is from "project.properties". But, from which properties file this "${db.username}" value will be taken?. how my pom will understand from where to take "${db.username}" .
Do I need to pass this value in maven goal like below :
mvn clean install -Ddb.username=myuserid

Hello you can use the maven resource plugin.
This plugin implement "Maven Filtering".
<project>
...
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<id>selenium-profile-chrome</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/selenium</outputDirectory>
<resources>
<resource>
<directory>src/non-packaged-resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
...
</build>
...
</project>

you could tryin using the maven replacer plugin
See https://code.google.com/archive/p/maven-replacer-plugin/
See an example here

Related

Maven : Download of executable zip based on OS

I want to download a zip file via mavens POM.XML, but based on the OS i need different URLs (Linux/Windows).
How can I change the URL based on the OS maven is executed? (locally, its Windows, on Jenkins its then Linux)
This is how the plugins part of my POM.XML looks like:
<build>
<plugins>
<plugin>
<groupId>com.googlecode.maven-download-plugin</groupId>
<artifactId>download-maven-plugin</artifactId>
<executions>
<execution>
<!-- the wget goal actually binds itself to this phase by default -->
<phase>process-resources</phase>
<goals>
<goal>wget</goal>
</goals>
<configuration>
<url>https://test/chromedriver_win32.zip</url>
<unpack>true</unpack>
<outputFileName>chromedriver</outputFileName>
<outputDirectory>${project.build.directory}/chromeDriver</outputDirectory>-->
<outputDirectory>${user.home}/localdata/tools/chromeDriver</outputDirectory>
<overwrite>true</overwrite>
<skipCache>true</skipCache>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Maven - URI is non hierarchial

Im trying learn Spring and Maven but im having some trouble.
When I go to run my tests from the terminal using mvn clean install I'm getting this error: java.lang.IllegalArgumentException: URI is not hierarchical . This is the block of code that throws the error :
LocationWeatherRootResponseTest.class.getClassLoader().getResource("extent.xml")).toURI()
When I change the above code to the following Im getting a null pointer exception.
LocationWeatherRootResponseTest.class.getClassLoader().getResourceAsStream("extent.xml")))
Update
When I change the code to the below Im getting a new error.
File file = new File(WeatherTest.class.getClassLoader().getResource("extent.xml").getPath());
Reporter.loadXMLConfig(file);
stacktrace :
java.io.FileNotFoundException: file:/home/user/IdeaProjects/spring-cucumber-test-harness/common/target/common-1.0-SNAPSHOT.jar!/extent.xml (No such file or directory)
I managed to solve this issue using maven-remote-resources-plugin . Now when I run mvn clean install on the master POM the framework runs from e2e.
In the module containing the resources I wanted to share, I added the following to the POM file
<build>
<plugins>
<plugin>
<artifactId>maven-remote-resources-plugin</artifactId>
<version>1.7.0</version>
<executions>
<execution>
<goals>
<goal>bundle</goal>
</goals>
</execution>
</executions>
<configuration>
<includes>
<include>**/*.xml</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
In the module where I want to use the resource. I added the following
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-remote-resources-plugin</artifactId>
<version>1.7.0</version>
<configuration>
<resourceBundles>
<resourceBundle>{groupId}:{resource artifactId}:1.0-SNAPSHOT</resourceBundle>
</resourceBundles>
</configuration>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
</execution>
</executions>
</plugin>

With maven, how to modify deploy files just prior to deploy phase?

I have a maven project with one war and several ear projects. Each ear project requires a slightly different war/WEB-INF/web.xml. Each ear's pom.xml uses com.google.code.maven-replacer-plugin:replacer and org.codehaus.mojo:truezip-maven-plugin to replace tokens in the web.xml, and then place that new web.xml in the final <project>-app.ear/web.war/WEB-INF. This all works great with building and creating the final EAR artifacts.
The problem I'm having is that when I run (using Netbeans, but that shouldn't matter), the web.xml used for deployment (<project>/target/gfdeploy/first-app/web_war/WEB-INF/web.xml) is the tokenized version. I tried adding execution phases for deploy, but that doesn't work.
How can I ensure that the run deploy has the modified web.xml so I can test my app during development?
Here is the relevant parts of the ear pom.xml:
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.3</version>
<executions>
<execution>
<id>package-replace</id>
<phase>package</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
<execution>
<id>deploy-replace</id>
<phase>deploy</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<file>${project.parent.basedir}/${web.xml}</file>
<outputFile>${project.build.directory}/${web.xml}</outputFile>
<replacements>
<replacement>
<token>#REALM_NAME#</token>
<value>${web.realm}</value>
</replacement>
</replacements>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>truezip-maven-plugin</artifactId>
<version>1.2</version>
<executions>
<execution>
<id>package-replace-web-xml</id>
<goals>
<goal>copy</goal>
</goals>
<phase>package</phase>
<configuration>
<files>
<file>
<source>${project.build.directory}/${web.xml}</source>
<outputDirectory>${project.build.directory}/${project.build.finalName}/${web.zip}/WEB-INF</outputDirectory>
</file>
</files>
</configuration>
</execution>
<execution>
<id>package-replace-web</id>
<goals>
<goal>copy</goal>
</goals>
<phase>package</phase>
<configuration>
<files>
<file>
<source>${project.build.directory}/${project.build.finalName}/${web.zip}</source>
<outputDirectory>${project.build.directory}/${project.build.finalName}.ear</outputDirectory>
</file>
</files>
</configuration>
</execution>
<execution>
<id>deploy-replace-web-xml</id>
<goals>
<goal>copy</goal>
</goals>
<phase>deploy</phase>
<configuration>
<files>
<file>
<source>${project.build.directory}/${web.xml}</source>
<outputDirectory>${project.build.directory}/gfdeploy/${project.artifactId}/web-${project.version}_war/WEB-INF</outputDirectory>
</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
I suggest you to keep your default src/main/webapp/WEB-INF/web.xml fully functional for running during development. Then, keep a similar file called src/main/webapp/WEB-INF/web-ear.xml with all the replacement preparation.
Wrap all your replacement plugin strategy inside a maven profile and targeted to the web-ear.xml file. Add to this profile a maven-war-plugin configuration that will use the alternative web-ear.xml file during build, instead of the default web.xml (check: http://maven.apache.org/plugins/maven-war-plugin/):
<profiles>
<profile>
<id>change-war-profile</id>
<build>
<plugins>
<!-- all your replacement plugins here -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webXml>src/main/webapp/WEB-INF/web-ear.xml</webXml>
</configuration>
</plugin>
<plugins>
</build>
</profile>
</profiles>
Make sure to activate this profile during the EAR maven build:
mvn package -Pchange-war-profile
you can run your war with the jetty-maven-plugin choosing the run-war goal.
That goal run the packaged war.
See: https://www.eclipse.org/jetty/documentation/9.4.x/jetty-maven-plugin.html#running-assembled-webapp-as-war
First of all, deploy phase (of the build lifecycle) means deployment a production ready artifact to the remote Maven repository (e.g., Nexus or Artifactory). Roughly speaking, artifact deploy can be treated as copying the artifact. Read the Introduction to the Build Lifecycle for more details.
Secondly, Apache Maven does not support application deploy to the application server out-of-the-box. However, there are several ways to do it, depending on the application server you use. Which one do you use?
Special plugin can be found for JBoss Application Server - jboss-as-maven-plugin. See usage example:
<project>
...
<build>
...
<plugins>
...
<plugin>
<groupId>org.jboss.as.plugins</groupId>
<artifactId>jboss-as-maven-plugin</artifactId>
<version>7.9.Final</version>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>deploy</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
...
</build>
...
</project>
Similar plugin can be found for GlassFish Server: glassfish-maven-plugin.
Also, if this is acceptable for you, you can perform 2-steps deploy from Netbeans:
Build the project: run mvn package with all your plugins are configured at package phase.
Deploy the project: run application on the app server from Netbeans if it is supported (See NetBeans Java EE Servers Support page).
Hope this helps.

Using Maven resource plugin on dependecy jar file

Maven has resource plugin that can delimiter a character that can tell it in pom file , for example in the blow config define it that replace "#" character.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>target/filtered-resources/scripts</outputDirectory>
<resources>
<resource>
<directory>src/assemble/resources/scripts</directory>
<filtering>true</filtering>
</resource>
</resources>
<useDefaultDelimiters>false</useDefaultDelimiters>
<delimiters>
<delimiter>#</delimiter>
</delimiters>
</configuration>
</execution>
</executions>
</plugin>
but it work on files that directly exists in my project , i want to do the same job in a jar file , i have a jar file that exists some java script plugins and i want add it to my project but i need replace somethings on it .
and the dependency is something like this :
<dependency>
<groupId>sample.javascript</groupId>
<artifactId>jsLibrary/artifactId>
<version>1.0.0</version>
</dependency>
1) you need to unpack your jar. You can use this plugin: https://maven.apache.org/plugins/maven-dependency-plugin/examples/unpacking-artifacts.html
2) change
<directory>src/assemble/resources/scripts</directory>
to the directory with unpacked jar

build-helper-maven-plugin add-test-resource error

I have this project structure:
/src
/main
/java
/resources
/test
/java
/resources
/it
/java
/resources
test for unit tests and it for integration tests. I'm using build-helper-maven-plugin to add additional test sources/resources to the classpath for later use maven-surfire-plugin for run
unit tests and maven-failsafe-plugin for integration tests.
Plugin config as belows:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.9.1</version>
<executions>
<execution>
<id>add-integration-test-sources</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/it/java</source>
</sources>
</configuration>
</execution>
<execution>
<id>add-integration-test-resources</id>
<phase>generate-test-resources</phase>
<goals>
<goal>add-test-resource</goal>
</goals>
<configuration>
<resources>
<directory>/src/it/resources</directory>
</resources>
</configuration>
</execution>
</executions>
</plugin>
This works fine for the test-sources (they are coppied correctly to /target/test-classes) but doesn't copy test-resources. I've tried different combinations of <configuration>: use <resource> instead <directory>, use an specific file instead a directory...but neither works.
Stacktrace with the error:
Caused by: org.apache.maven.plugin.PluginConfigurationException: Unable to parse configuration of mojo org.codehaus.mojo:build-helper-maven-plugin:1.9.1:add-test-resource for parameter directory: Cannot configure instance of org.apache.maven.model.Resource from src/it/resources
at org.apache.maven.plugin.internal.DefaultMavenPluginManager.populatePluginFields(DefaultMavenPluginManager.java:597)
at org.apache.maven.plugin.internal.DefaultMavenPluginManager.getConfiguredMojo(DefaultMavenPluginManager.java:529)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:92)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
PROVISIONALLY, I've fixed it adding the integration tests resources to maven <build> configuration:
<build>
...
<testResources>
<testResource>
<directory>src/it/resources</directory>
</testResource>
</testResources>
</build>
But I would prefer to have centralized all classpath modifications under build-helper-maven-plugin.
Can anyone post example with a correct config?
Thanks in advance.
According to the javadoc of the maven-build-helper-plugin:add-test-resources. The resources is an array of org.apache.maven.model.Resource. Thus you must configure it this way:
<configuration>
<resources>
<resource>
<directory>/src/it/resources</directory>
</resource>
</resources>
</configuration>
Take a look at how to configure plugin parameters.

Categories