0

I have a class in an application (it is a Spring Boot project), for example:

package com.exemple.spring.springboot.model;

public class Employee {
   ...
}

And in the pom.xml I have this:

<?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.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.exemple.spring</groupId>
    <artifactId>spring-boot</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    ...

Now I want to use this class Employee in another application like this:

package com.exe.spring.controller;

import org.springframework.stereotype.Controller;
import com.exemple.spring.springboot.model.Employee;


@Controller
public class EmployeeController {

    public Employee getAnEmployee() {
       ...
    }
}

I've build the JAR with mvn clean install for the first app and I've added it in the pom.xml of the second app like this:

<?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.exe.spring</groupId>
    <artifactId>rest-client</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>rest-client</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

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

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.exemple.spring</groupId>
            <artifactId>spring-boot</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

    </dependencies>

And the problem is that I cannot import the Employee class in the EmployeeController class. I've imported it manually like this import com.exemple.spring.springboot.model.Employee;, but Intellij show me an error: Cannot resolve symbol 'springboot'. I see that the dependency is presented in the local repository. It is very weird because Eclipse doens't see anything to import for Employee, and Intellij see the class but when I try to click on "Import class" it doesn't import anything. What can I do to import it correctly? Thank you!

Yuan
  • 1
  • 3

2 Answers2

0

You can not directly add it as dependency, it has to be present in one of certral/ local / remote repository.

You need to add the jar to repo, e.g. local repoository,

mvn install:install-file -DlocalRepositoryPath=repo -DcreateChecksum=true -Dpackaging=jar -Dfile=[your-jar] -DgroupId=[...] -DartifactId=[...] -Dversion=[...]

And then add repository as,

<repository>
    <id>repo</id>
    <url>file://${project.basedir}/repo</url>
</repository>

Check other optios here

Vikas
  • 6,868
  • 4
  • 27
  • 41
  • Thank you for the reply, but I've did mvn clean install for the first app, and I see that it is presented in the local repository, and then I simply add the dependency in the pom.xml of the 2nd app. Isn't it enough? – Yuan Aug 24 '19 at 14:11
  • That is enough. – J Fabian Meier Aug 24 '19 at 19:17
0

Check follow:

  • You've used mvn clean install to build and install the first JAR.
  • You have your JAR in the classpath of the second app. You can see all of them in "External libraries" in idea. If not, reimport maven project or use auto-import option

Also you can use different maven project structure e.g :

Common parent
 |
 |-------> first jar 
 |
 |-------> second jar (with first one as a dependency)

I think it should work better for you.

Lyashko Kirill
  • 513
  • 3
  • 14