将外部jar添加到Spring Boot Maven项目

时间:2019-10-25 05:42:36

标签: java spring spring-boot dependency-injection

我有两个Spring Boot项目

  1. 第一个项目包含我的Entity,DTO和REPO类

  2. 第二个包含所有Controller和服务

所以在这里,我们要做的工作是在第二个项目的Pom文件中添加依赖项后,将我的实体项目的jar创建为mavan依赖项添加到第二个项目中,我也在.m2或第一个项目目录中添加jar

在pom中添加mavan依赖项第一个项目后,没有收到任何错误,但是当我尝试访问任何类或我的第一个项目无法在类中找到时

第二个项目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.1.9.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.coreservice</groupId>
    <artifactId>bms-coreservice</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>bms-coreservice</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-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.bms</groupId>
            <artifactId>dataload</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        <!--    <scope>system</scope>
            <systemPath>${project.basedir}/lib/dataload-0.0.1-SNAPSHOT.jar</systemPath> -->
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.kafka</groupId>
            <artifactId>spring-kafka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.kafka</groupId>
            <artifactId>spring-kafka-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</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-security</artifactId>
        </dependency>
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>0.9.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.github.munerf.modelmapper-spring-boot-starter/modelmapper-spring-boot-starter -->
        <dependency>
            <groupId>com.github.munerf.modelmapper-spring-boot-starter</groupId>
            <artifactId>modelmapper-spring-boot-starter</artifactId>
            <version>0.0.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/postgresql/postgresql -->


    </dependencies>

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

添加显示jar的Dependency mavan repo列表后

enter image description here

我也尝试

<repositories>
        <repository>
            <id>in-project</id>
            <name>In Project Repo</name>
            <url>file://${basedir}\src\lib\dataload-0.0.1-SNAPSHOT.jar</url>
        </repository>
    </repositories>

2 个答案:

答案 0 :(得分:1)

  

首先,您可以将此代码放入pom文件中,您要将哪个项目注入另一个项目

     

For example this is DAO ' s pom file

<modelVersion>4.0.0</modelVersion>
<groupId>tr.com.project</groupId>
<artifactId>project-dao</artifactId>
<version>0.0.1-SNAPSHOT</version>

<properties>
    <java.version>1.8</java.version>
    <application.module.version>0.0.1-SNAPSHOT</application.module.version>
</properties>

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.1.1</version>
            </plugin>
        </plugins>
    </pluginManagement>
</build>
  

您可以访问该DAO的另一个项目   您必须将此代码放入另一个项目的pom

    <dependency>
        <groupId>tr.com.project</groupId>
        <artifactId>project.dao</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>

答案 1 :(得分:0)

您可以使用maven安装外部JAR的项目:

mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> \
    -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>

或者如果jar是使用maven构建的,那么这已经足够了:

mvn install:install-file -Dfile=<path-to-file>

然后,该项目将仅在您的maven存储库中可用,您可以像对待其他任何maven依赖项一样将其包括在内。