普通Maven项目打包jar,提供第三方调用

新建本地项目pom.xml


<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.0modelVersion>

    <groupId>com.licfgroupId>
    <artifactId>loggerartifactId>
    <version>1.0.0version>
    <dependencies>
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>fastjsonartifactId>
            <version>1.2.56version>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <configuration>
                    <source>1.8source>
                    <target>1.8target>
                configuration>
            plugin>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-jar-pluginartifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>trueaddClasspath>
                            <useUniqueVersions>falseuseUniqueVersions>
                            <classpathPrefix>lib/classpathPrefix>
                        manifest>
                    archive>
                configuration>
            plugin>
        plugins>
    build>
project>

建立本地工程

在src/main/java下建包,包名为com.licf.logger。在此包下建立Logger类。内容如下:

package com.licf.logger;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.util.HashMap;
import java.util.Map;

public class Logger {
    public static String getSysInfo() {
        Map<String, Object> map = new HashMap<>();
        map.put("user", "licanfeng");
        map.put("password", "123456");
        return JSON.toJSONString(map, SerializerFeature.WriteMapNullValue);
    }
}

打包到本地仓库

使用mvn install命令打包到本地仓库,使用mvn package打包到本地。

第三方调用

在pom.xml文件中引入依赖:

        <dependency>
            <groupId>com.licfgroupId>
            <artifactId>loggerartifactId>
            <version>1.0.0version>
        dependency>

在项目中使用:

import com.licf.logger.Logger;

//示例如下
System.out.println(Logger.getSysInfo());

你可能感兴趣的:(工具)