why is one dependency not packaged with the JAR in Maven project - maven

EDIT: see the edit below. This question still needs attention because changing the pom as suggested did not change the behavior even after a clean.
I have a Java Maven project with a plugin to produce a standalone JAR which has thus far worked perfectly. In the pom.xml,
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<mainClass>ptcs.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
I recently wanted to use the Fraction class from org.apache.commons.math3, so I did the following. In my code,
import org.apache.commons.math3.fraction.Fraction;
Adding a dependency to pom.xml,
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>3.6.1</version>
</dependency>
EDIT: originally test was in the pom, but I removed it. There was no change in the behavior. I have also cleaned the project.
I'm working in VS Code, which puts a little "run" button above public static void main. That run button works, but trying to run the JAR reports there is no class definition for Fraction. Also running mvn java:exec ... reports the same problem.
What am I doing wrong, and why did it work perfectly before adding this dependency?
The error I get when running the JAR is
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache
/commons/math3/fraction/Fraction
at ptcs.App.main(App.java:39)
Caused by: java.lang.ClassNotFoundException:
org.apache.commons.math3.fraction.Fraction
at
java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
at
java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLo
aders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 1 more

Thats because the dependency is marked for scope test only and that means the dependency is only needed in the test phase.
The build process to make the jar file exclude these files from the packaged jar file and also exclude that file when starting the application with mvn java:exec ....
To fix it simple remove <scope>test</scope>

Is the package in the jar contained? Moreover, did you use the maven-dependency-plugin?
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
</plugin>

Related

includeSystemScope Parameter in pom.xml not working

I'm tring to include a custom jar in my Spingboot application. In my case the additional jar contains a custom font for jasper Report.
This is my "system" decendency
<dependency>
<groupId>jasperFontOverrides</groupId>
<artifactId>jasperFontOverrides</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${basedir}/lib/MyriadPro.jar</systemPath>
</dependency>
...
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
</build>
The system package is visible while I debug my application in my IDE but when I'm done and I what to generate the package for production deploy
mvn install -DskipTests
My system package is not included the final jar.
Is there anything missing in my maven configuration?
If this is a multi-module project, you probably need to define the configuration section in the parent pom.
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
I have spent almost a day debugging this and it seems the configuration set in the child pom is not enough to get this to work. My guess is that the dependencies are calculated early in the build and the includeSystemScope option set in the child module is processed too late to be applied.

Spring Boot useTestClasspath throws CannotLoadBeanClassException and ClassNotFoundException

I have a maven project with a test class located at src/test/java/MyDevClass which is intended for development/testing purposes only. I would like to use it when I start spring-boot-maven-plugin with the command line mvn spring-boot:run.
So, my pom.xml contains:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- TODO: Will create a maven profile and have useTestClasspath only for development/testing -->
<useTestClasspath>true</useTestClasspath>
</configuration>
</plugin>
</plugins>
</build>
But, I get the following error:
org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [MyDevClass]
Caused by: java.lang.ClassNotFoundException: MyDevClass
Intriguing enough, I have another project using tomcat7-maven-plugin and it works fine:
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.0</version>
<configuration>
<useTestClasspath>true</useTestClasspath>
</configuration>
</plugin>
What I am missing?
Spring-boot maven plugin does not include current module's test sources (and resources) into classpath even if useTestClasspath is set to true.
I ran a forked execution with verbose logging (-X flag to Maven), and the plugin listed the forked JVM classpath. Test classes were not included.
If you look at the plugin sources (version 1.5.3 at time of writing), the flag useTestClasspath is only used in the addDependencies method.
I see two options as workarounds
Add the target/test-classes dir to the run classpath:
<directories>
<directory>${project.build.testOutputDirectory}</directory>
</directories>
For older plugin version use:
<folders>
<folder>${project.build.testOutputDirectory}</folder>
</folders>
The problem here is that the folder is added to the beginning of classpath, while test files are preferable at its end.
Create another Maven module with test classes and resources, and add it as a <scope>test</scope> dependency.

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.

