When I Eclipse project to maven, Why can't I export a .jar without "Fat Jar Export: Could not find class-path for ...target/test-classes - export

I had a working Eclipse project for which I could export a .jar file with no problems.
I then did a 'configure/convert to maven'. Now, when I try to export the .jar file I get the error message:
Fat Jar Export: Could not find class-path entry for 'Users/ward/dropbox/workspaces/newautomation/workspace/automation/target/test-classes'.
Although it does write the .jar file, the .jar file does not work properly.
Any help?
ward

As stated in my question, I had a project running in Eclipse and could successfully 'export a runnable jar' using Eclipse. That allowed me to distribute my project as a self contained '.jar' and run it on other systems with a simple:
java -jar project.jar
I am using Eclipse 4.3.0.
I decided I wanted to covert the project to a 'maven project' and did so using eclipse 'project/configure/convert to maven'.
I then found that my old mechanism for creating a .jar no longer worked.
Casting about the internet I finally found the information I needed. The first bit of information was that I needed to create what was called a 'Fat Jar'. When using maven, one creates a 'Fat Jar' by running maven, NOT using the export function in Eclipse. To create a 'Fat Jar', one needs to click on 'Project/Run As' and select 'maven build'. That will get you to a screen where you specify 'Goals'. Ultimately, for me, the goals were "compile assembly:single".
Doing this for the first time will result in warnings and errors. The most important error is:
Failed to execute goal org.apache.maven.plugins:maven-assembly-plugin:2.2-beta-5:single
This is because the 'pom.xml' file created by the 'convert' process is incomplete. After several frustrating hours, here is what I finally made work. Again, to create the 'Fat Jar', I do:
project/Run As/Maven build
with goals: compile assembly:single
My 'pom.xml' is:
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>example</groupId>
<artifactId>example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>HelloWorld</name>
<description>Hello World Example</description>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>17</release>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<manifest>
<mainClass>example.HelloWorld</mainClass>
</manifest>
</archive>
<finalName>helloWorld</finalName>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
</project>
Final notes:
The section concerning 'UTF-8' is not strictly needed, it just gets rid of an annoying warning.
There may be other 'unneeded' lines in the 'pom.xml'.
The 'compile' may be unnecessary but I found that when editing the pom.xml file I often got rid of Eclipse syntax type errors by running 'compile' and so I just do it as a matter of course now.

Related

Maven Toolchain config for jdk17 not pickedup by surefire plugin 3.0.0.M7

