Maven: add external JAR to spring boot project while building jar - maven

One external is added as dependency in spring boot project with scope=system. Build is successful and when running the project from eclipse it is successful. But when I am running the project from command project using command java -jar springBoot.jar. then it is failed with below exception :
Unsatisfied dependency expressed through field 'jdbcTemplate'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource

Add below tag in pom.xml. When adding below tag then it will include jars having scope = system.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>

Related

Runtime Error in SprinBoot project : unable to find SqlSessionFactory

I have a simple SpringBoot project in IntelliJ that I generated at http://start.spring.io. I added a dependency from the local repository and it was successfully imported. This library depends on the different local library.
I am able to Build my project successfully, however when I am trying to Run it I am getting runtime error. I added new dependency to my project that contains the class which was not found during the startup but it didn't resolve my issue and I am still having problems on the startup.
Here is the original dependency I added to my project :
<dependency>
<groupId>connect_mgr</groupId>
<artifactId>connect-mgr</artifactId>
<version>1</version>
Inside this "connect_mgr" project there is a dependency on the different library :
<dependency>
<groupId>services_core</groupId>
<artifactId>services-core</artifactId>
<version>1</version>
</dependency>
There is also an implementation in the "services_core" project :
#Bean
#DependsOn("sqlSessionFactory")
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
In my Project I also added a dependency to IBatis-Core that contains "SqlSessionFactory" class.
And still, on the startup of my project I am getting this error :
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [com/config/MyBatisMapperConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception; nested exception is java.io.FileNotFoundException: class path resource [null] cannot be opened because it does not exist
Main class in my projec resides in "services_core" project.

setting up path for a library jar for different environments to make compatible with CI-CD

I've a jar library that needs to be read as an argument to my class MyProject and a respective app property in application.properties my.customJar=/WEB-INF/lib/myJar.jar to use the jar in my local app run and it works well locally but it fail on the pcf cloud. I did some changes & respective to this property, I've tried adding an override in cloud environment application-dev.properties i.e. my.customJar=${project.basedir}/src/main/webapp/WEB-INF/lib/myJar.jar
I've also tried with relative path my.customJar=./src/main/webapp/WEB-INF/lib/myJar.jar but neither of them appear to have worked.
The problem arises specifically when I send this code to the pcf cloud I am getting FileNotFoundException. And it fails there with error mentioned below. Can anyone please guide me with how can I set the path for the other profiles. Also, please let me know if there is any more convenient way to do this as well?
Note: I am using multiple spring profiles.
Error I am getting
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myController' defined in file [/home/abc/app/BOOT-INF/classes/com/example/myController.class]: Unsatisfied dependency expressed through constructor parameter 2; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jarUserBean' defined in class path resource [com/example/configuration/CommonConfigs.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.myJar.api.MyProject]: Factory method 'jarUserBean' threw exception; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/lib/myJar.jar]
2021-08-27T07:53:53.647+01:00 [APP/PROC/WEB/0] [OUT]
Here, the jarUserBean is a bean created using the jar's insputStream i.e.
#Value("${my.customJar}")
private String pathToJar;
#Bean
#ConditionalOnProperty(prefix = "my", name = "customJar")
public MyProject jarUserBean() throws IOException, ProjectInvalidException, InterruptedException {
Resource resource = loader.getResource(pathToJar);
InputStream is = resource.getInputStream();
return new MyProject(is);
}
Adding this plugin in the pom helped me reading it from the classpath itself.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>jar</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>

Spring boot 2.0.1 application does not start with spring-cloud.version Finchley.RC1 but works fine with Finchley.M9

I am getting following exceptions while running a spring boot 2.0.1 application with spring clould version Finchley.RC1 but works fine if I change the version to Finchley.M9 in pom.xml as:
<spring-cloud.version>Finchley.RC1</spring-cloud.version>
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'propertySourceBootstrapConfiguration': Unsatisfied dependency expressed through field 'propertySourceLocators'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.company.configmanagement.springboot.autoconfig.CMBootstrapConfiguration$ConsulPropertySourceConfiguration': Unsatisfied dependency expressed through field 'configManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'configManager' defined in com.company.configmanagement.springboot.autoconfig.ConfigManagementAutoConfiguration: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: com.google.common.base.Splitter.splitToList(Ljava/lang/CharSequence;)Ljava/util/List; | AnnotationConfigApplicationContext:558
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'propertySourceBootstrapConfiguration': Unsatisfied dependency expressed through field 'propertySourceLocators'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.company.configmanagement.springboot.autoconfig.CMBootstrapConfiguration$ConsulPropertySourceConfiguration': Unsatisfied dependency expressed through field 'configManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'configManager' defined in com.company.configmanagement.springboot.autoconfig.ConfigManagementAutoConfiguration: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: com.google.common.base.Splitter.splitToList(Ljava/lang/CharSequence;)Ljava/util/List;
Looks like it was a version compatibility issue as an internal dependency was pulling guava version 14.0. For now, using the following version of dependency fixed the issue.
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>23.0</version>
</dependency>

how to resolve UnsatisfiedDependencyException with spring boot hibernate?

I am getting the following issue when Spring boot hibernates Connection.
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bookController': Unsatisfied dependency expressed through field 'bookService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bookServiceImp': Unsatisfied dependency expressed through field 'bookDao'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bookDaoImp': Unsatisfied dependency expressed through field 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'getSessionFactory' defined in class path resource [com/go/myapp/Config/AppConfig.class]: Invocation of init method failed; nested exception is org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
If you are building your project with maven look for the pom file. when you add dependencies there are Compile Dependencies for those dependencies. For an Example SpringBoot Starter 1.5.8.Release has https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter/1.5.8.RELEASE
SpringBoot 1.5.8.Release
SpringCore 4.3.12.Release
So please check your dependencies and there compile dependencies .
posting your POM file would help

Error while deploying Spring Batch Application - java.lang.NoClassDefFoundError: org/codehaus/jettison/mapped/MappedXMLOutputFactory

I am stuck with this error from last few days , while loading the spring batch application,it keep on giving error
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jobRepository' defined in ServletContext resource [/WEB-INF/classes/spring/batch/jobs/config/context.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: org/codehaus/jettison/mapped/MappedXMLOutputFactory
I have added jettison to my pom and here below my pom file looks like
org.codehaus.jettison
jettison
1.3.2
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-oxm
${spring.version}
org.springframework.batch
spring-batch-test
${spring.batch.version}
jettison
org.codehaus.jettison
pom.xml should have dependency like :
<dependency>
<groupId>org.codehaus.jettison</groupId>
<artifactId>jettison</artifactId>
<version>1.3.2</version>
</dependency>
Also, do you see jettision-1.3.2.jar in your maven dependecies?
if you are using eclipse, ctr+sft+t : are you able to see this class?

Resources