I have been trying from a couple of days to resolve the following error but I am unable to resolve it :(
My module's pom.xml file 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>junitcategorizer</artifactId>
<groupId>com.topdesk.test.junitcategorizer</groupId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<artifactId>junitcategorizer.instrument</artifactId>
<name>JUnitCategorizer InstrumentationAgent</name>
<description>The agent used to instrument the called Java classes</description>
<dependencies>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm-commons</artifactId>
<version>4.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
<manifestEntries>
<Premain-Class>com.topdesk.junitcategorizer.instrument.InstrumentationAgent</Premain-Class>
<Agent-Class>com.topdesk.junitcategorizer.instrument.InstrumentationAgent</Agent-Class>
<Can-Redefine-Classes>true</Can-Redefine-Classes>
<Can-Retransform-Classes>true</Can-Retransform-Classes>
<Boot-Class-Path>${project.artifactId}-${project.version}.jar</Boot-Class-Path>
<Can-Set-Native-Method-Prefix>true</Can-Set-Native-Method-Prefix>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<includes>
<include>org.ow2.asm:*</include>
</includes>
</artifactSet>
<relocations>
<relocation>
<pattern>org.objectweb.asm</pattern>
<shadedPattern>org.shaded.asm</shadedPattern>
</relocation>
</relocations>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
I am getting the following error:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.
12:test (default-test) on project junitcategorizer.instrument: There are test failures.
[ERROR]
[ERROR] Please refer to D:\Masters\thesis related papers and tools\junitcategorizer\junitcategorizer.instrument\target\surefire-reports for the individual test results.
[ERROR] -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project junitcategorizer.instrument: There are test failures.
Please refer to D:\Masters\thesis related papers and tools\junitcategorizer\junitcategorizer.instrument\target\surefire-reports for the individual test results.
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:212)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:120)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:347)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:154)
at org.apache.maven.cli.MavenCli.execute(MavenCli.java:582)
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:214)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:158)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: org.apache.maven.plugin.MojoFailureException: There are test failures.
Please refer to D:\Masters\thesis related papers and tools\junitcategorizer\junitcategorizer.instrument\target\surefire-reports for the individual test results.
at org.apache.maven.plugin.surefire.SurefireHelper.reportExecution(SurefireHelper.java:83)
at org.apache.maven.plugin.surefire.SurefirePlugin.writeSummary(SurefirePlugin.java:673)
at org.apache.maven.plugin.surefire.SurefirePlugin.handleSummary(SurefirePlugin.java:647)
at org.apache.maven.plugin.surefire.AbstractSurefireMojo.executeAfterPreconditionsChecked(AbstractSurefireMojo.java:137)
at org.apache.maven.plugin.surefire.AbstractSurefireMojo.execute(AbstractSurefireMojo.java:98)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:132)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
... 19 more
[ERROR]
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
I tried the following but did not help:
mvn dependency::tree
mvn clean install -U or Right-click on "project" Go to "Maven" >> "Update"
Please help me!
This solved my issue.
It was 2.10 in my POM, just updated to 2.19.1 and refresh the POM
Add to your pom :
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
</plugin>
</plugins>
In your error code he didn't find surefire plugin so add it
This is what solves the problem:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
from Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.10:test
This happens when Maven tries to run your test cases while building the jar. You can simply skip running the test cases by adding -DskipTests at the end of your maven command.
Ex: mvn clean install -DskipTests
or
mvn clean package -DskipTests
Was facing the same issue multiple times and I have 2 solutions:
Solution 1:
Add surefire plugin reference to pom.xml. Watch that you have all nodes!
In my IDEs auto import version was missing!!!
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
</plugin>
</plugins>
Solution 2: My IDE added wrong import to the start of the file.
IDE added
import org.junit.Test;
I had to replace it with
import org.junit.jupiter.api.Test;
Try this it works!
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
<shutdown>kill</shutdown> <!-- Use it if required-->
</configuration>
</plugin>
It worked for me with version 3.0.0-M1.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M1</version>
</plugin>
You might need to run it with sudo.
This is a kind of test failure.#SpringBootApplication annotation contains these configurations.
1) #Configuration
2) #ComponentScan
3) #EnableAutoConfiguration
#EnableAutoConfiguration is the reason for this error. This will try to automatically configure application according to dependencies in your pom.xml
As a example when you have spring-data-jpa dependency in pom it will try to add configuration to application by looking at application.properties file for data source. So you need add data source to solve that.
For MySQL :
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://localhost/lahiru
spring.datasource.username=root
spring.datasource.password=
Or
You could hide this by skipping testing.
mvn install -DskipTests
For more details.
I get exactly the same stacktrace when tests fail.
More to the top you should see messages identfying the failing test classes.
Or go to
D:\Masters\thesis related papers and tools\junitcategorizer\junitcategorizer.instrument\target\surefire-reports
and have a look at the failure reports.
Fix the problems and your build is ok.
Good news : Your poms seem to be ok, Maven can compile and execute tests.
Here is the simplest way to resolve this error:
1) Go to your pom.xml file path
2) And edit the pom.xml like:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
</plugin>
</plugins>
3) Save the file
That's it.
I have the same problem and try almost all these suggestions and another options but nothing works for my case.
So I had run verbose command: (mvn install -X) and saw the problem with Jacoco lib, and finally found this answer that works for me.
Change the Jacoco lib version in my pom file.
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.4</version>
If you skip tests by command or Intellij maven tool it will work, but its not solution. As solution simply add following plugin.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
</plugin>
Check your test files. You must clear all test files error on your project and try Clean and build.
If you are using ubantu then try to run MVN with sudo.
I got same error for
mvn -Dtest=PerfLatency#latencyTest test -X
But
sudo mvn -Dtest=PerfLatency#latencyTest test -X
Solved my problem
Make sure the name of the class created in the package is something like somethingTest.java
Maven only picks the java files ending with Test notation.
I was getting the same error and resolving the names of all my classes by adding 'Test' at the end made it work.
I was facing the same problem and how i resolved see below steps or Image:
Clicked on Windows menu item of eclipse
Clicked on preferences
select Installed JREs
Add your installed jdk's path(JRE home: C:\Program Files\Java\jdk1.8.0_201\jre`)
Add JRE name: jdk1.8.0_201
Clicked on Finish
Apply changes and close
enter image description here
Change the version number to 2.19.1 works for me :)
`<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<systemPropertyVariables>
<xmlOutputDir>${project.build.directory}/surefire</xmlOutputDir>
</systemPropertyVariables>
</configuration>
</plugin>`
after a few days of wondering, my solution was that I forgot to use #Test annotation before the #BeforeEach
My situation might be a bit different - but I was using Java 11 instead of Java 8.
I was facing the same kind of issue, your version numbers in the dependency of Selenium, TestNG, Junit should the same that you have used in your project. For example, in your project you are using the Selenium version 3.8. This version number should be mentioned in the dependency.
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.0.0-beta1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.8.1</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8</version>
<scope>test</scope>
</dependency>
</dependencies>
3 years already, but the same thing happened to me and I wanted to contribute with my case. Using the #nikk solution I got better results, but other errors still appeared, although yes, I was allowed to package.
However, my task was to implement the code of a co-worker on the server and it did not happen to him in his test environment, so I decided to investigate a little more since the code was functional and did not correspond to me touching anything .
In the end it turned out that his application inserted mysql tables into a database that did not exist. The solution was as easy as creating such a database and the error disappeared.
This issue could be related to the already busy port. Surefire run on 5005 port. So you need to make sure that this port is free. If not change it or kill the process. This happens in Intellij some time.
This error is brought up for many different reasons so everyone has a different solution. However, your own webapp should hold the answer for it.
You can look it up in
/module_with_failure/target/surefire-reports/*.txt
It may be better to set the surefire-plugin version in the parent pom, otherwise including it as a dependency will override any configuration (includes file patterns etc) that may be inherited, e.g. from Spring Boots spring-boot-starter-test pom using pluginManagement
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
I just adjust the cucumber version, because I had a JUnit dependency in a low version compared with the other version of cucumber in my pom file.
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>6.9.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-picocontainer -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-picocontainer</artifactId>
<version>6.9.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-junit -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>6.9.0</version>
<scope>test</scope>
</dependency>
I have faced the same problem and try all the method mentioned above. Then I identified that the problem is in the pom file.
so if you have given
TestNG.xml
in your pom file. Then, Perhaps the location of the TestNG.xml file is not correct.
By writing above command it will search the file in the root directory. If your TestNG.xml file is present somewhere else other then root directory then give that path or else copy the whole path of the TestNG.xml file from the server and paste it.
In my case I have done like below:
/home/ubuntu/TestNG.xml
and the problem resolved.
Karate is case sensitive so making sure your file and folder name and also package reference in Test runner file.
After trying for some time I was able to fix the issue so thought of posting as it can be helpful to someone in the future:
It has nothing to do with your pom.xml file and does not require any additional library or plugins.
You are getting the path of the file where failing reports are present. Something like this:
[ERROR] Please refer to /Users/{username}/{folderPath}/{projectName}/target/surefire-reports for the individual test results.
Click on that path and go to the folder. Within the folder search for the file testing-failed.xml file and open it in any of the editors.
You will be able to see the Class/Test which is failing. Something like this:
<class name="PackageName.ClassName">
Go to that file in your IDE and try to run it. If it's failing then fix it and try to run the mvn clean install again.
This should fix the issue, at least for me it worked.
I had a similar error yesterday:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.21.0:test (default-test) on project my-working-tests: There are test failures.
The error was solved by implementing the following two steps:
adding the 2.22.1 to the pom.xml fie.
run command mvn -o test
In the step step, the errored test got past, but my terminal threw error, Could not resolve dependencies, for the next test. That made sense because the command, mvn -o test, was doing the test offline, without downloading dependencies. Please note that if you do not have a next test, you will not meet the same new error as I met.
Then, I rerun the command mvn install again. It ran successfully.
Please check the code for the step 1
Old code (raised the error):
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
.......
</configuration>
</plugin>
Updated code (solved the error):
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
.......
</configuration>
</plugin>
Hope it helps to your error.
If you're hitting this issue in Eclipse, you can try right click the project > Run as > Maven Install
For any missing plugin, add it to your pom.xml file under the plugins section and Run Maven Install again. I hope it helps.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M7</version>
</plugin>
HI All can you try adding the below in your POM and then use
mvn clean compile and then mvn install.
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>

Maven Jar requires two execution contexts

I need to be able to allow my maven package to be executed in two contexts.
User needs to execute jar from the command line to generate a licence key from an AWT dialog that appears when the user executes java -jar myjar.jar
myjar needs to also download all its transitive dependencies (in the "normal" maven way) when a client project references myjar as a dependency.
Scenario 1 requires the AWT forms jar to be packaged so that the AWT dialog pops up.
Scenario 2 requires all the other dependencies to be downloaded as the client project builds.
How do I package this to keep it as small as possible?
I've tried the jar-plugin, the shade-plugin and the assembly-plugin independently without much luck.
JAR-PLUGIN:
Placing the forms-1.2.1.jar in the lib/ directory and adding it to the classpath doesn't work because java -jar myjar.jar will not load it at the commandline
SHADE-PLUGIN:
Downloads the internet. How to exclude some dependencies?
ASSEMBLY-PLUGIN:
Downloads the internet. How to exclude some dependencies?
Is there a way to package the transitive dependencies of forms-1.2.1.jar (for scenario 1) but exclude all other dependencies at packaging time so they are downloaded at client project build time (for scenario 2).
Can the jar-plugin or assembly-plugin do this?
What you think about the following:
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<!-- NOTE: We don't need a groupId specification because the group is
org.apache.maven.plugins ...which is assumed by default.
-->
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.2</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
[...]
</project>
That will produce a jar file which contains all dependencies and make it possible to call it from command line, but to get it working correctly you need a supplemental part:
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.2</version>
<configuration>
[...]
<archive>
<manifest>
<mainClass>org.sample.App</mainClass>
</manifest>
</archive>
</configuration>
[...]
</plugin>
[...]
</project>

Error when trying to run packaged Jar (Maven)

I am having an issue running a JAR that I have packaged using Maven. It is a very simple class that just takes a file from a directory on the local machine and posts it to an SFTP.
I am using the "mvn package" command and am seeing the JAR show up in my target folder, but when I attempt to run the JAR I am getting the following error:
"Exception in thread "main" java.lang.NoClassDefFoundError: com/jcraft/jsch/Jsch
at. java.lang.Class.getDeclaredMethods0
etc.
Caused by: java.lang.ClassNotFoundException: com.jcraft.jsch.Jsch"
Now the program runs just fine when I run it in eclipse just running the main method, so I assume that it is something with the maven package command not bringing all the correct classes into my JAR? In my "Maven Dependencies" all I have is the "jsch-0.1.49.jar" and "junit-3.8.1.jar" Any help would be greatly appreciated. I am sure there is a step I missed somewhere in this process.
The maven package command and the maven-jar-plugin did not build jar files with the dependencies attached. They just build a jar file with your project sources.
You need an uber jar which consist of everything your main class need. The easiest way to archive this is by using the maven-shade-plugin:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
</project>

Resources