maven 导 工具类jar包
【摘要】 在项目中经常有比较多的工具类方法,每次使用的时候都是要打开多个项目去找对应的工具方法,或者去网上搜索,太费事,不如自己整个jar包,导入到maven库,即能重复使用,又避免了复制代码。(导jar包里面有个坑,也可能是因为我菜,没有很快找到解决方法)1.new project2.之后如图3.指定groupId 和artifactId以及版本 next4.指定项目名称以及存储位置。pom 文件<...
在项目中经常有比较多的工具类方法,每次使用的时候都是要打开多个项目去找对应的工具方法,或者去网上搜索,太费事,不如自己整个jar包,导入到maven库,即能重复使用,又避免了复制代码。(导jar包里面有个坑,也可能是因为我菜,没有很快找到解决方法)
1.new project
2.之后如图
3.指定groupId 和artifactId以及版本 next
4.指定项目名称以及存储位置。
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.abc</groupId>
<artifactId>framework</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>
<name>framework-auto</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<useUniqueVersions>false</useUniqueVersions>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>入口类package.有main方法的类</mainClass>
</manifest>
<manifestEntries>
<version>0.0.1</version>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build></project>
之后是入口类:
public class Main {
public static void main(String[] args) {
System.out.println("hello jar");
}}
然后是工具类,因为多数项目都是spring boot类型的就是使用注解写了一个例子,并从配置中取值。
import org.apache.commons.lang3.StringUtils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.Configuration;import java.net.Inet4Address;import java.net.InetAddress;import java.net.NetworkInterface;import java.util.Enumeration;import java.util.HashMap;import java.util.Map;@ConfigurationProperties(prefix = "sms")@Configurationpublic class SmsSendUtil {
private static final Logger logger = LoggerFactory.getLogger(SmsSendUtil.class);
private String appKey;
public String getAppKey() {
return appKey;
}
public void setAppKey(String appKey) {
this.appKey = appKey;
}
/**
* 获取主机ip
* @return
* @throws Exception
*/
public String getIpAddress() throws Exception{
if (StringUtils.isBlank(appKey)){
throw new Exception("没有配置sms.appKey");
}
Enumeration allNetInterfaces = NetworkInterface.getNetworkInterfaces();
while (allNetInterfaces.hasMoreElements()){
NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();// System.out.println(netInterface.getName());
Enumeration addresses = netInterface.getInetAddresses();
InetAddress ip = null;
while (addresses.hasMoreElements()){
ip = (InetAddress) addresses.nextElement();
if (ip != null && ip instanceof Inet4Address){
//剔除 127.0.0.1 和作为网关的.1 地址
if (!ip.getHostAddress().startsWith("127") && !ip.getHostAddress().endsWith(".1")){
return ip.getHostAddress();
}
}
}
}
return "";
}}
之后是(双击666)
最后执行mvn install 命令,把生成的jar导入到本地库中。
引用时在pom文件中加入
<dependency>
<groupId>com.abc</groupId>
<artifactId>framework</artifactId>
<version>0.0.1</version></dependency>
测试类:
@RestController@RequestMapping("/test")public class TestContrller {
@Resource
private CoreProperties coreProperties;
@Resource
private SmsSendUtil smsSendUtil;
@RequestMapping(value = {"/",""})
public String hellTask() throws Exception {
String userName = coreProperties.getAppKey();
System.out.println("========================================");
String ipAddress = smsSendUtil.getIpAddress();
return "hello " + userName + "ip" + ipAddress;
}}
application.properties
sms.appKey=ajlfjafl
(主要的坑在pom文件,导出了好几次都是没有依赖包,调用工具类方法时,直接报类找不到异常)
【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
作者其他文章
评论(0)