Trying to use maven toolchains to compile a Java 17 project while my active jdk is JDK8. I set up my toolchains so they use JDK17 to compile it, and this compilation fails while running tests with error:
Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:3.0.0-M7:test failed: java.lang.UnsupportedClassVersionError: my/source/ControllerTest has been compiled by a more recent version of the Java Runtime (class file version 61.0), this version of the Java Runtime only recognizes class file versions up to 52.0.
My pom tooLchain config fr compiler and surefire plugin(version M7):
<properties>
.....
<toolchain>17</toolchain>
<vendor>oracle</vendor>
</properties>
....
....
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-toolchains-plugin</artifactId>
<configuration>
<jdkToolchain>
<version>${toolchain}</version>
<vendor>${vendor}</vendor>
</jdkToolchain>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<jdkToolchain>
<version>${toolchain}</version>
<vendor>${vendor}</vendor>
</jdkToolchain>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<jdkToolchain>
<version>${toolchain}</version>
<vendor>${vendor}</vendor>
</jdkToolchain>
<argLine>#{surefireArgLine}</argLine>
<forkCount>1</forkCount>
<reuseForks>false</reuseForks>
<argLine>-Xmx1024m -XX:MaxPermSize=256m</argLine>
<includes>
<include>**/**Test.java</include>
</includes>
</config>
why isn't my toolchain config getting picked up by surefire. Compilation itself is fine, but surefire test phase fails. Appreciate your help
I was able to get toolchains working if i trigger tests with "mvn test" on surefire plugin 2.22. It doesn't pick up the toolchains though if the tests are triggered with surefire:test.
I have been trying to make it work on surefire 3 but i keep running into the same issue that you posted about. Please let me know as well if you found a way to make this work with surefire 3.

"mvn allure:serve" generates report to an temp folder

I was going through the io.qameta.allure documentation and saw this option to generate allure report using "mvn allure:serve"
mvn allure:serve
The report will be generated into a temp folder. The web server with results will start. You can additionally configure the server timeout
regarding the below lines, can we modify in the configuration to generate reports under the particulate folder instead of the temp folder?
do anyone have information about it using this command only?
You can do it by defining a configuration > reportDirectory for allure-maven in your pom.xml file.
Example: in a java/selenium/testng project, here is the reporting section of pom.xml file:
<reporting>
<excludeDefaults>true</excludeDefaults>
<plugins>
<plugin>
<groupId>io.qameta.allure</groupId>
<artifactId>allure-maven</artifactId>
<version>2.10.0</version>
<configuration>
<reportVersion>${allure.version}</reportVersion>
<reportDirectory>allure-report/</reportDirectory>
</configuration>
</plugin>
</plugins>
</reporting>
In order to generate allure reports you need to use: mvn allure:report.
To specify the path of the report generated use the allure maven plugin and in the pom.xml file use this:
<configuration>
<reportVersion>2.14.0</reportVersion>
<resultsDirectory>*allure result path*</resultsDirectory>
<reportDirectory>*allure report path*</reportDirectory>
</configuration>
The following command: mvn allure:serve always uses a temp folder on your device and I haven't seen anywhere in their documentation the ability of defining the path when using the serve command.

Error resolving version for plugin 'org.apache.maven.plugins:maven-jar-plugin' from the repositories ,Plugin not found in any plugin repository

I am trying to Maven build my Java project but it is getting failed and i am getting below error:
Error resolving version for plugin 'org.apache.maven.plugins:maven-jar-plugin' from the repositories [local (C:\Users\Vinita.Gupta.m2\repository), central (https://repo.maven.apache.org/maven2)]: Plugin not found in any plugin repository -> [Help 1]
I am trying to build the project in Eclipse Neon and have installed and setup Maven path,m2connector for Eclipse.
Note: I am not connected to network using any proxy
Most of the solutions which i have found online were having issues due to proxy settings but i am directly connected to network and able to browse below directory via browser but couldn't connect through Maven:
https://repo.maven.apache.org/maven2
Please help !!
I had the same error, but for the maven-failsafe-plugin.
With the help of a colleague, we got rid of the problem adding the version line to the plugin declaration in the project pom.xml. We added the <version>2.20.1</version> line resulting in the following plugin declaration.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.20.1</version>
<configuration>
<skipTests>false</skipTests>
<argLine>-Djava.library.path=${project.parent.basedir}/lib/${arquitecturaMaquina}</argLine>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
It is a long working project (over a year) and never had this error. I do not know what triggered it, but all the other answers about resetting the local repository, checking the proxy, etc did not work.
Then we added that version line to the pom.xml and the project built correctly again. Now I removed the line and it is still working. My guess is that it indeed had something to do with some messed up cache of some kind, and that requiring the specific version got rid of the mess and now it is working as before the problem appeared.
Add this to your pom.xml and it should work
<pluginRepositories>
<pluginRepository>
<id>maven2</id>
<url>https://repo.maven.apache.org/maven2/</url>
</pluginRepository>
</pluginRepositories>
I had a weird solution. I got the same error when i was connected in Office LAN. Once I switched to common wifi, the error was not thrown. So i believe this is more to do with the network restriction.
Spent a day working on it, finally, the solution was to change the URL in settings.xml of a .m2 directory
old line was:
<url>http://blah:8081/bla/blabla/</url>
However, for a reason unknown to me, and from my machine only, the URL was getting rejected. As a result, I was getting the error mentioned in a title... Therefore, the solution was to put the full URL such as:
<url>http://blah.org.blah:8081/bla/blabla/</url>
-Then, simply re-open IDE (if using it), update the project (build, clean or whatever else is necessary to refresh).
You may also need to specify <port> or to add the proxy settings, but none of that was required in my case. Therefore, the solution is about adjusting the settings.xml. I have tried adding the version numbers as suggested and it was a partial solution. Cleaning and updating the project (without the settings.xml change) did nothing. Removing *.lastUpdated from .m2 was not a solution either.
What worked for me was to add a Proxy Bypass for repo.maven.apache.org under ‘Window’ > ‘Preferences’ > ‘General’ > ‘Network connections’.
After that I ran into compilation errors related to certain classes not being resolved. Just did a clean and build, viola Build Successful!
Check settings.xml file is available in C:\Users\username.m2\settings.xml and change below
C:/Users/username/.m2/repository
Its worked for me.
Added this to pom.xml and update project.
Worked for me.
<build>
<finalName>spring-security-demo</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>

Unable to invoke SureFire plugin to run a Java Test

I have created a Java test (i can convert it to Junit as well if required). I want it to invoke when the project build. I added the dependency in the Pom.xml but on building the project, the test is not triggered. I think the Surefire plugin is not invoked in the first place because i can not see any reports in the target folder.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<includes>
<include>com.configtests/ConfigTests.java</include>
</includes>
</configuration>
</plugin>
Generally, you have this kind of structure:
Module
-- src
-- main
-- java
-- com.module
-- (classes)
-- test
-- java
-- com.module
-- (test classes)
-- pom.xml
Then the plugin syntax looks like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<includes>
<include>*</include>
</includes>
</configuration>
</plugin>
I assume you want to have ConfigTests.java under the package you have named com.configtests in the test directory. Should suffice to just do:
<include>ConfigTests.java</include>
No reason to declare the package prepended. It should find the class properly so long as you have the pom.xml at the top-level and the class under a folder in the same directory accordingly (in the test folder!)
EDIT:
Also, to run the tests as part of your build it should be running the tests auto-magically with: mvn clean install or, you can run the tests isolated like mvn test. Output in the console will indicate if the tests were run or not.

xmlbeans classes created by maven are not placed in the right package structure

I'm using xmlbeans to generate some java classes. I'm using maven 3 in my project.
I have included the dependency and the plugin details as shown below.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>xmlbeans-maven-plugin</artifactId>
<version>2.3.3</version>
<executions>
<execution>
<goals>
<goal>xmlbeans</goal>
</goals>
</execution>
</executions>
<inherited>true</inherited>
<configuration>
<schemaDirectory>src/main/xsd</schemaDirectory>
</configuration>
</plugin>
When I run a clean package goal I get compiler errors simply because the classes which uses the xmlbeans generated reference are not found in the classpath. This is because the xmlbeans classes are not compiled into the right directory structure. rather it just places it in the target\classes\aseXMLR30 (where as it should be in the formal structure ex: com.ex.first)
I have googled and read many blogs nothing helped me so far!
any replies/answers are really appreciated!
I know this question is 4 months old but in case you and/or others are still looking for a solution, this blog post may be able to provide a clue.
Essentially, what it says is that all references to org.maven.ide.eclipse should be replaced by org.eclipse.m2e. It references .launch files but to be on the safer side just grep recursively through your Eclipse directory and projects.

Resources