How to avoid install / deploy spring boot fat jar to maven repo - java

We are using the spring-boot-maven-plugin's repackage task for packaging fat-jar (sample pom.xml below), and tried the <attach>false</attach>, but seems the fat-jar will be install / deployed anyway.
Currently, we configured maven-{install,deploy}-plugin's <skip>true</skip> to work around this issue, but not sure how to use boot plugin's <attach> or other ways to fix this.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.2.4.RELEASE</version>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<attach>false</attach>
</configuration>
</execution>
</executions>
</plugin>
Thanks

You have to configure it correctly like this:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.2.4.RELEASE</version>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<attach>false</attach>
</configuration>
</execution>
</executions>
</plugin>

Related

Building with JIB give Multiple valid main classes were found even though the mainClass is define

When building my service project with jib command mvn clean compile jib:build it's give the following error:
Failed to execute goal
com.google.cloud.tools:jib-maven-plugin:1.0.2:build
(build-image-and-tag-image) on project my-service: Multiple valid
main classes were found: com.myservice.MyServiceApplication,
io.swagger.Swagger2SpringBoot, perhaps you should add a mainClass
configuration to jib-maven-plugin -> [Help 1]
However I have set the main classes for spring-boot
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.myservice.MyServiceApplication</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
It's still doesn't work.
I've tried to add it to the jib config to:
<plugins>
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<configuration>
<container>
<mainClass>com.myservice.MyServiceApplication</mainClass>
</container>
</configuration>
<executions>
<execution>
<id>build-image-and-tag-image</id>
<phase>package</phase>
<goals>
<goal>dockerBuild</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
It's still doesn't work.
Any other way to force jib to ignore the other class and use com.myservice.MyServiceApplication instead.
Note: mvn clean install work fine and I have no problem using it has a stand alone spring boot app.
The main class need to be set in the < plugins > define in < build > of the pom.xml file.
It would look like this to fix the problem:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.myservice.MyServiceApplication</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<configuration>
<container>
<mainClass>com.myservice.MyServiceApplication</mainClass>
<ports>
<port>8080</port>
</ports>
<environment>
<application.title>${project.name}</application.title>
<application.version>${project.version}</application.version>
</environment>
<jvmFlags>
<jvmFlag>-javaagent:/usr/local/newrelic/newrelic.jar</jvmFlag>
</jvmFlags>
</container>
</configuration>
</plugin>
.... (more plugin)
</plugins>
</build>

Spring boot META-INF/build-info.properties not in artifact

I would like to access my commit info in /info. Thing is, it works in IDE but there seems to be a problem while packaging the artifact, the file META-INF/build-info.properties is not packaged into jar! Any hint why?
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>build-info</goal>
</goals>
<phase>package</phase>
</execution>
</executions>
<configuration>
<additionalProperties>
<number>${buildNumber}</number>
<job>${buildJob}</job>
</additionalProperties>
</configuration>
</plugin>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>${start-class}</mainClass>
<executable>true</executable>
</configuration>
<executions>
<execution>
<goals>
<goal>build-info</goal>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
Remove <phase>package</phase> from your execution and now do a mvn clean install. It should generate META-INF/build-info.properties file

How to package spring-boot to jar with local jar using maven

I have a jar in my system, and I want to package it into jar application using maven, my dependency and plugin as below:
<dependency>
<groupId>org.wltea</groupId>
<artifactId>IKAnalyzer</artifactId>
<version>3.2.3</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/BOOT-INF/lib/IKAnalyzer3.2.3Stable.jar</systemPath>
</dependency>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<mainClass>gpdi.MyApp</mainClass>
</configuration>
</execution>
</executions>
</plugin>
I also try maven-dependency-plugin but did'nt work when packaging jar file.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>compile</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/${project.build.finalName}/BOOT-INF/lib</outputDirectory>
<includeScope>system</includeScope>
</configuration>
</execution>
</executions>
</plugin>
Add the below plugin and clean your project :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<executions>
<execution>
<id>install-Transformation lib</id>
<phase>clean</phase>
<configuration>
<file>${basedir}/file/path/jarname.jar</file>
<repositoryLayout>default</repositoryLayout>
<groupId>org.wltea</groupId>
<artifactId>IKAnalyzer</artifactId>
<version>3.2.3</version>
<packaging>jar</packaging>
<generatePom>true</generatePom>
</configuration>
<goals>
<goal>install-file</goal>
</goals>
</execution>
</executions>
</plugin>
Once you clean the project. Comment scope and systempath like below then build and install :
<dependency>
<groupId>org.wltea</groupId>
<artifactId>IKAnalyzer</artifactId>
<version>3.2.3</version>
<!--scope>system</scope>
<systemPath>${project.basedir}/src/main/BOOT-INF/lib/IKAnalyzer3.2.3Stable.jar</systemPath-->
</dependency>

