0

I have created two projects

  1. common entity project provides entities and repositories and deployed as a package in GitHub.

  2. sample project uses the common entity project package as a dependency

but repositories are not @Autowirded and entities tables are not generated in my second project. project configuration are given below.

the common entity project 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 
    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.6.RELEASE</version>
        <relativePath/>
    </parent>

    <groupId>com.orbund</groupId>
    <artifactId>galactic-be-common-entities</artifactId>
    <version>0.0.13</version>
    <packaging>jar</packaging>
    <name>galactic-be-common-entities</name>
    <description>Orbund galactic backend common entities project for School Information 
    System</description>

    <properties>
        <java.version>1.8</java.version>
        <github.global.server>github</github.global.server>
    </properties>

    <profiles>
        <profile>
            <id>develop</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <spring.profiles.active>develop</spring.profiles.active>
            </properties>
        </profile>
        <profile>
            <id>release</id>
            <properties>
                <spring.profiles.active>release</spring.profiles.active>
            </properties>
        </profile>
    </profiles>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <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.liquibase</groupId>
            <artifactId>liquibase-maven-plugin</artifactId>
            <version>3.4.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <configuration>
                            <classifier>exec</classifier>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.liquibase</groupId>
                <artifactId>liquibase-maven-plugin</artifactId>
                <version>3.4.1</version>
                <configuration>
                    <propertyFile>src/main/resources/liquibase.properties</propertyFile>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <distributionManagement>
        <repository>
            <id>github</id>
            <name>Galactic Maven Packages</name>
            <url>https://maven.pkg.github.com/shamim-orbund/mvnrepo</url>
        </repository>
    </distributionManagement>

</project>

and I have configured repositories as

@Configuration
@EnableJpaRepositories("com.orbund.galactic.be.common.entities.repository")
@EntityScan(basePackages = {"com.orbund.galactic.be.common.entities.entity"})
public class RepositoryConfig {

}

The second project 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 
    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.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.orbund</groupId>
    <artifactId>galactic-be-common-sample</artifactId>
    <version>0.0.1</version>
    <name>galactic-be-common-sample</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.orbund</groupId>
            <artifactId>galactic-be-common-entities</artifactId>
            <version>0.0.13</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <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.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <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>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <requiresUnpack>
                        <dependency>
                            <groupId>com.orbund</groupId>
                            <artifactId>galactic-be-common-entities</artifactId>
                            <version>0.0.13</version>
                        </dependency>
                    </requiresUnpack>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Any suggestions and help are appreciated. Thanks in advance.

Shamim
  • 11
  • 2
  • You could change the spring logging to debug to see what is going on – bichito Apr 11 '20 at 12:50
  • I get java.lang.ClassNotFoundException: org.springframework.data.jpa.repository.JpaRepository @efekctive – Shamim Apr 11 '20 at 12:59
  • Now you know what could be the root cause – bichito Apr 11 '20 at 13:05
  • @efekctive I know the root cause and it can be solvable if it was single project by @EnableJpaRepositories("com.orbund.galactic.be.common.entities.repository") and I configured it before. But the repository is in another project and external jar is used as dependency in another project and it is not working. – Shamim Apr 12 '20 at 01:05
  • 1
    If https://stackoverflow.com/questions/5269450/multiple-packages-in-contextcomponent-scan-spring-config does not help you, Spring will not be able to process your annotated components. they need to be in the path. – bichito Apr 12 '20 at 01:21
  • it does not help, the question is Autowired is not working if repository is in an external jar and how do I resolve it. @efekctive do you have an example that works with an external jar with the repository. I have some hurry and need to submit a prototype project. Any help is appreciable. – Shamim Apr 12 '20 at 02:44
  • During load the spring IOC scans everything in its classpath looking for anything that has an annotation. If your autowired is not being picked up it is because it isn’t in the classpath or there is a bug in spring. – bichito Apr 12 '20 at 02:49

1 Answers1

1

Finally, I solved the issue by importing the first project application to the second project application. Now spring scans entities and repositories classpath and generated beans as necessary.

@SpringBootApplication
@Import(GalacticBeCommonEntitiesApplication.class)
public class GalacticBeCommonSampleApplication {
    public static void main(String[] args) {
        SpringApplication.run(GalacticBeCommonSampleApplication.class, args);
    }
}
Shamim
  • 11
  • 2