How to convert a maven plugin into a gradle plugin (maven-surefire-puglin )? - maven

I am trying to use this plugin but with gradle:
<properties>
<aspectj.version>1.8.10</aspectj.version>
</properties>
<dependencies>
<dependency>
<groupId>io.qameta.allure</groupId>
<artifactId>allure-junit4</artifactId>
<version>LATEST_VERSION</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20</version>
<configuration>
<testFailureIgnore>false</testFailureIgnore>
<argLine>
-javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
</argLine>
<properties>
<property>
<name>listener</name>
<value>io.qameta.allure.junit4.AllureJunit4</value>
</property>
</properties>
</configuration>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
Instead, if it is not really necessary (because there is any easier way on gradle to proceed) can someone give a clue on how to do it?

Have a look at the documentation on testing Java & JVM projects in Gradle.
It should answer the basic questions around setting up testing in a Gradle project.
However if you are using specific features of the maven-surefire-plugin that you fail to replicate, please amend your question with more specific elements.

Related

Using Provided Artifact As Maven Plugin Dependency

This seems like it should be a simple question, but I can't seem to find any information about it. When a maven plugin has a required dependency, is it possible to tell it to use an artifact defined elsewhere in the section of the pom?
As an example, I'm trying to add the 'maven-processor-plugin' to my build. That plugin has a dependency on 'hibernate-jpamodelgen'. I'm working with wildfly, so I already have that jar as a dependency of the project. I want to ensure I am using the same version for both. Is what I'm trying to do even possible?
Some code snippets:
<dependencies>
<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-ejb3</artifactId>
<version>${version.server.bom}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArguments>
<processor>-proc:none</processor>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<version>4.5</version>
<executions>
<execution>
<id>process</id>
<goals>
<goal>process</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<outputDirectory>${project.build.directory}/generated-sources/java/jpametamodel</outputDirectory>
<processors>
<processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
</processors>
<overwrite>true</overwrite>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<!-- How do I handle this without hard coding the version? -->
<!-- <version>???</version> -->
</dependency>
</dependencies>
</plugin>
</build>
Define a property like <hibernate-jpamodelgen.version> in the <properties> section of the POM.
Then use that property for the version like ${hibernate-jpamodelgen.version}.

Can we execute multiple profiles in the same pom.xml file

I have created a Maven project in Eclipse and have 2 xml files in it. I have created 2 profiles "TestNG.xml" and "TestNG2.xml" in the pom.xml file for my Maven project.
I ran the following command from command prompt
- mvn test
Expected behavior : TCs under both the TestNG.xml and TestNG2.xml files should get executed.
Actual behavior : Only TCs under TestNG2.xml file is getting executed.
NOTE :- When i am executing the .xml files individually using the below command it works fine{TestNG.xml file has been profiled as Regression in the pom.xml file}
- mvn test -PRegression
Below is the snapshot of my code from pom.xml file
<groupId>Rohit</groupId>
<artifactId>MavenProject05202020</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>MavenProject05202020</name>
<url>http://maven.apache.org</url>
<profiles>
<profile>
<id>Regression</id>
<!-- Below is MAVEN SURE FIRE PLUGIN. This will help to execute all your TCs present in your test folder{i.e.:-src/test/java}-->
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>
<!-- Below is MAVEN SURE FIRE PLUGIN.
<profile>
<id>Smoke</id>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng2.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>
</profiles>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- below is the dependency for Selenium project -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.8.1</version>
</dependency>
<!-- Below is the dependency for TESTNG. -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.1.0</version>
<scope>test</scope>
</dependency>
<!-- Below is the dependency for RestAPITest project -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>4.3.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
If you just define the surefire-plugin twice, these definitions will override each other.
Instead, you need to define an <execution> in the plugin declaration and bind it to a phase. Plugins can have more than one execution in the <executions> block.

Maven can't find any test