Findbugs plugin configuration in maven pom.xml

My goal is to execute findbugs on a maven project -> generate xml -> convert xml to html & finally fail build if there are HIGH priority FindBugs warnings. Below is the plugin configuration configuration in pom.xml I have configured
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>3.0.3</version>
<executions>
<execution>
<id>noFailOnError</id>
<phase>verify</phase>
<goals>
<goal>check</goal>
</goals>
<configuration>
<failOnError>false</failOnError>
<xmlOutput>true</xmlOutput>
<findbugsXmlOutputDirectory>${project.build.directory}/findbugs</findbugsXmlOutputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>xml-maven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>transform</goal>
</goals>
</execution>
</executions>
<configuration>
<transformationSets>
<transformationSet>
<dir>${project.build.directory}/findbugs</dir>
<outputDir>${project.build.directory}/findbugs</outputDir>
<stylesheet>fancy-hist.xsl</stylesheet>
<!--<stylesheet>default.xsl</stylesheet> -->
<!--<stylesheet>plain.xsl</stylesheet> -->
<!--<stylesheet>fancy.xsl</stylesheet> -->
<!--<stylesheet>summary.xsl</stylesheet> -->
<fileMappers>
<fileMapper
implementation="org.codehaus.plexus.components.io.filemappers.FileExtensionMapper">
<targetExtension>.html</targetExtension>
</fileMapper>
</fileMappers>
</transformationSet>
</transformationSets>
</configuration>
<dependencies>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>findbugs</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>3.0.3</version>
<executions>
<execution>
<id>failing-on-high</id>
<phase>verify</phase>
<goals>
<goal>findbugs</goal>
</goals>
<configuration>
<failOnError>true</failOnError>
</configuration>
</execution>
</executions>
</plugin>
My problem is that html transformation is not happening stating that
[WARNING] No files found for transformation by stylesheet fancy-hist.xsl
Can the pom.xml correctness be verified & also can someone help me with the reason on why html tansformation is not happening ?
The issue with the above plugin configuration is that failing-on-high is configured during the verify phase rather than install phase. So in case of build error in verify phase, no output xml is generated because of which the output xml was not found. This was fixed by changing it to install
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>3.0.3</version>
<executions>
<execution>
<id>failing-on-high</id>
<phase>install</phase>
<goals>
<goal>findbugs</goal>
</goals>
<configuration>
<failOnError>true</failOnError>
</configuration>
</execution>
</executions>
</plugin>

Deploy additional jar file with Maven?

I have an artifact which is being built and deployed in a particular way (not as a jar file). In the course of deployment, a war file is built.
How can I configure the pom so that the artifact is also deployed as a jar file, to a different location?
Maven deploy means deploy artifact to a Maven Repository, not an application server.
Configure additional JAR artifact like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>make-a-jar</id>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
Attach this new artifact to your project:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>attach-artifacts</id>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>${project.build.directory}/${project.artifactId}-${project.version}.jar</file>
<!-- <file>${project.build.directory}/${project.build.finalName}.jar</file> - if finalName is defined -->
<type>jar</type>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
This blog post and its comments have the answer.
These three plugin configurations will allow you to build/install/deploy a jar version alongside the war.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>make-a-jar</id>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<packaging>jar</packaging>
<artifactId>${project.artifactId}</artifactId>
<groupId>${project.groupId}</groupId>
<version>${project.version}</version>
<file>
${project.build.directory}/${project.artifactId}-${project.version}.jar
</file>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<executions>
<execution>
<phase>deploy</phase>
<goals>
<goal>deploy-file</goal>
</goals>
<configuration>
<packaging>jar</packaging>
<generatePom>true</generatePom>
<url>${project.distributionManagement.repository.url}</url>
<artifactId>${project.artifactId}</artifactId>
<groupId>${project.groupId}</groupId>
<version>${project.version}</version>
<file>${project.build.directory}/${project.artifactId}-${project.version}.jar</file>
</configuration>
</execution>
</executions>
</plugin>
The "maven way" is to split out src/main/java into a separate module, and have the war file depend on that.
If you're absolutely resistant to that approach, you may be able to use a profile to alter the contents of the packaging element. I'm not sure if that's possible though.
Separating them is the right way to go. Forcing maven to produce a war and a jar in the same module is possible but will cause you problems down the road.
You should add the corresponding dependency of the artifact in the dependencies of the pom file.
Ex:
<dependency>
<groupId>org.apache.myfaces.core</groupId>
<artifactId>myfaces-api</artifactId>
<version>1.2.2</version>
<scope>compile</scope>
</dependency>
One way to solve this is to have the module build a jar and then use the assembly plugin to build a war file with the jar in WEB-INF/lib of that war. I would strongly recommend against this. You'd be better off having a jar project and a war project with a parent project building both modules.

Categories

Resources