0

I have a testng listener where it needs to retrieve the following values from the pom.xml.

<groupId></groupId>
<artifactId></artifactId>
<version></version>

I tried [1] but that was giving the values of the listener pom. What I need to get is the values of the pom.xml that I will eventually use this lisetner

In Summary: I have a testng listener implementation "listener.jar" which is a dependency in "Project A" and need to read the above above parameters in "Project A".

[1] Get Maven artifact version at runtime

Community
  • 1
  • 1
nu1silva
  • 149
  • 1
  • 9
  • You will have to create an XmlReader, which will be parameterized with the Maven POM XSD Schema (and you'll have to generate the Java classes from it) [XSD](https://maven.apache.org/xsd/maven-4.0.0.xsd). Once you've done this, you will be able to read the data your `pom.xml` contains. – DamCx Dec 09 '16 at 09:35
  • You wanna update your `pom.xml`on-the-fly? – DamCx Dec 09 '16 at 09:46
  • Not update just read. I can only change my listener and not the project. I just need to read the pom.xml in "the project" and retrieve the values. – nu1silva Dec 09 '16 at 09:49
  • So you have to do it outside of your project, otherwise you might have troubles to access it – DamCx Dec 09 '16 at 09:51
  • yes it needs to be from my end. Did consider the XMLReader option but was looking for a way to get these through testng. – nu1silva Dec 09 '16 at 09:54

2 Answers2

1

Solved! and here is how it was done.

On the maven-surefire-plugin I added the required values as "systemPropertyVariables"

<systemPropertyVariables>
      <component.name>${artifactId}</component.name>
      <component.version>${version}</component.version>
</systemPropertyVariables>

So the plugin would look like this;

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12</version>
                <configuration>
                    <systemPropertyVariables>
                        <component.name>${artifactId}</component.name>
                        <component.version>${version}</component.version>
                    </systemPropertyVariables>
                    <suiteXmlFiles>
                        <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
                    </suiteXmlFiles>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

In the testng.xml we need to mention these as parameters inorder to access them through the listener.

<suite name="SampleTest_TestSuite1" verbose="1">
<parameter name="component" value="${component.name}"/>
<parameter name="version" value="${component.version}"/>

<test name="SampleTest" preserve-order="true" parallel="false">
    <classes>
        ...
    </classes>
</test>
</suite>

Then we access these parameters through testng listener.

public class DataBaseReporterListener implements IReporter {

    @Override
    public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String s) {

        String componentName = suites.get(0).getParameter("component");
        String componentVersion = suites.get(0).getParameter("version");
nu1silva
  • 149
  • 1
  • 9
-1

give a properties tag in POM.xml and add an appropriate name to it. Add the properties you need to add in custom nodes like

<properties>
<property>
<id>ABC</id>
...
<customprop1>property value</customprop1>
</property>
</properties>

Now after activationg this profile you can access value in any jsp file by just using

${customprop1}
  • This won't help him to get his actual Dependencies versions in his Java code, but only some properties within his `pom.xml`. – DamCx Dec 09 '16 at 09:44
  • I wont be able to update the pom.xml. So, thats out of the question. – nu1silva Dec 09 '16 at 09:45