Maven dependency exclusion of Tomcat doesn't work - java

I created a maven-based spring-boot-project to explore how to make it run on a Websphere Application Server.
This is way I need to exclude the Embedded Tomcat of spring-boot-starter-web.
I've tried the following things:
Added a new dependency for spring-boot-starter-tomcat with the scope: provided (see Removing embedded tomcat server from war file when deploying to JBOSS [Spring-Boot]) followed by a mvn clean update.
Used the exclusions-tag in the spring-boot-starter-web dependency to exclude spring-boot-starter-tomcat (see https://maven.apache.org/guides/introduction/introduction-to-optional-and-excludes-dependencies.html) followed by a mvn clean update.
Neither of those works.
Here is my 'native' POM:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/>
</parent>
<packaging>war</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
...
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
This is my dependency with explicit Tomcat (scope: provided):
<project ...>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
This is my dependency with the exclusion:
<project ...>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring‑boot‑starter‑tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
This is the output of mvn dependency:tree:
https://imgur.com/a/VUkYCLm
(Sorry I don't know how to upload pictures here.)
I'd expect the Tomcat to be removed from my maven dependencies.
Thanks in advance.

Running your exclusions example that you posted ran fine when i build it .
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
Also I did mvn clean install after adding this exclusion and my dependency tree also doesn't include tomcat jar anymore :
Did you mvn clean install after adding the exclusions ?

Related

Why JVM cannot find the definition of SpringApplication class?

It might look like a duplicate of this question, but the solutions from it didn't work for me. I am getting an Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/boot/SpringApplication at com.example.demo.Application.main error when I try to run the application using spring-boot. My pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.blog</groupId>
<artifactId>module-8-16-springboot-webflow-thymeleaf</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath />
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.webflow</groupId>
<artifactId>spring-webflow</artifactId>
<version>2.5.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.webflow</groupId>
<artifactId>org.springframework.js</artifactId>
<version>2.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- And other dependencies -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
JDK 1.8 and JRE 8 are installed on the system and worked fine before. I deleted the C:\Users\{username}\.M2\repository\org\springframework\boot directory on the system and forced Maven to reload the dependencies. Then I decompiled the file C:\Users\{username}\.M2\repository\org\springframework\boot\spring-boot\2.0.1.RELEASE\spring-boot-2.0.1.RELEASE.jar and still found it in it has this SpringApplication class that the JVM cannot find. I also explicitly added spring-boot-2.0.1.RELEASE.jar to the project, but the error remained the same. There is only a suspicion that I am using Open JDK, and not Oracle, which did not install on the system, but simply downloaded the deployed files and placed them in the folder I needed (the JDK installer for some reason does not work for me). Does anyone have any suggestions on what this problem may be related to?

Cannot start maven service - Class not found #

Ok...for the first time in my life as a Developer I have no Idea how to fix this error.
I'm having issues with Maven commands and Intellij.
(IMPORTANT)
All maven commands on local project isn't working and returns the error:
cannot find or load main class #, java.lang.ClassNotFoundException: #
Detail for the "#" on the error message.
My pom.xml file is:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>garantia.safra</groupId>
<artifactId>gateway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>gateway</name>
<description>description</description>
<properties>
<java.version>11</java.version>
<start-class>garantia.safra.gateway.GatewayApplication</start-class>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwd</artifactId>
<version>0.9.1</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
I tried to delete .m2/repository folder but it didn't work.
I tried to 'Settings > Build, execution, deployment > Build Tools > maven > Importing' and set the import as 'project JDK' but it didnt work. (I'm using JDK 11, and it was working before)
I tried to update the maven repositories by Intellij (remote and local) and now I have a error from remote repository (can't find any imported package classes)
The only thing I did that solve it once, was recreate the project as a new one and add all the classes and pom.xml, but when I added my configuration files it broke again.
I'm using a proxy from production (.maven/jvm.config)
# -Dhttp.proxyHost=<proxy_host>
# -Dhttp.proxyPort=<proxy_port>
# -Dhttps.proxyHost=<proxy_host>
# -Dhttps.proxyPort=<proxy_port>
But the lines are all commented right?
And application.properties:
server.address=0.0.0.0
So...This is being a very anoying problem, and I'm lack of ideas. Maybe I'm gonna try to recreate the project AGAIN and reimport each file to see which is the broken one, but if someone had this issue before and fixed it...how?
Empty or delete .maven/jvm.config file, it's loaded directly and added to the command line, the comments syntax is not supported.

Why I am getting an unknown error in pom.xml file when I add the spring-boot-starter-security dependency?

I got an unknown error after adding the spring-boot-starter-security dependency to pom.xml file.I tried using maven--->update project option in eclipse too,but the error is remaining.I am 100 % sure that the error is related with the above mentioned dependency,because when I removed it the problem solved.
pom.xml file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.hasiya.springboot.demo</groupId>
<artifactId>firstSpringBootApp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>firstSpringBootApp</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
You can update Maven project to solve this problem.
go to project,right click ,find maven ,click on it ,go to update project, check the checkbox of force update.
This worked for me:
https://github.com/sarl/sarl/issues/921
which is basically adding this to your pom.xml file:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.1</version>
</plugin>

Failing to deploy spring-boot WAR to Elastic Beanstalk

I'm having trouble deploying my WAR file to elastic beanstalk (Tomcat). I've downloaded the logs and I'm getting a bunch of errors https://pastebin.com/bVTjLJDD
I tested this WAR file successfully with Apache Tomcat deployed locally on my PC, so I don't know what is causing these errors.
My pom.xml :
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.johnnathan.springboot</groupId>
<artifactId>product-store</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>product-store</name>
<description>Product storefront using Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>4.1.3</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.12</version><!--$NO-MVN-MAN-VER$-->
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.8.RELEASE</version><!--$NO-MVN-MAN-VER$-->
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
I've extended SpringBootServletInitializer in the main class. As seen in pom.xml I also set the tag for the tomcat dependency. Everything works fine when tested on the local Apache Tomcat server. What could be the issue here?
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultValidator' defined in class path resource [org/springframework/boot/autoconfigure/validation/ValidationAutoConfiguration.class]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.hibernate.validator.internal.engine.valueextraction.ValueExtractorManager
Might be because of this, the war is not deployed.
Make sure you have all the libraries in place while making the WAR.
Please check the library is used only for compile or something.

Spring boot shows 'can not be resolved' even if the dependency exists in classpath

I have a spring boot application having starter-data-jpa dependency added to pom.xml. I can clearly find the javax.persistence package present in maven dependency section. But I am unable to use #Entity annotation. Upon manually entering import javax.persistence.Entity eclipse shows unresolved dependency.
Thanks in advance.
here is my pom.xml file.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springframework.boot</groupId>
<artifactId>currency-exchange-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>currency-exchange-serviceweb</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
You're specifying <scope>test</scope> for the JPA starter, which specifically tells Maven that you only want that dependency available for test classes. Remove the scope and it should work.
Try removing <scope> for dependency JPA starter from pom.xml, do mvn clean install -U. this should solve your issue.
You should remove <scope>test</scope> from the spring-boot-starter-data-jpa dependency first.
If it still doesn't work, verify that your project runs when using Maven directly, by using the following command within a terminal:
mvn spring-boot:run
If this command works, reimport your project within your IDE, and test again. Often, the IDE doesn't immediately update the classpath, and is at fault.

Categories

Resources