前言
自己定义自己的maven的jar包依赖,本地版本。
实现
pom.xml
pom.xml
<?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>org.example</groupId>
<artifactId>maven-dependency-learning</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- validation -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.0</version>
</dependency>
</dependencies>
<distributionManagement>
<repository>
<id>central</id>
<name>XXX-release</name>
<url>http://xx.xxxx.com.cn:80/artifactory/XXX-release/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>XXX-snapshot</name>
<url>http://xx.xxxx.com.cn:80/artifactory/XXX-snapshot/</url>
</snapshotRepository>
</distributionManagement>
<build>
<plugins>
<plugin>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<attach>true</attach>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
spring.factories
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.zwy.maven.learning.AutoConfiguration
AutoConfiguration.java
AutoConfiguration.java
package com.zwy.maven.learning;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan(
basePackages = {"com.zwy.maven.learning"}
)
public class AutoConfiguration {
public AutoConfiguration() {
}
}
utils.TestUtil
TestUtil.java
package com.zwy.maven.learning.utils;
public class TestUtil {
private static final String TEST_STRING = "test";
// 返回字符串
public static String returnString() {
return "通用代码库测试:返回字符串";
}
}
application.properties
application.properties
server.port=8080
项目结构
自定义的jar包的项目结构如下:
打包
将我们自定义的maven依赖打包,打包安装到本地maven库,依次执行:
mvn clean
mvn package
mvn install
可以看到maven把我们的jar包部署到本地maven库中。
我们在打印日志里可以拿到jar包地址,jar包安装地址其实是自己定义好的:
C:\Users\32610\.m2\repository\org\example\maven-dependency-learning\1.0-SNAPSHOT\maven-dependency-learning-1.0-SNAPSHOT.jar
找出来看看:
引入依赖
测试项目引入依赖:
<dependency>
<groupId>com.zwy.maven.learning</groupId>
<artifactId>maven-dependency-learning</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
在需要引入的项目中使用:
mvn install:install-file -Dfile=C:\Users\32610\.m2\repository\org\example\maven-dependency-learning\1.0-SNAPSHOT\maven-dependency-learning-1.0-SNAPSHOT.jar
还有其他方法:
mvn install:install-file -Dfile=C:\Users\32610\.m2\repository\org\example\maven-dependency-learning\1.0-SNAPSHOT\maven-dependency-learning-1.0-SNAPSHOT.jar -DgroupId=org.example -DartifactId=maven-dependency-learning -Dversion=1.0-SNAPSHOT -Dpackaging=jar
mvn install 完之后,然后再reload pom.xml文件:
这个时候自定义的jar包依赖就导入进来了。
测试
代码测试一下,在controller中直接引入我们写得TestUtil的函数:
package org.example.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zwy.maven.learning.utils.*;
@RestController
public class HelloController {
@RequestMapping("/sayHello")
public String sayHello(){
return TestUtil.returnString();
}
}
启动项目
调用接口测试一下:
成功!