idea打jar包并发布jar包到maven

Posted by     小兵兵 on Thursday, June 11, 2020

目录

idea打jar包

创建空项目

通过maven创建空项目,该项目主要写一些工具类,打成jar包,供其他人调用

image-20200611111401543

填写项目名称,点击Finish

image-20200611111436590

编码

写工具类

package com.zbiti.juc;

import java.util.concurrent.*;

public class ThreadCallableTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService tp = Executors.newFixedThreadPool(2);
        FutureTask<Integer> ft1 = new FutureTask<>(new MyCallable(100));
        FutureTask<Integer> ft2 = new FutureTask<>(new MyCallable(200));
        tp.execute(ft1);
        tp.execute(ft2);

        Integer res1 = ft1.get();
        Integer res2 = ft2.get();

        tp.shutdown();
        System.out.println(res1);
        System.out.println(res2);

    }
}

class MyCallable implements Callable<Integer> {
    private Integer number;

    public MyCallable(Integer number) {
        this.number = number;
    }

    @Override
    public Integer call() throws Exception {
        Integer sum = 0;
        for (int i = 1; i <= number; i++) {
            sum += i;
        }
        return sum;
    }
}

打jar包前设置

image-20200611113352247

Main Class:选择程序运行的入口类

JAR files from libraries:选择第二个

Directory for META-INF/MANIFEST.MF:选择F:\workspace\IntelliJ_IDEA2020\hellojar\src\main\resources这个资源目录

image-20200611113442390

打jar包

image-20200611143129716

测试

java -jar hellojar.jar

image-20200611143356232

发布本地jar包到maven

安装jar包到maven仓库

-Dfile是需要导入的jar包所在的路径

-DgroupId、-DartifactId、-Dversion应与pom.xml中使用时一致

mvn install:install-file -Dfile=F:\workspace\IntelliJ_IDEA2020\hellojar\out\artifacts\hellojar_jar\hellojar.jar -DgroupId=com.zbiti -DartifactId=juc -Dversion=1.0 -Dpackaging=jar

image-20200611143645012

image-20200611143745269

使用

导入依赖

image-20200611143917121

使用jar包里的类

image-20200611144230040