I copy pom.xml with a similar project were all ok. Add need dependencies. Also, I add Jupiter with scope "compile" otherwise project doesn't compile. I try to find a solution in similar problems but nothing found.
Env: ubuntu 18, idea 2018.3, openjdk8, maven last version.
I try to change more new or old version junit, surefire plugin and junit-platform-surefire-provider but don't find need combination.
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<junit.api.version>5.4.0</junit.api.version>
<junit.jupiter.version>5.4.0</junit.jupiter.version>
<junit.platform.version>1.3.1</junit.platform.version>
<aspectj.version>1.9.4</aspectj.version>
<allure.version>2.12.1</allure.version>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
</dependency>
<dependency>
<groupId>io.qameta.allure</groupId>
<groupId>io.qameta.allure</groupId>
<artifactId>allure-junit5</artifactId>
<version>${allure.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.4.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<testFailureIgnore>false</testFailureIgnore>
<argLine>
-javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
</argLine>
<systemProperties>
<property>
<name>junit.jupiter.extensions.autodetection.enabled</name>
<value>true</value>
</property>
</systemProperties>
</configuration>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>io.qameta.allure</groupId>
<artifactId>allure-maven</artifactId>
<version>LATEST_VERSION</version>
<configuration>
<reportVersion>2.4.1</reportVersion>
</configuration>
</plugin>
</plugins>
</build>
By default the maven Surefire Plugin expects your tests filenames to match the following conditions:
**/Test*.java - includes all of its subdirectories and all Java
filenames that start with "Test".
**/*Test.java - includes all of
its subdirectories and all Java filenames that end with "Test".
**/*Tests.java - includes all of its subdirectories and all Java
filenames that end with "Tests".
**/*TestCase.java - includes all
of its subdirectories and all Java filenames that end with
"TestCase".
If you test file names don't match the above criteria you have two options. First of all modify your test filenames so that they do match the above criteria. Secondly you can modify the inclusion criteria by configuring the maven-surefire-plugin
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<includes>
<include>%regex[.*(Cat|Dog).*Test.*]</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
[...]
</project>
For more information have a look at the maven-surefire-plugin documentation.

For Selenium WebDriver - Maven and initial pom.xml configuration

I am new for Selenium Webdriver. When I start with the tool I came to know there are two things necessary to start with: Maven and pom.xml. But I didn't find the details on these things. Could anyone let know what is the meaning of these files and how do I create them.
Also I would be thankful, if someone could share some knowledge on the Selenium WebDriver like how do I start with the tool, and how to write a scripts - I have Java knowledge, so I can prefer to that language.
Thanks in advance :) awaiting to learn about the tools :)
Maven and it's pom.xml are not a must (but a recommended solution).
Their role in the process is just to add the selenium jar to your project.
You can manually add the Selenium jar file to your project by downloading the jar from http://docs.seleniumhq.org/download/ and adding it to your classpath.
OR,
https://code.google.com/p/selenium/downloads/list?can=1&q=&colspec=Filename+Summary+Uploaded+ReleaseDate+Size+DownloadCount
Selenium site has also the relevant documentation to get you started - http://docs.seleniumhq.org/docs/03_webdriver.jsp
Maven: http://maven.apache.org/
I would recommend starting with a pom.xml that looks like this. You will have to manually create the directories src/main/java, src/test/java, and src/test/resources, but after do so, if you run "mvn clean build", it will refresh and give you the proper perspective in Eclipse IDE. Maven can be confusing on a new project because it doesn't auto generate those directories. :
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>SeleniumMavenExample</groupId>
<artifactId>SeleniumMavenExample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<selenium.version>2.39.0</selenium.version>
<maven.surefire.plugin.version>2.16</maven.surefire.plugin.version>
<testng.version>6.8.7</testng.version>
</properties>
<build>
<directory>target</directory>
<outputDirectory>target/classes</outputDirectory>
<finalName>${project.artifactId}-${project.version}</finalName>
<testOutputDirectory>target/test-classes</testOutputDirectory>
<sourceDirectory>src/main/java</sourceDirectory>
<testSourceDirectory>src/test/java</testSourceDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>src/test/resources</directory>
</testResource>
</testResources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.surefire.plugin.version}</version>
<configuration>
<showSuccess>true</showSuccess>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
</suiteXmlFiles>
<configuration>
<systemPropertyVariables>
<build-name>${surefire.testng.build}</build-name>
</systemPropertyVariables>
<groups>${surefire.testng.groups}</groups>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.metamodel</groupId>
<artifactId>MetaModel-full</artifactId>
<version>4.0.0-incubating</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>${testng.version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>16.0.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.6</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>${selenium.version}</version>
</dependency>
<dependency>
<groupId>net.lightbody.bmp</groupId>
<artifactId>browsermob-proxy</artifactId>
<version>2.0-beta-9</version>
</dependency>
</dependencies>
</project>

Maven profile execution

I have a problem and could not identify the reason until now.
I have a maven project that has several modules. One of these modules is the webservices client.
So, during development, when running the install in maven, it needs to access the local server to generate the client. When I run the plugin to generate the release of the project, clients should point to the production server.
To do this I set as a key property ${server.address} which is used to point to the server when generating the clients. There is one profile which, when active, this key property rewrites the address to the production server.
What's going on? Running mvn install is generating correctly, ie, pointing to the local server. When I generate the release using the command mvn release:prepare -B release:perform -Denv=prd is not rewriting the variable as it should.
The strange thing is that if I run mvn install -Denv=prd, it generates correctly, pointing to the production server.
Could someone give me a hint of what to change to work also in release cycle?
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>groupId</groupId>
<artifactId>artifactId</artifactId>
<version>0.0.2-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<server.address>http://localhost:8080</server.address>
</properties>
<profiles>
<profile>
<id>prd</id>
<activation>
<property>
<name>env</name>
<value>prd</value>
</property>
</activation>
<properties>
<server.address>http://srvprd009:8080</server.address>
</properties>
</profile>
</profiles>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<testSourceDirectory>src/test/java</testSourceDirectory>
<testResources>
<testResource>
<directory>src/test/resources</directory>
</testResource>
</testResources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<executions>
<execution>
<id>generate-client</id>
<phase>generate-sources</phase>
<configuration>
<sourceRoot>src/main/gen</sourceRoot>
<wsdlOptions>
<wsdlOption>
<wsdl>${server.address}/services/utilities?wsdl</wsdl>
<extraargs>
<extraarg>-p</extraarg>
<extraarg>${project.package}</extraarg>
<extraarg>-impl</extraarg>
<extraarg>-verbose</extraarg>
<extraarg>-frontend</extraarg>
<extraarg>jaxws21</extraarg>
<extraarg>-xjc-XhashCode</extraarg>
<extraarg>-xjc-Xequals</extraarg>
</extraargs>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>0.6.4</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>${maven-clean-plugin.version}</version>
<configuration>
<filesets>
<fileset>
<directory>src/main/gen</directory>
<includes>
<include>**/*.*</include>
</includes>
</fileset>
</filesets>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>${build-helper-maven-plugin.version}</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/main/gen</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>ts-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>corporate-tools.fragmental.security</groupId>
<artifactId>basic-ws-client</artifactId>
<version>${fragmental.version}</version>
</dependency>
<!-- JEE -->
<dependency>
<groupId>javax.j2ee</groupId>
<artifactId>j2ee</artifactId>
<version>1.4</version>
<scope>provided</scope>
</dependency>
<!-- JAX-WS -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>${jaxws.version}</version>
</dependency>
<dependency>
<groupId>javax.xml.ws</groupId>
<artifactId>jaxws-api</artifactId>
<version>${jaxws.version}</version>
<exclusions>
<exclusion>
<groupId>javax.xml.soap</groupId>
<artifactId>saaj-api</artifactId>
</exclusion>
<exclusion>
<artifactId>jsr250-api</artifactId>
<groupId>javax.annotation</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
The release plugin has a kinda strange (for me ;)) usage with special properties and forwarding them to the real goal executed. If you use normal profile argument -Pprod this would work. However most other arguments you add at mvn call are ignored. So could you try the following one:
mvn release:prepare -B release:perform -Darguments="-Denv=prd"
I believe you are using Maven 2.0.x right?
If I remember correctly, property overriding in profile is not working fine at or before Maven 2.0.7. Upgrade to latest 2.0.x (2.0.11) or even 2.2.x/3.0.x will work as you expected.
However, a bit off topic, I believe you are accessing the server to get the WSDL right? I don't think it is a good idea especially for release build, because it is making the build non-reproducible. Consider putting the WSDL with the source code (at least for release build) to make build reproducible.
I fixed my problem creating two profiles, assuming that first has de dafault activation = true and set my server.address to localhost.
The second profile that set server.address to prd server is called by command line directly by -P prd
So, when I execute with no arguments, the default profile sets the server.address to localhost:8080 and when I execute the release, I use -P prd and the release works fine.
thank you for your answers.

Resources