How to run jar file of a maven project generated after build? - java

I made a simple project with a few tests in it and I want to be able to launch tests on other computers. I built the project using Run As -> Maven build... -> Goals: package in Eclipse, and I found a jar file in target folder of the project. But when I try to run it in cmd using java -jar project.jar I get the following error:
Error: Main method not found in class com.example.TestPurchase, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
As far as I know, TestNG doesn't need any Main method, because of the annotations.
That brings me to some questions:
Is something wrong with how I build my project?
Did I understand the method of executing tests via jar file correct?
Do I even have to use that jar file? Because I can run tests from command line being in project folder using mvn test
Here is my pom.xml file:
<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>com.example</groupId>
<artifactId>Sightsy</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<selenium.version>3.12.0</selenium.version>
<testng.version>6.13.1</testng.version>
<javafaker.version>0.14</javafaker.version>
<guava.version>23.2-jre</guava.version>
<extentreports.version>3.0.7</extentreports.version>
<extenttestng.version>1.3.1</extenttestng.version>
<assertj.version>3.8.0</assertj.version>
<maven.compiler.version>3.7.0</maven.compiler.version>
<commons.version>3.7</commons.version>
<commons.io.version>2.6</commons.io.version>
</properties>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>${selenium.version}</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>${testng.version}</version>
</dependency>
<dependency>
<groupId>com.github.javafaker</groupId>
<artifactId>javafaker</artifactId>
<version>${javafaker.version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<dependency>
<groupId>com.aventstack</groupId>
<artifactId>extentreports</artifactId>
<version>${extentreports.version}</version>
</dependency>
<dependency>
<groupId>com.vimalselvam</groupId>
<artifactId>testng-extentsreport</artifactId>
<version>${extenttestng.version}</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons.io.version}</version>
</dependency>
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>3.4</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.example.TestPurchase</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/main/resources/suites/testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>

I'm not familiar with TestNG framework, but my understanding is as following:
When "mvn package" is executed, your project is "validated", "compiled" and then "packed". During compilation, your code is changed to bytecode and can be interpreted (executed) by java virtual machine (JVM). This compiled code has only your application (I assume, that this is an application that is executed on Java server).
During the "package" step, these compiled classes (without tests) are put together in jar file. This jar file should contain only your application (again, without tests) - this is what you want because your jar file is smaller and contain only what is really needed.
Now when "mvn test" is executed, classes with test code are compiled and then executed. TestNG add the main function "automatically" so JVM knows what to do. Please note that running again "mvn package" will still NOT include these tests in jar file.
To address your questions directly:
Ad 1. No
Ad 2. Yes, "mvn test" is the correct way.
Ad 3. No, you should not use jar file during testing. It is used later during deployment.

First of all, you must have a separate class with "main" method, where you will specify your xml suite files.
package com.example;
import java.util.ArrayList;
import java.util.List;
import org.testng.TestNG;
public class MainClass {
public static void main(String[] args) {
TestNG testSuite = new TestNG();
List<String> suites = new ArrayList<String>();
suites.add("path_to_your_xml_suite_file_in_target_folder");
testSuite.setTestSuites(suites);
testSuite.run();
}
}
Then, you will need to specify this class with "main" method in configuration of maven-shade-plugin in pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.example.TestNGMainClass</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
Then, when you will execute mvn build package without performing tests, you will have a fully working executable jar file in target folder, which you can launch in command line using java -jar name_of_you_jar_file.jar. It will need drivers and xml suite files to work

Related

How to build standalone executable cucumber jar file?

