spring-boot-maven-plugin doesn't create fat jar - java

I'm using spring-boot-maven-plugin to package my REST service. I'm building the jar using mvn clean install or mvn clean package. After I decompile the jar, I don't find any of the dependencies added (I was expecting it to be a fat jar with all dependencies)
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.9.RELEASE</version>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>repackage</goal>
<goal>build-info</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>true</executable>
<finalName>myapp</finalName>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
When I run the spring boot using java -jar myapp.jar -Drun.jvmArguments="-Dspring.profiles.active=qal" I'm getting ClassNotFoundException for many of the classes. It's clear that artifact didn't build as expected. However, if I start spring boot application using maven ./mvnw spring-boot:run -Drun.jvmArguments="-Dspring.profiles.active=qal" I guess, it finds all the dependencies in target folder hence works fine. How can I fix the build issue so that I can start app using java -jar command.
EDIT: It's multi-module maven project

it seems you are using a wrong command. mvn clean package is maven command, you should use command 'repackage', it used for
Repackages existing JAR and WAR archives so that they can be executed
from the command line using java -jar
as it mentioned here https://docs.spring.io/spring-boot/docs/current/maven-plugin/repackage-mojo.html
Or probably it's plugin configuration issue. Just checked: it works with spring-boot-maven-plugin-2.0.0.RELEASE
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>exec</classifier>
</configuration>
</execution>
</executions>
</plugin>

Use this one
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>${start-class}</mainClass>
<executable>true</executable>
<fork>true</fork>
<!-- Enable the line below to have remote debugging of your application on port 5005
<jvmArguments>-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005</jvmArguments>
-->
</configuration>
</plugin>

Related

Spring boot with maven and tanuki wrapper

I have spring boot app and i want to run it as service. I am using tanuki wrapper to do so :
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<version>1.8.1</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>generate-daemons</goal>
</goals>
</execution>
</executions>
<configuration>
<logsDirectory>logs</logsDirectory>
<daemons>
<daemon>
<id>DataCopier</id>
<wrapperMainClass>org.tanukisoftware.wrapper.WrapperSimpleApp</wrapperMainClass>
<mainClass>com.myProject.mainClass</mainClass>
<platforms>
<platform>jsw</platform>
</platforms>
</daemon>
</daemons>
</configuration>
</plugin>
However how do i run this? I included it as plugin. I tried to mvn install it to see if this generates something but it does not generate anything in target folder. How do i run this then?

Can I make an executable jar with "build-info" by "Spring Boot Maven Plugin"?

I am trying to make an executable jar file with "Spring Boot Maven Plugin".
Usually, I can do this by setting "repackage". goal in my pom.xml file was set like this.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.2.6.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
And my build command is like this:
mvn clean package spring-boot:repackage
This works, but I want to have META-INF/build-info.properties in the jar file.
By having this file, I can check build info in my springboot application, so I want to have it.
To generate "META-INF/build-info.properties" file, I need to set "build-info" goal.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.2.6.RELEASE</version>
<executions>
<execution>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
Well now I am in a dilemma because
Setting "repackage" goal -> an executable jar without "META-INF/build-info.properties"
Setting "build-info" goal -> an inexecutable jar with "META-INF/build-info.properties"
I want to have an executable jar with "META-INF/build-info.properties".
I am pretty new to this maven process. Is there anyone who can help me?
SOLVED
I solved this issue. The problem was spring-boot-maven-plugin was in the wrong place. It was inside <pluginManagement>. I moved this plugin to <plugins>
Like #Mark Bramnik answered, I can set both goal settings in pom.xml, and mvn clean package command is enough!
You can run multiple goals of the same plugin.
So you can probably do something like this:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.2.6.RELEASE</version>
<executions>
<execution>
<goals>
<goal>build-info</goal>
</goals>
</execution>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>

How to deploy war to Tomcat from child pom using maven tomcat plugin?

I have a maven project with the following plugin in the pom
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/casemanager</path>
<update>true</update>
</configuration>
</plugin>
When I do a mvn tomcat7:run, my war is deployed just fine. But I have the following configuration in one of the modules of my maven project
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<executions>
<execution>
<id>tomcat-run</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<fork>
true
</fork>
</configuration>
</execution>
<execution>
<id>tomcat-shutdown</id>
<goals>
<goal>shutdown</goal>
</goals>
<phase>post-integration-test</phase>
</execution>
</executions>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
I want tomcat to run in the pre-integration-test phase. Now when I do a mvn verify, it starts the server but does not deploy my war. I have even tried providing the warSourceDirectory. Am I missing something here?
I figured it out. Seems like when I execute a goal in a child pom, tomcat does not search for wars in other modules on the same level. I had to use webapp
<webapps>
<webapp>
<groupId>com.fico.fos.casemanager</groupId>
<artifactId>casemanager.ui</artifactId>
<version>1.0.0-SNAPSHOT</version>
<type>war</type>
<asWebapp>true</asWebapp>
</webapp>
</webapps>
I had to use this within the configuration tags

Attaching maven-exec-plugin to initialize

I am trying to attach an exec process to a maven build. I want to build a secondary dependency (that is actually a Clojure project build with maven) every time I build the maven project.
Currently, I have the pom.xml below, but the exec process is just not running. The maven documentation is not that much help.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<id>compile-with-lein</id>
<phase>initialize</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>lein</executable>
<commandlineArgs>install</commandlineArgs>
<workingDirectory>../nrepl-clojure</workingDirectory>
</configuration>
</plugin>

Running an Executable Jar using Maven

Is it possible to run an executable jar file using Maven?
A brief introduction to setup:
Environment is running on Centos
I have included the exec plugin into my maven POM and have it running a shell script that is meant to run the jar file and works fine using the terminal but I get Unable to access jarfile error when run in maven.
I have chmod the jar, shell and other files to 777
I have editted the sudoers file as suggested by other posts
I have also given maven and Jenkins root access
All of these changes still result in the above error and I have run out of ideas.
HELP!!!
POM File
<pluginRepositories>
<pluginRepository>
<id>smartbear-sweden-plugin-repository</id>
<url>http://www.soapui.org/repository/maven2/</url>
</pluginRepository>
</pluginRepositories>
<build>
<plugins>
<plugin>
<artifactId>exec-maven-plugin</artifactId>
<groupId>org.codehaus.mojo</groupId>
<executions>
<execution>
<!-- Run our Password Checker script -->
<id>Password Checker</id>
<phase>generate-sources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>src/pass/passCheck.sh</executable>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>eviware</groupId>
<artifactId>maven-soapui-plugin</artifactId>
<version>4.5.1</version>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>test</goal>
</goals>
<id>MPesa-Tests</id>
<configuration>
<projectFile>src/test/GenericAPI-G2-soapui-project.xml</projectFile>
<outputFolder>SoapUI-Results/</outputFolder>
<junitReport>true</junitReport>
<exportwAll>true</exportwAll>
<printReport>false</printReport>
<projectProperties>
<value>message=Tests Completed</value>
</projectProperties>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<configuration>
<outputDirectory>/usr/share/Surefire/</outputDirectory>
<reportDirectories>
<reportDirectories>/usr/share/Surefire/reports/</reportDirectories>
</reportDirectories>
</configuration>
</plugin>
</plugins>
</build>
</project>
I had this exact same need to execute a Spring Boot packaged application and solved it by using the maven dependency plugin to copy the artifact to /tmp (or anywhere for that matter) and then using the maven exec plugin to execute that jar using the exec:exec goal.

Categories