自学内容网 自学内容网

springboot项目maven引入本地包没打包进去

当在 Spring Boot 项目中引入本地的 Maven 依赖时,有时会遇到依赖未被正确打包进项目的情况。这可能是由于 Maven 依赖的范围(scope)或者构建配置问题所致。下面是一些可能的原因和解决方法,以及详细的代码介绍:

  1. 依赖范围(scope)问题

    • 如果你的本地依赖在 pom.xml 中使用了 scope 属性,并且范围设置不正确,可能会导致依赖无法正确打包。
    • 确保本地依赖的 scope 设置为 compile 或者 runtime,这样它会被打包进项目中。
    <dependency>
        <groupId>your.groupId</groupId>
        <artifactId>your-artifactId</artifactId>
        <version>your-version</version>
        <scope>compile</scope> <!-- 或者使用 runtime -->
    </dependency>
    
  2. Maven 构建配置问题

    • 确保你在本地项目的 pom.xml 文件中正确地添加了对本地依赖的引用。
    • 使用 Maven 的 install 命令将本地依赖安装到本地仓库中,以便项目可以正确地引用它。

下面是一个示例的 Spring Boot 项目结构和 pom.xml 文件的代码示例:

项目结构

your-project/
|-- src/
|   |-- main/
|       |-- java/
|       |-- resources/
|-- pom.xml

pom.xml 文件

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

    <groupId>your.groupId</groupId>
    <artifactId>your-artifactId</artifactId>
    <version>your-version</version>
    <packaging>jar</packaging>

    <name>your-project</name>
    <description>Spring Boot Project</description>

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

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <!-- Spring Boot Starter Dependencies -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- 本地依赖 -->
        <dependency>
            <groupId>your.local.dependency.groupId</groupId>
            <artifactId>your-local-dependency-artifactId</artifactId>
            <version>your-local-dependency-version</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

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

确保你在 <dependencies> 部分正确地添加了本地依赖,并且 scope 设置正确。然后,在项目根目录下执行 mvn clean install 命令来构建并安装项目到本地 Maven 仓库。这样你的 Spring Boot 项目就应该能够正确地引用和打包本地依赖了。


原文地址:https://blog.csdn.net/weixin_43784341/article/details/137854186

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!