Spring dev tool class is not found at run time - spring-boot

I have created a SpringBoot app which has spring-boot-devtools in Maven pom
I can run it without issue in Intellji. But if I run it from the jar being built, it throws the following exception. It gives the following exception. It looks like the devtool jars are not packaged?!
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.x.x.x.TestApplication]; nested exception is java.io.FileNotFoundException: class path resource [org/springframework/boot/devtools/filewatch/FileChangeListener.class] cannot be opened because it does not exist

Devtools is excluded from a packaged jar by default to avoid it being accidentally used in a production deployment. If you want to include Devtools in your packaged jar you can opt-in. To do so, set excludeDevtools to false in your plugin configuration:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludeDevtools>false</excludeDevtools>
</configuration>
</plugin>
</plugins>
</build>

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.

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

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>

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.

Spring Boot Maven - Include native library

In my project i'm using Sigar library that require some native library. I would like to include all the files (.ddl, .so etc - that are platform specific) inside the JAR or, in the same directory where the jar will be run.
Sigar search those library in the java.library path, but i cant include all of them in the system PATH (my application will be executed in some production machines, and i can't touch the PATH).
In order to do this, i've this build phase in my pom.xml
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<forkMode>once</forkMode>
<workingDirectory>target</workingDirectory>
<argLine>-Djava.library.path=${baseDir}/src/main/resources/lib</argLine>
</configuration>
</plugin>
</plugins>
</build>
When i run the pack goal (that is included in spring-boot-maven-plugin) and i run the jar (with java -jar myJar.jar) i got this error:
Unable to open nested entry 'lib/libsigar-amd64-freebsd-6.so'. It has
been compressed and nested jar files must be stored without
compression. Please check the mechanism used to create your executable
jar file
Someone can tell me how i can build this JAR ? I've just used spring-boot-maven-plugin because is included in Spring Boot Starter 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