结合nexus 建工具类 方便服务调用工具类
主要目的是测试引用外部依赖的公共方法。
写了个小例子,项目中的代码无实际意义。
新建工具类
- 新建TimeUnit类
import java.time.LocalDateTime;
/**
* @author qeq
* @date 2022-11-15 09:20
*/
public class TimeUnit {
public String getTime(){
LocalDateTime now = LocalDateTime.now();
int year = now.getYear();
int monthValue = now.getMonthValue();
int dayOfMonth = now.getDayOfMonth();
int hour = now.getHour();
int minute = now.getMinute();
int second = now.getSecond();
String str = year + "-" + monthValue + "-" + dayOfMonth + " " + hour + ":" + minute + ":" + second;
return str;
}
public static void main(String[] args) {
TimeUnit timeUnit = new TimeUnit();
String time = timeUnit.getTime();
System.out.println(time);
}
}
- 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.7.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.common</groupId>
<artifactId>common</artifactId>
<version>0.0.1</version>
<name>common</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<!--配置当前工程保存在私服中的具体位置-->
<distributionManagement>
<repository>
<!--和maven/settings.xml中server中的id一致,表示使用该id对应的用户名和密码-->
<id>test-release</id>
<!--release版本上传仓库的具体地址-->
<url>http://192.168.88.100:8081/repository/test-release/</url>
</repository>
<snapshotRepository>
<!--和maven/settings.xml中server中的id一致,表示使用该id对应的用户名和密码-->
<id>test-snapshot</id>
<!--snapshot版本上传仓库的具体地址-->
<url>http://192.168.88.100:8081/repository/test-snapshot/</url>
</snapshotRepository>
</distributionManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
请根据自己私服配置,更改私服的配置项,build构建小编踩了坑。
新建测试项目
- 新建一个服务,引入公共依赖
<dependency>
<groupId>com.common</groupId>
<artifactId>common</artifactId>
<version>0.0.1</version>
</dependency>
-
nexus 上传的依赖:
-
编写测试controller
import com.common.common.util.TimeUnit;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author qeq
* @date 2022-10-21 14:21
*/
@RestController
@RequestMapping("/testController")
public class TestController {
/**
* 127.0.0.1:8080/testController/test
* @author qeq
* @date 2022/10/21 14:22
* @return String
**/
@RequestMapping("test")
public String test(){
TimeUnit timeUnit = new TimeUnit();
String time = timeUnit.getTime();
return time;
}
}
- 测试之: