maven项目引入外部依赖jar包

1.创建lib文件夹,将自己需要的jar包放进去,选中lib包,点击鼠标右键,选择add as library,点击后jar包左侧会有个三角形箭头。

在这里插入图片描述

2.pom文件中添加依赖,此处以fastjson-1.1.41.jar举例。

<dependency>
     <groupId>fastjson</groupId>		//可以随便写,一般写真实groupId
     <artifactId>fastjson</artifactId>	//可以随便写,一般写jar包名称
     <scope>system</scope>				
     <version>1.1.41</version>			//可以随便写,最好写真实版本
     <systemPath>${pom.basedir}/lib/fastjson-1.1.41.jar</systemPath>  
     //${pom.basedir} 代表根目录,地址正确的话,ctrl+鼠标左键可以直接点过去,这里填对应jar包的路径
 </dependency>
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib-2.2.2</artifactId>
            <scope>system</scope>
            <version>2.2.2</version>
            <systemPath>${pom.basedir}/lib/cglib-2.2.2.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <scope>system</scope>
            <version>1.6.1</version>
            <systemPath>${pom.basedir}/lib/dom4j-1.6.1.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>fastjson</groupId>
            <artifactId>fastjson</artifactId>
            <scope>system</scope>
            <version>1.1.41</version>
            <systemPath>${pom.basedir}/lib/fastjson-1.1.41.jar</systemPath>
        </dependency>

3.打包时也要打进去,添加相关配置

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                    //只需要在<configuration>中额外添加下面这一行即可
                    <includeSystemScope>true</includeSystemScope>	
                </configuration>
            </plugin>
        </plugins>
    </build>

4.引入后测试,能成功使用fastjson相关代码。

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class ImportJarTest {

    public static void main(String[] args) {
        String data = "{\n" +
                "    \"name\": \"张三\",\n" +
                "    \"age\": \"23\"\n" +
                "}";
        JSONObject json = JSON.parseObject(data);
        Object name = json.get("name");
        System.out.println(name);
    }
}

更多推荐