Utility Jar using Spring Boot - spring

Created a custom jar using spring boot and this is using for CRUD operations on a database table.The purpose is used to make this a utility jar, so that other services or applications can use this jar for any operations on that table. Following are the steps I followed:
1). Added this jar entry in pom.xml of a REST SERVICE and build got successfully.
2). Autowired the service class of Utility jar inside the controller of REST SERVICE.
But when I started the REST SERVICE (service is developed on spring boot), I got the error as 'the ****controller can require a bean of type *****serviceUtility. Consider defining a bean of type in your configuration'. But I am not able to see any configuration class inside the rest service and it is using application.yml for datasource related things. I am new to Spring and Spring Boot. Could any one guide me how to configure the utility jar in external services.

It is hard to tell without source code, but Spring Boot jars do not by default work as utility jars. The packaging is different. To get a Spring Boot jar to work as a utility jar within another project, you'll want to configure the build plugin like this :
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
</plugins>
</build>
Which is described here:
https://docs.spring.io/spring-boot/docs/current/maven-plugin/examples/repackage-classifier.html

If you are using spring boot , by default , the utility jar will not move all its dependencies .
You can add all the dependencies in the pom.xml for the Rest service .
Else you end up packaging some core modules twice increasing the size of your jar .

Related

How to run spring batch application without spring boot maven plugin

