I have a spring boot project that has a few local jar files dependencies like
mssql-jdbc-6.2.1.jre8.jar
internal-commons.jar
internal-db.jar
etc.
How do I package the project into a WAR file? Thanks in advance!
You can use existing libraries to achieve this.
If you use maven, you can use the maven-dependency-plugin
For example, in your pom.xml file, add the following to your dependencies:
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.2.0</version>
</dependency>
And then, use it as a plugin in your pom.xml:
<plugin>
<!-- http://jonathangraham.github.io/2016/01/05/Local_Jar_Dependency_With_Maven -->
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
The entire process is explained here in more detail.
Related
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>
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>
I have a situation where i need to Unpack a spring jar and replace a file in that package with my .class file and repackage it back as jar. I understood that this can be done in Maven. So i created a maven project and i added the .java(to be replace in the Spring jar) to it . Here is my Project structure.
here is my pom.xml
<project>
<modelVersion>4.0.0</modelVersion>
<name>repackage</name>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.4.6</version>
<packaging>pom</packaging>
<build>
<plugins>
<!-- Unpacking -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>unpack</id>
<phase>package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.6.SEC03</version>
<type>jar</type>
<overWrite>false</overWrite>
<outputDirectory>target</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<!-- Packing -->
<!-- <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classesDirectory>target</classesDirectory>
<classifier>sample</classifier>
</configuration>
</execution>
</executions>
</plugin>-->
</plugins>
</build>
</project>
I am able to unpack the spring jar using maven dependency plugin and i figured out how to pack it back to a jar using maven-jar-plugin. The part i am struggling to understand 1) how convert my .java file into a .class file. As the packaging is pom type <packaging>pom</packaging> I am not able to compile the java file(JdkVersion.java). if successful in creating the class file 2) How to replace this .class file into unpacked spring jar as the file i need to replace is inside the sub directory of /springpackage
You don't have to. Spring is an open source project just clone its source code https://github.com/spring-projects/spring-framework?files=1 and update the code and create a package using simple mvn package command
i have a problem and I'm not able to solve or understand the hole workflow behind this process.
I use eclipse with maven.
This is my simple test pom.xml
...
<dependencies>
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>2.2.8</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jvnet.jax-ws-commons</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>wsimport</goal>
</goals>
<configuration>
<xdebug>true</xdebug>
<verbose>true</verbose>
<keep>true</keep>
<packageName>abc.model</packageName>
<sourceDestDir>${project.build.directory}/generated-sources/wsimport/</sourceDestDir>
</configuration>
</execution>
</executions>
</plugin>
<plugin> <groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.9.1</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-sources/wsimport/</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
The jaxws-maven-plugin generate the files in the sourceDestDir. The build-helper-maven-plugin add the files during the maven install process the to the correct folder and also into the resulting jar file. So far so good.
But I'm not able to use the generated files/classes in eclipse. So in eclipse maven does not recognize the files as source or does not but this in the source path. Did I make an mistake or did I miss something?
Thanks for your help.
Update:
I observer a strange behavior. Same test project same pom file. If I import this existing Maven Project in eclipse it works like expected. I can directly use the generated files as source.
But if I delete this source folder, I'm still not able to restore this with maven.
I want to override the configLocation option in maven checkstyle plugin. Sample part of POM.xml is :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<configuration>
<configLocation>blahblah/checkstyle/checkstyle.xml</configLocation>
<consoleOutput>true</consoleOutput>
</configuration>
<dependencies>
<dependency>
<groupId>com.example.blahblah</groupId>
<artifactId>checkstyle-config</artifactId>
<version>2.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
<configuration>
<configLocation>checkstyle.config.xml</configLocation>
<suppressionsLocation>checkstyle.suppressions.xml</suppressionsLocation>
... other configuration ...
</configuration>
</plugin>
As it can be seen above, checkstyle-config is a separate maven project which contains the rules for style check and the config file use for rules is blahblah/checkstyle/checkstyle.xml. If I have to override blahblah/checkstyle/checkstyle.xml and use some other .xml which is stored in current project and not checkstyle-config project, then how can I do that?
You can override the plugin configuration in your module by copying the above plugin configuration part and just overriding the config location. In this you can move the configuration tag to within the execution, so that that configuration is applicable to that execution only.
See below example
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>check</goal>
</goals>
<configuration>
<configLocation>blahblah/checkstyle/checkstyle.xml</configLocation>
</configuration>
</execution>
</executions>