I want to create a executable jar file so anyone can run it from their computer with least install require components.
I found several tutorial but none of them a work.
When I execute jar file I've built they are return error like:
Error: Could not find or load main class fully.qualified.MainClass
Caused by: java.lang.ClassNotFoundException: fully.qualified.MainClass
OR like this:
Error: Could not find or load main class io.cucumber.core.cli.Main
Caused by: java.lang.ClassNotFoundException: io.cucumber.core.cli.Main
My project run from Intellij with no problem.
Here my project structure
https://i.stack.imgur.com/NcQzf.png
And my pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>com.example</groupId>
<artifactId>CucumberSelenium</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<cucumber.version>7.6.0</cucumber.version>
<selenium.version>4.8.0</selenium.version>
<webdrivermanager.version>5.2.1</webdrivermanager.version>
<junit.jupiter.version>5.9.0</junit.jupiter.version>
<apache.common.version>2.4</apache.common.version>
<projectlombok.version>1.18.24</projectlombok.version>
<maven.compiler.plugin.version>3.10.1</maven.compiler.plugin.version>
<maven.surefire.plugin.version>3.0.0-M7</maven.surefire.plugin.version>
<maven.compiler.source>19</maven.compiler.source>
<maven.compiler.target>19</maven.compiler.target>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-bom</artifactId>
<version>${cucumber.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>${junit.jupiter.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit-platform-engine</artifactId>
<scope>test</scope>
</dependency>
<!-- JUnit Platform -->
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<!-- Selenium -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>${selenium.version}</version>
</dependency>
<!-- Web Driver Manager -->
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>${webdrivermanager.version}</version>
</dependency>
<!-- Apache Common -->
<dependency>
<groupId>org.apache.directory.studio</groupId>
<artifactId>org.apache.commons.io</artifactId>
<version>${apache.common.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${projectlombok.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.4.2</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin.version}</version>
<configuration>
<source>19</source>
<target>19</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.surefire.plugin.version}</version>
<configuration>
<properties>
<configurationParameters>
cucumber.junit-platform.naming-strategy=long
</configurationParameters>
</properties>
<debugForkedProcess>true</debugForkedProcess>
<forkCount>0</forkCount>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.4.2</version>
<configuration>
<archive>
<manifest>
<mainClass>io.cucumber.core.cli.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>assemble-all</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
I've run mvn clean compile assembly:single, it's output a CucumberSelenium-1.0-SNAPSHOT-jar-with-dependencies.jar file, but it wont runs.
I just came across a similar issue and, despite trying the ubiquitous
java -cp . org.example.Main
(while on current directory being where the Main.class is), I kept getting this dreaded
"Error: Could not find or load main class"
I eventually resorted to comparing the actual command with parameters invoked by IntelliJ (on IntelliJ IDEA's debug log) with mine, and discovered that the following solves the issue:
java -cp C:\Users\WebViwer\IdeaProjects\MyProj\target\classes org.example.Main
I am guessing that once the fully qualified class name is specified, the current directory (where the class resides) is no longer valid as a classpath: Only the top level classes directory should be specified (in this org.example, 2 levels up).
You have a a few problems going on.
The maven-assembly-plugin should not be a in the dependencies section. It is not a dependency used by the runtime code of your project.
Your step definitions, feature files and glue code located in src/test are not included in the jar file build by the assembly plugin.
Your test scoped dependencies will also not be included by the assembly plugin either.
You must configure the containerDescriptorHandler of the assembly plugin with metaInf-services or the plugin will not merge files in META-INF/services correctly.
You can verify most of these by opening the generated jar file (it's a .zip file in disguise).
My project run from Intellij with no problem.
When running tests in Intelij you are using the test scope, the jar file only includes runtime scoped code.
For a more comparable test you must create a new run configuration that invokes the main method.
I found several tutorial but none of them a work.
It appears that you are relatively new to Java and Maven. It would be prudent to follow a proper course first rather than tutorials. You are missing fundamental knowledge that is generally not taught in tutorials.
I want to create a executable jar file so anyone can run it from their computer with least install require components.
It's also prudent to consider why you are doing this.
Tests will typically change as quickly as the source code they are testing does. Manually distribution of jar files won't keep up with this.
This means it's generally better to integrate the tests and the test source code into the automated build pipeline of the project they test.
This also means that you don't need to distribute your tests as a jar. Rather you should expect (and possibly train) people to use Maven, GIT and Java.

EXE using launch4j does not work (javafx project), JAR files need to be ran from the console specifying the VM argument of the javafx location

created a very simple gui with javafx trying to test out how creating a javafx exe would work, extracted a runnable jar file from eclipse but that doesnt run unless i use the cmd and add the VM argument --module-path "" --add-modules=javafx.controls,javafx.fxml, so i tried to make an exe from that jar file using launch4j, it would redirect me to java.com/downloads so i embedded the JRE bin and lib folders with the exe file now it does not run at all.
Am i missing something or doing something wrong?
Wrapping into native container:
For Wrapping your application into an EXE or DEB, or..., you should not use launch4j, but "jpackage". It is the official tool from oracle for wrapping JARs into EXEs or DEBs, .. and it is included in JDK 17 LTS. It works like a charm. Please note: Your projects don't have to use JDK 17, they can use JDK 11 as well.
General about JFX:
I can't tell you why, but JFX does NOT like it, if the class, which contains the main() extends from another class. It took me hours to find that out. It may sound stupid, uneccessary or similiar, but I can only advise you to do this workaround:
Mainclass:
package com.wedasoft.FxMultiMessageSender;
public class FxMultiMessageSenderMain {
public static void main(String[] args) {
App.main(args);
}
}
JFX-Application-Starting-Class:
package com.wedasoft.FxMultiMessageSender;
public class App extends Application {
public static final String APPLICATION_NAME = "xxxxxxxx";
private Stage primaryStage;
private Scene primaryScene;
public static void main(String[] args) {
//createDatabase();
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
System.out.println("Starting " + APPLICATION_NAME);
}
}
Not really working JARs:
Your question sounds like you are using a normal Java project and embedded the JFX libraries. If so, I can only advise you to use Maven or Gradle for at least JFX projects; you won't get happy without one of them.
If you use Maven, then try to integrated your pom.xml with the following pom.xml. You do not need to specify any VM arguments. You need to adjust the main class <mainClass>com.wedasoft.FxMultiMessageSender.FxMultiMessageSenderMain</mainClass> in the javafx-maven-plugin and in the maven-shade-plugin.
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wedasoft</groupId>
<artifactId>FxMultiMessageSender</artifactId>
<version>0.0.2</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<javafx.version>17</javafx.version>
</properties>
<dependencies>
<!-- javafx -->
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>${javafx.version}</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>${javafx.version}</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-media</artifactId>
<version>${javafx.version}</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-swing</artifactId>
<version>${javafx.version}</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-web</artifactId>
<version>${javafx.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.8</version>
<executions>
<execution>
<!-- Default configuration for running -->
<!-- Usage: mvn clean javafx:run -->
<id>default-cli</id>
<configuration>
<mainClass>com.wedasoft.FxMultiMessageSender.FxMultiMessageSenderMain</mainClass>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.wedasoft.FxMultiMessageSender.FxMultiMessageSenderMain</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

How to package dependency JARs as part of project JAR using maven

I have 2 maven projects
1) My Utils Project - imports datecalc-common as a dependency, contains my custom utility classes. Note that it doesn't have a main class.
2) My Main Project - imports 'My Utils Project' as a dependency.
'My Main Project' shows the error 'Cannot find class HolidayCalculator'.
My Utils Project-pom.xml is given below.
How do I modify it so that the dependency jars are included as part of 'My Utils Project'.jar ?
<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>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<groupId>com.mycompany.mycommoncode</groupId>
<artifactId>myutilsjar</artifactId>
<version>0.0.12-SNAPSHOT</version>
<name>myutilsjar</name>
<description>Common Date Utils used by my classes</description>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<dependency>
<groupId>net.objectlab.kit</groupId>
<artifactId>datecalc-common</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>net.objectlab.kit</groupId>
<artifactId>datecalc-jdk8</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.uuid</groupId>
<artifactId>java-uuid-generator</artifactId>
<version>3.1.4</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
</dependencies>
<distributionManagement>
<snapshotRepository>
<id>snapshots</id>
<url>my maven url</url>
</snapshotRepository>
</distributionManagement>
</project>
NOTE: I DO NOT want to use 'shade' plugin. Is there any other way of doing this?
I had to solve that same issue some months ago. I wrote a small library that was to be shared between many projects and I wanted to add it to maven.
This is what I did.
For your "Utils project"
Compile it using maven-assembly-plugin:
Add this to your pom.xml:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>attached</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Build the library with it's dependencies using the following command:
mvn clean compile assembly:attached
For your main project
Add this plugin to your pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
<executions>
<execution>
<id>install-external</id>
<phase>clean</phase>
<configuration>
<file>${project.basedir}/src/main/resources/lib/sso-client-1.0.jar</file>
<repositoryLayout>default</repositoryLayout>
<groupId>SSOClient</groupId>
<artifactId>sso-client</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<generatePom>true</generatePom>
</configuration>
<goals>
<goal>install-file</goal>
</goals>
</execution>
</executions>
</plugin>
As you can see, I wrote a custom SSO Client.
This will read the jar you compiled in the previous step, and make it available as a dependency. I decided to place it in the resources folder of the main project. The version has to match the one in your "utils project".
Also add the dependency to the pom.xml:
<dependency>
<groupId>SSOClient</groupId>
<artifactId>sso-client</artifactId>
<version>1.0</version>
</dependency>
That's it! You can now reference the classes of the package in your main project.
Let me know if you need clarification.
The Apache Maven Shade Plugin provides the capability to package your artifact in a "uber-jar", that is to say a jar including all dependencies required to run the project. If needed this plugin also supports packages shading (renaming) for some of the dependencies.
More info at:
https://maven.apache.org/plugins/maven-shade-plugin/examples/includes-excludes.html#

How to run junit tests on a non java project

I have a project folder but it is not a java project. It is a maven project. I have written a junit test and it runs perfectly when running in the eclipse IDE but when I run the maven command mvn install, it seems to skip my junit tests. I have included the test file in src/test/java/ (the name of my test is AppTest.java) and the main .java file (with the main method) is in src/main/java/. I have noticed that the project I am currently working on is a maven project and not a maven java project. I have included a screen of my current folder structure:
folder structure
Maven test output <- should not build as I have a deliberate test that should fail
This is the POM. I have deleted/commented out some sensitive parts so the pom file may be syntactically wrong but the main plugins I use are there; tap4j, junit and surefire.
<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>integration-api-parent</artifactId>
<groupId>uk.gov.dwp</groupId>
<version>1.0.2</version>
</parent>
<artifactId>aa</artifactId>
<version>1.0.6</version>
<packaging>pom</packaging>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>aa</finalName>
<plugins>
<!-- plugin>
<groupId>com.github.fracpete</groupId>
<artifactId>latex-maven-plugin</artifactId>
<configuration>
<forceBuild>true</forceBuild>
</configuration>
</plugin>
<plugin>
<groupId>com.github.fracpete</groupId>
<artifactId>latex-maven-plugin</artifactId>
<configuration>
<forceBuild>true</forceBuild>
</configuration>
</plugin-->
<plugin>
<!-- Plug-in utilised for the execution of the JMeter Integration Tests -->
<!-- These tests are executed against the nominated integration server where as -->
<!-- instance of AA exists -->
<groupId>com.lazerycode.jmeter</groupId>
<artifactId>jmeter-maven-plugin</artifactId>
<version>1.9.0</version>
<executions>
<execution>
<id>jmeter-tests</id>
<phase>verify</phase>
<goals>
<goal>jmeter</goal>
</goals>
</execution>
</executions>
<configuration>
<ignoreResultErrors>false</ignoreResultErrors>
<suppressJMeterOutput>false</suppressJMeterOutput>
<overrideRootLogLevel>INFO</overrideRootLogLevel>
</configuration>
</plugin>
<plugin>
<!-- Step to copy the latest plug-ins that form this build to the integration server -->
<!-- This is done using the SCP command via the ANT plug-in thus allowing it to execute on all platforms -->
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<dependency>
<groupId>ant</groupId>
<artifactId>ant-jsch</artifactId>
<version>1.6.5</version>
</dependency>
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.42</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.tap4j/tap4j -->
<dependency>
<groupId>org.tap4j</groupId>
<artifactId>tap4j</artifactId>
<version>4.2.1</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
</plugin>
<!-- plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>src/main/assembly/cassandra-assembly.xml</descriptor>
<descriptor>src/main/assembly/devenv-assembly.xml</descriptor>
<descriptor>src/main/assembly/main-assembly.xml</descriptor>
</descriptors>
</configuration>
</plugin-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
</plugin>
<!-- plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>templating-maven-plugin</artifactId>
<configuration>
<skipPoms>false</skipPoms>
<sourceDirectory>${project.basedir}/src/main/latex-templates</sourceDirectory>
<outputDirectory>${project.build.directory}/latex</outputDirectory>
</configuration>
</plugin-->
</plugins>
</build>
</project>
AppTest:
package AccessGateway;
import static org.junit.Assert.*;
import java.io.File;
import org.junit.Test;
import org.tap4j.consumer.TapConsumer;
import org.tap4j.consumer.TapConsumerFactory;
import org.tap4j.model.TestSet;
public class AppTest {
Practise prac;
final String DIRECTORY = "C:\\Users\\Hello\\Desktop\\";
#Test
public void testHeaderProcessor() {
prac = new Practise();
assertFalse(prac.runTest(new File(DIRECTORY+"TAPHeaderProcessor.txt")));
}
#Test
public void testHeaderPortForward() {
prac = new Practise();
assertFalse(prac.runTest(new File(DIRECTORY+"TAPHeaderPortForward.txt")));
}
#Test
public void catunittest() {
prac = new Practise();
assertFalse(prac.runTest(new File(DIRECTORY+"catunittest.txt")));
}
#Test
public void catunitcrowstest() {
prac = new Practise();
assertFalse(prac.runTest(new File(DIRECTORY+"catcrowd.txt")));
}
#Test
public void testCrowd() {
prac = new Practise();
assertFalse(
prac.runTest(new File(DIRECTORY+"TAPCrowd.txt")));
}
#Test
public void testADFS() {
prac = new Practise();
assertFalse(
prac.runTest(new File(DIRECTORY+"TAPADFSformat.txt")));
}
}
The problem is the packaging of your project which is pom
You can't execute Surefire on this kind of project.
Try adding surefire plugin. When i have tests in my app i always include it (works for junit as well as testng). Based on your logs i can see that you dont have it declared.
<plugins>
[...]
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>

Problems testing services created with maven-scr-plugin when using pax-exam

I've set up a very simple "HelloWorld" service to demonstrate my problem. It uses the maven-scr-plugin to generate a service descriptor and has a pax-exam unit test. But when I try to run 'mvn clean test' it blocks for a while before giving me this error:
org.ops4j.pax.swissbox.tracker.ServiceLookupException: gave up waiting for service com.liveops.examples.osgi.helloworld.HelloWorldService
If I run 'mvn -DskipTests=true package' and then run 'mvn test' (without clean)
it works. The difference seems to be the addition of this line in my META-INF/M
ANIFEST.MF file:
Service-Component: OSGI-INF/com.liveops.examples.osgi.helloworld.internal.HelloImpl.xml
Does anyone know if there is a way to make sure this line is added earlier in the build process so that 'mvn clean test' will pass? Or is there something else I might be doing wrong?
For reference, here is the pom.xml, the service, and the unit test.
<?xml version="1.0" encoding="UTF-8"?>
<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>com.liveops.examples</groupId>
<artifactId>HelloWorldService</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>bundle</packaging>
<dependencies>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>4.3.1</version>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<dependency>
<groupId>org.ops4j.pax.exam</groupId>
<artifactId>pax-exam</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.ops4j.pax.exam</groupId>
<artifactId>pax-exam-container-native</artifactId>
<version>3.3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.ops4j.pax.exam</groupId>
<artifactId>pax-exam-junit4</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.ops4j.pax.url</groupId>
<artifactId>pax-url-aether</artifactId>
<version>1.6.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.ops4j.pax.exam</groupId>
<artifactId>pax-exam-link-mvn</artifactId>
<version>3.3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.5.8</version>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.framework</artifactId>
<version>4.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.scr.annotations</artifactId>
<version>1.9.0</version>
</dependency>
</dependencies>
<properties>
<namespace>com.liveops.examples.osgi.helloworld</namespace>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<!--
| the following instructions build a simple set of public/private classes into an OSGi bundle
-->
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.3.7</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${project.name}</Bundle-SymbolicName>
<Bundle-Version>${project.version}</Bundle-Version>
<!-- Bundle-Activator>${namespace}.internal.HelloActivator</Bundle-Activator -->
<!--
| assume public classes are in the top package, and private classes are under ".internal"
-->
<Export-Package>!${namespace}.internal.*,${namespace}.*;version="${project.version}"</Export-Package>
<Private-Package>${namespace}.internal.*</Private-Package>
<!--
| each module can override these defaults in their osgi.bnd file
-->
<!--_include>-osgi.bnd</_include-->
</instructions>
</configuration>
<executions>
<execution>
<id>generate-manifest</id>
<phase>process-classes</phase>
<goals>
<goal>manifest</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-scr-plugin</artifactId>
<version>1.9.0</version>
<configuration>
<supportedProjectTypes>
<supportedProjectType>jar</supportedProjectType>
<supportedProjectType>bundle</supportedProjectType>
<supportedProjectType>war</supportedProjectType>
</supportedProjectTypes>
<generateAccessors>true</generateAccessors>
<strictMode>true</strictMode>
<specVersion>1.1</specVersion>
<outputDirectory>target/classes</outputDirectory>
</configuration>
<executions>
<execution>
<id>generate-scr-scrdescriptor</id>
<goals>
<goal>scr</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
HelloWorld Implementation class
package com.liveops.examples.osgi.helloworld.internal;
import com.liveops.examples.osgi.helloworld.HelloWorldService;
import org.apache.felix.scr.annotations.Service;
import org.apache.felix.scr.annotations.Component;
#Component
#Service(HelloWorldService.class)
public class HelloImpl implements HelloWorldService
{
public String helloWorld(String personalization)
{
return "Hello " + personalization + "!";
}
}
The unit test
package com.liveops.examples.osgi.helloworld.internal;
import com.liveops.examples.osgi.helloworld.HelloWorldService;
import junit.framework.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.ops4j.pax.exam.Option;
import org.ops4j.pax.exam.Configuration;
import org.ops4j.pax.exam.junit.PaxExam;
import org.ops4j.pax.exam.util.PathUtils;
import javax.inject.Inject;
import static org.ops4j.pax.exam.CoreOptions.*;
#RunWith(PaxExam.class)
public class HelloImplTest
{
#Inject
HelloWorldService hws;
#Configuration
public static Option[] configuration() throws Exception{
return options(
systemProperty("org.ops4j.pax.logging.DefaultServiceLog.level").value("WARN"),
mavenBundle("org.apache.felix", "org.apache.felix.scr", "1.6.2"),
bundle("reference:file:" + PathUtils.getBaseDir() + "/target/classes"),
junitBundles());
}
#Test
public void testInjection()
{
Assert.assertNotNull(hws);
}
#Test
public void testHelloWorld() throws Exception
{
Assert.assertNotNull(hws);
Assert.assertEquals("Hello UnitTest!", hws.helloWorld("UnitTest"));
}
}
Use the ProbeBuilder to enhance your tested bundle:
#ProbeBuilder
public TestProbeBuilder probeConfiguration(TestProbeBuilder probe) {
probe.setHeader("Service-Component", "OSGI-INF/com.liveops.examples.osgi.helloworld.internal.HelloImpl.xml");
return probe;
}
This most likely is all that's missing.
EDIT:
just in case you're trying to use pax-exam in the same bundle you need to take certain actions in your configuration method:
streamBundle(bundle()
.add(SomceClass.class).add("OSGI-INF/com.liveops.examples.osgi.helloworld.internal.HelloImpl.xml", new File("src/main/resources/OSGI-INF/com.liveops.examples.osgi.helloworld.internal.HelloImpl.xml")
.toURL())
.set("Service-Component", "OSGI-INF/com.liveops.examples.osgi.helloworld.internal.HelloImpl.xml")
.build()).start()
a complete sample can be found at here
I may have a fix for this, though it seems a bit in-elegant. I can explicitly add the Service-Component to the maven-bundle-plugin section of the pom file.
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.3.7</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${project.name}</Bundle-SymbolicName>
<Bundle-Version>${project.version}</Bundle-Version>
<Export-Package>!${namespace}.internal.*,${namespace}.*;version="${project.version}"</Export-Package>
<Private-Package>${namespace}.internal.*</Private-Package>
<!--Explicitly add the components no that they can be found in the test phase -->
<Service-Component>OSGI-INF/com.liveops.examples.osgi.helloworld.internal.HelloImpl.xml</Service-Component>
</instructions>
</configuration>
<executions>
<execution>
<id>generate-manifest</id>
<phase>process-classes</phase>
<goals>
<goal>manifest</goal>
</goals>
</execution>
</executions>
</plugin>
Please let me know if anyone can think of a better way.
The Maven SCR Plugin only generates the service component descriptors but it does not include them in the manifest automatically.
So there's nothing inelegant about including a <Service-Component> instruction in the Maven Bundle Plugin configuration, that's just the documented usage.
Since the manifest header is missing, SCR does not register any services on behalf of your bundle, and that's why Pax Exam can't find the required service.

Categories