Is it possible to run a spring batch application without spring boot maven plugin?
I need to run the application on another server and transferring a big jar (with spring dependencies) is time consuming. For this reason I want to compile and create a jar without spring dependencies included inside the jar and run it from command line.
If you want to create a simple JAR without spring boot dependencies, remove spring-boot-plugin from build.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
Above snippet should not be present in your pom.xml.
With this, fat/uber jar won't be created.
To run built jar:
java -cp MyJar.jar:lib/* com.somepackage.subpackage.Main
Where your libraries are contained in lib directory relative to current directory and MyJar.jar is the name of the jar built with maven.

Quarkus Endpoints in dependency jars

I have a quarkus application which has dependencies to another maven module within the same project
within that module are REST endpoints
For some strange reason i cannot access those endpoints tho.. It seems quarkus will only accept endpoints of java classes within the quarkus module, or am I mistaken?
I foudn a solution:
if yopu add the jandex, endpoiints of other modules are being scanned, and can thus be found :
<build>
<plugins>
<plugin>
<groupId>org.jboss.jandex</groupId>
<artifactId>jandex-maven-plugin</artifactId>
<version>1.1.0</version>
<executions>
<execution>
<id>make-index</id>
<goals>
<goal>jandex</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
You can do this by creating a dummy extended class:
Lets assume your imported jar has this pattern, app\proto-gen\1.0-SNAPSHOT\proto-gen-1.0-SNAPSHOT.jar
Add the below to application.properties,
quarkus.index-dependency.mygrpc.group-id=app
quarkus.index-dependency.mygrpc.artifact-id=proto-gen
#Singleton
MyGrpc extends XImplBase{
//your implementation
}
beans you extended/implemented in your current project will be started.
Check https://quarkus.io/guides/cdi-reference. You need to add a beans.xml to external models, create an index or reference the dependency using quarkus.index-dependency in the application.properties.
Then it will work when running tests or using the runner. But not in dev, because there is a probably in the current version (1.1.1Final). This problem has been fixed in the master, though, and will be available in the next release next month.
Please check ClassCastException in Quarkus multi-module project for more details.

Spring boot doesn't recognize mvn liquibase cli-changes and vice versa

I'm facing a problem with spring boot and mvn liquibase.
I'm able to update and rollback liquibase via Tag when I submit changes with
mvn liquibase:update
and rollback them with
mvn liquibase:rollback -Dliquibase.rollbackTag=0.0.0
Unfortunately I'm not able to start my spring boot app when I submit changes with liquibase:update.
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'liquibase' defined in class path resource [org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration$LiquibaseConfiguration.class]: Invocation of init method failed; nested exception is liquibase.exception.MigrationFailedException: Migration failed for change set classpath:db/changelog/changelog-0.0.1.xml::0.0.1::
Which tells me that spring boot tries to run the changesets again.
When I take a look at the databasechangelog table, there a duplicated entries with different deployment_ids, could this be the problem?
Here's the screenshot
Ps: When I let Spring boot do the updating I can run the app, but I can't rollback via CLI :(
Here's my configuration in pom.xml
<plugins>
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>3.6.3</version>
<configuration>
<driver>org.postgresql.Driver</driver>
<url>connection/url>
<username>user</username>
<password>password</password>
<promptOnNonLocalDatabase>false</promptOnNonLocalDatabase>
<rollbackTag>0.0.0</rollbackTag>
<changeLogFile>destination of rollback file</changeLogFile>
</configuration>
</plugin>
</plugins>
</build>
in application.properties
spring.liquibase.change-log=classpath:db/changelog-master.xml
spring.liquibase.test-rollback-on-update=true
I guess the tagging of changelog files is correct, because it wouldn't work in CLI either.
Thank you.
I guess that classpath in maven is different from classpath in springboot app. Liquibase computes the classpath and stores it in some property in db (I don't know it's name). So when you prepare your db with maven, and then you run your app, the path to your files is different so for liquibase that means something like new changelog file.
You can fix this by adding logicalFilePath attribute to all your changelog files.

"mvn spring-boot:run": "/health" endpoint with project version

I'd like to use Spring Boot's actuator endpoint /info to show project metadata such as the Maven-provided project version.
To do so, I followed the appropriate part in Spring Boot's documentation.
The shown solution works for me when my Spring Boot application starts up with java -jar [...].
Just as the documentation says, it doesn't work when the application starts up with mvn spring-boot:run — in that case, my /info endpoint reports "version":"#project.version#".
The documentation mentions that one has to properly configure Spring Boot's Maven plugin, but I could not find any information about doing so.
How can I configure my Maven project in order to have a mvn spring-boot:run-started Spring Boot application to show project metadata?
Configure the spring-boot-maven-plugin with <addResources>false</addResources>.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.3.0.BUILD-SNAPSHOT</version>
<configuration>
<addResources>false</addResources>
</configuration>
</plugin>
Source

Spring boot application with apache axis

I am trying to run a spring boot jar which has axis2 dependencies in it. I am using spring boot maven plugin to build the jar (with dependencies). When I try to run my jar, I get the following exception in my console:
org.apache.axis2.AxisFault: The G:application\myapp\target\myapp.jar!\lib\axis2-1.6.1.jar file cannot be found.
at org.apache.axis2.deployment.repository.util.DeploymentFileData.setClassLoader(DeploymentFileData.java:111)
at org.apache.axis2.deployment.ModuleDeployer.deploy(ModuleDeployer.java:70)
at org.apache.axis2.deployment.repository.util.DeploymentFileData.deploy(DeploymentFileData.java:136)
at org.apache.axis2.deployment.DeploymentEngine.doDeploy(DeploymentEngine.java:813)
at org.apache.axis2.deployment.RepositoryListener.loadClassPathModules(RepositoryListener.java:222)
at org.apache.axis2.deployment.RepositoryListener.init2(RepositoryListener.java:71)
at org.apache.axis2.deployment.RepositoryListener.<init>(RepositoryListener.java:64)
at org.apache.axis2.deployment.DeploymentEngine.loadFromClassPath(DeploymentEngine.java:175)
at org.apache.axis2.deployment.FileSystemConfigurator.getAxisConfiguration(FileSystemConfigurator.java:135)
at ...
I then checked the structure of my jar. It has lib folder inside it, which contained all the jars (including the above mentioned axis jar). Attached is the screen shot of lib folder.
Following are the solutions which I have tried:
Placed axis jar in the same directory as application jar.
Created lib folder in the same directory as application jar and placed axis jar inside it.
Modified manifest file to include Class-Path: /lib/
None of the solutions has worked. However, when I run the application class in eclipse, the app starts and runs perfectly. But, once I create the jar, nothing seems to run.
Can anyone please help? Thanks in advance.
It looks like Axis can't cope with being run from a jar that's nested within another jar. It works fine in Eclipse as the Axis jar is available directly on the filesystem rather than being nested inside your Spring Boot application's jar file.
You can configure your application's fat jar file so that Spring Boot knows to unpack the Axis jar into a temporary location when it's run. If you're using Maven:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<requiresUnpack>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2</artifactId>
</dependency>
</requiresUnpack>
</configuration>
</plugin>
</plugins>
</build>
And if you're using Gradle:
springBoot {
requiresUnpack = ['org.apache.axis2:axis2']
}
See the Spring Boot documentation for some further details.

Resources