maven 导 工具类jar包

在项目中经常有比较多的工具类方法,每次使用的时候都是要打开多个项目去找对应的工具方法,或者去网上搜索,太费事,不如自己整个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")
@Configuration
public 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文件,导出了好几次都是没有依赖包,调用工具类方法时,直接报类找不到异常)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值