Mohamed Sweelam
Mohamed Sweelam

Reputation: 1229

Spring boot deployments could not see .class file

In my project i build a multi modules dependency application , it is running perfectly in compilation/development mode , but when packaging it in a war file , it gives me NoClassDefFound for entity class which I used in Controller module.

The project structure is as follow :
 1. Project root 
    1.1 Controller dependency module
    1.2 Model dependency module
    1.3 Core dependency module

and the root pom file is packaging as pom and all others are Jar except Controller module which is packaging as War file; as a test I tried to build only Controller with Model dependency only and the pom file for both are attached here :

Root pom 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>

<groupId>com.egabi.fatca</groupId>
<artifactId>fatca</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<name>fatca</name>
<url>http://maven.apache.org</url>
<description>Structure project for FATCA</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.10.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>

<modules>
    <module>model</module>
    <module>common</module>
    <module>core</module>
    <module>api</module>
</modules>


<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>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

<!--<distributionManagement>-->
    <!--<repository>-->
        <!--<id>releases</id>-->
        <!--<url>http://10.3.1.73:9990/content/repositories/releases</url>-->
    <!--</repository>-->
<!--</distributionManagement>--></project>

The Model pom 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>

<artifactId>fatca-model</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<url>http://maven.apache.org</url>
<name>model</name>
<description>Model layer for fatca</description>

<parent>
    <groupId>com.egabi.fatca</groupId>
    <artifactId>fatca</artifactId>
    <version>1.0.0</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc7</artifactId>
        <version>12.1.0.1</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
</project>

and finally the Controller pom 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>

<artifactId>fatca-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>api</name>
<description>Controller layer for fatca</description>

<parent>
    <groupId>com.egabi.fatca</groupId>
    <artifactId>fatca</artifactId>
    <version>1.0.0</version>
</parent>

<properties>
    <start-class>com.egabi.fatca.FatcaApplication</start-class>
    <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>com.egabi.fatca</groupId>
        <artifactId>fatca-model</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

<!--<repositories>-->
    <!--<repository>-->
        <!--<id>spring-snapshots</id>-->
        <!--<name>Spring Snapshots</name>-->
        <!--<url>http://repo.spring.io/snapshot</url>-->
        <!--<snapshots>-->
            <!--<enabled>true</enabled>-->
        <!--</snapshots>-->
    <!--</repository>-->
    <!--<repository>-->
        <!--<id>spring-milestones</id>-->
        <!--<name>Spring Milestones</name>-->
        <!--<url>http://repo.spring.io/milestone</url>-->
        <!--<snapshots>-->
            <!--<enabled>false</enabled>-->
        <!--</snapshots>-->
    <!--</repository>-->
<!--</repositories>-->
<!--<pluginRepositories>-->
    <!--<pluginRepository>-->
        <!--<id>spring-snapshots</id>-->
        <!--<name>Spring Snapshots</name>-->
        <!--<url>http://repo.spring.io/snapshot</url>-->
        <!--<snapshots>-->
            <!--<enabled>true</enabled>-->
        <!--</snapshots>-->
    <!--</pluginRepository>-->
    <!--<pluginRepository>-->
        <!--<id>spring-milestones</id>-->
        <!--<name>Spring Milestones</name>-->
        <!--<url>http://repo.spring.io/milestone</url>-->
        <!--<snapshots>-->
            <!--<enabled>false</enabled>-->
        <!--</snapshots>-->
    <!--</pluginRepository>-->
<!--</pluginRepositories>-->

<!--<build>-->
    <!--<plugins>-->
        <!--<plugin>-->
            <!--<groupId>org.springframework.boot</groupId>-->
            <!--<artifactId>spring-boot-maven-plugin</artifactId>-->
        <!--</plugin>-->
    <!--</plugins>-->
<!--</build>-->
</project>

Also note i figured out that the model jar is exist in the final generated war ; and after investigating i found that parent module like api cannot see the packages defined in its child modules like Model , Core ,etc.

I tried to use @ComponentScan and it worked between 2 modules only, so my issue now is to make api parent see all his child modules .

I searched for my question in some solution here but it did not solve my issue , so your help is appreciated.

Upvotes: 3

Views: 2067

Answers (3)

willbe17
willbe17

Reputation: 1

you have set the @ComponentScan with the right path to find the file. something like this.

@Configuration
@ComponentScan("com.your.path")
public class ContextWithJavaConfig {
}

or for multiple paths, something like this.

@Configuration
@ComponentScan(basePackages={"com.firstpackage","com.secondpackage"})
public class ContextWithJavaConfig {
}

Upvotes: 0

Htiti Oussama
Htiti Oussama

Reputation: 1

i think you need to use this annotations

@EnableJpaRepositories(basePackages = "your.path.to.module.data.dao") @EntityScan(basePackages = "your.path.to.module.data.entity")

Upvotes: 0

Essex Boy
Essex Boy

Reputation: 7950

A few things:

In your child project the dependency should be

<dependency>
  <groupId>com.egabi.fatca</groupId>
  <artifactId>fatca-model</artifactId>
  <version>${project.version}</version>
</dependency>

Also your child projects needs only an artifactId, they will get their groupId and version from the parent, take those tags out (version and groupId) from the children.

Build the whole thing as one multi project from the root:

mvn clean install

Upvotes: 0

Related Questions