I want to deploy my application using the embedded tomcat in spring -boot. I figured that I have to run the java -jar spring-boot-app.jar command, but I cannot find the jar file for the application anywhere.
On running mvn clean package I am able to generate a war file to deploy externally, how can I do the same with embedded tomcat ?
You need to remove following line from pom.xml
<packaging>war</packaging>
or replace war packaging with jar. Make sure you have spring-boot-maven-plugin in maven build plugins
The jar should then be available in target folder
To create an executable jar, we need to add the spring-boot-maven-plugin to our pom.xml. To do so, insert the following lines just below the dependencies section:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
For more information,refer this :
https://docs.spring.io/spring-boot/docs/current/reference/html/getting-started.html
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.
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.
I am trying to figure out what is the best way to setup a spring boot application in such a way that its has its own jar dependencies but additional jars are added to classpath at runtime when its being run as java -jar command. What approach makes more sense
Use the original jar (without dependencies added to it) and place all jars (application and runtime) in a folder on file system and use PropertiesLauncher to specify the loader.path to jars folder.
Use the fat jar (with application jars) place the additional jars on the filesystem and somehow include those as additional jars that need to be added to classpath. Not sure how this can be done.
Is there another better way to do this
The PropertiesLauncher was designed to work with fat jars, so you should be able to keep the fat jar and add as many additional dependencies as you like in an external location, e.g. with loader.path=/opt/app/lib:lib. I guess that's your option 2? If it doesn't work we can discuss in a github issue.
I resolved this issue using the following spring-boot-maven-plugin configuration, I had to build my Uber jar without excluded artifacts to create my external "lib" directory, then I added my excluded artifacts again and packaged my Uber jar with my application specific dependencies only.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.3.1.RELEASE</version>
<configuration>
<layout>ZIP</layout>
<executable>true</executable>
<excludeArtifactIds>
<!-- My libs which will be packaged with my Uber jar-->
<!-- core,data-feeder,engine,lightspeed-tcp-api,order-manager,store,strategies,utils,viewer -->
<!-- Other libs -->
antlr,aopalliance,aspectjrt,aspectjweaver,classmate,commons-lang,
dom4j,h2,hibernate-commons-annotations,hibernate-core,hibernate-entitymanager,
hibernate-jpa-2.1-api,hibernate-validator,jackson-annotations,jackson-core,jackson-databind,
jandex,javassist,javax.transaction-api,jboss-logging,jboss-logging-annotations,jcl-over-slf4j,
jul-to-slf4j,log4j-over-slf4j,logback-classic,logback-core,mysql-connector-java,slf4j-api,
snakeyaml,spring-aop,spring-aspects,spring-beans,spring-boot,spring-boot-autoconfigure,
spring-boot-starter,spring-boot-starter-aop,spring-boot-starter-data-jpa,spring-boot-starter-jdbc,
spring-boot-starter-logging,spring-boot-starter-tomcat,spring-boot-starter-web,
spring-boot-starter-websocket,spring-context,spring-core,spring-data-commons,spring-data-jpa,
spring-expression,spring-jdbc,spring-messaging,spring-orm,spring-tx,spring-web,spring-webmvc,
spring-websocket,tomcat-embed-core,tomcat-embed-el,tomcat-embed-logging-juli,tomcat-embed-websocket,
tomcat-jdbc,tomcat-juli,validation-api,xml-apis
</excludeArtifactIds>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
Then, I added the following property to my "application.properties" which inside my jar "resources/" dir to specify my "lib" dir for Spring PropertiesLauncher where I put "lib" dir along with my jar in the same dir.
loader.path=lib/
Finally, I did run my jar using the following command
java -jar back-tester-0.0.1-beta-01.jar
Also, you can add the "loader.path" property to your command line without putting it in your "application.properties" like the following command but this way didn't work with me as I packaged my jar as an executable one which I'm running as linux service.
java -Dloader.path="lib/" -jar back-tester-0.0.1-beta-01.jar
Now, I successfully reduced my jar size from 29 M to only 1 M jar which contains only my application specific libs and it works out of the box.
thank you #Ashraf Sarhan, you rescue my two days :)
I added in pom file:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<layout>ZIP</layout>
<executable>true</executable>
<mainClass>vn.com.Mymainclass</mainClass>
<excludes>
<exclude>
<groupId>com.vn.groupId</groupId>
<artifactId>excluded-id-a</artifactId>
</exclude>
<exclude>
<groupId>com.vn.groupId</groupId>
<artifactId>excluded-id-b</artifactId>
</exclude>
</excludes>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
And Placed ./lib folder containing two jars of two files which excluded above beside with my-main-spring-boot-app.jar file, and I ran:
java -Dloader.path="lib/" -jar my-main-spring-boot-app.jar
It worked perfectly.
We have a maven project where we use several linux bash scripts for various entries to our java application. We have solved this by the exec-maven-plugin so the scripts typically looks like: mvn -e -o -q exec:exec -Dexec.executable="java" -Dexec.args="...". For some reason, we are constrained to use tho offline flag (-o).
My question is: How do I ensure that the exec-maven-plugin is downloaded during the compile phase? There is a risk that a developer doesn't have the exec-maven-plugin downloaded and since the exec-maven-plugin is used with the maven offline flag it won't be downloaded if it is not there.
Thanks to the comments of #eis I managed to sort this out. The pom now looks like this:
<properties>
<additionalJavaArgs></additionalJavaArgs>
</properties>
...
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<executable>java</executable>
<commandlineArgs>
-XX:+PrintCommandLineFlags -showversion -classpath %classpath ${additionalJavaArgs}
</commandlineArgs>
</configuration>
</plugin>
</plugins>
</build>
The bash script looks like the following:
ADDITIONAL_JAVA_ARGS=$*
mvn -e -o -q exec:exec -DadditionalJavaArgs="$ADDITIONAL_JAVA_ARGS"
That way the user of the script can add a main class followed by an unlimited number of application arguments.