• idea环境下如何打包可运行jar?


    工作中有时候偶尔写一些工具类、小程序,可是java程序员制作一个可运行jar实在折腾,利用idea开发环境,可以快速打包自己的可运行jar。具体怎么操作呢?

    创建一个空白的java项目并完成自己的程序开发

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    完成java代码:

    /**
     * 测试窗口
     * @author binbin
     * @date 2023/9/27 10:29
     */
    public class InfoFrame extends JFrame {
        public InfoFrame() {
            setTitle("System Information");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(300, 200);
    
            //居中显示
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            setLocation((screenSize.width - getWidth())/2, (screenSize.height - getHeight())/2);
    
            //初始化菜单
            JMenuBar bar = new JMenuBar();
            JMenu menu = new JMenu("帮助");
            JMenuItem exitItem = new JMenuItem("退出");
            exitItem.addActionListener(e -> {
                System.exit(0);
            });
            menu.add(exitItem);
            bar.add(menu);
            setJMenuBar(bar);
    
            //初始化系统信息
            JTextArea infoTextArea = new JTextArea(6, 10);
            infoTextArea.setText(getSystemInfo());
            infoTextArea.setEditable(false);
            add(new JScrollPane(infoTextArea));
        }
    
        private String getSystemInfo() {
            StringBuffer b = new StringBuffer();
            b.append("系统系统:").append(System.getProperty("os.name")).append("\r\n");
            b.append("系统版本:").append(System.getProperty("os.version")).append("\r\n");
            b.append("系统架构:").append(System.getProperty("os.arch")).append("\r\n");
            b.append("用户名称:").append(System.getProperty("user.name")).append("\r\n");
            b.append("用户主目录:").append(System.getProperty("user.home")).append("\r\n");
            b.append("当前工作目录:").append(System.getProperty("user.dir")).append("\r\n");
            return b.toString();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    public class App
    {
        public static void main( String[] args )
        {
            EventQueue.invokeLater(() -> {
                new InfoFrame().setVisible(true);
            });
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    代码结构如下:
    在这里插入图片描述

    引入maven-assembly-plugin插件打包

    
    
    <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>org.hbingroupId>
      <artifactId>infoartifactId>
      <version>1.0-SNAPSHOTversion>
    
      <name>infoname>
      <url>www.binbin.orgurl>
    
      <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <maven.compiler.source>1.8maven.compiler.source>
        <maven.compiler.target>1.8maven.compiler.target>
      properties>
    
      <dependencies>
        <dependency>
          <groupId>junitgroupId>
          <artifactId>junitartifactId>
          <version>4.11version>
          <scope>testscope>
        dependency>
      dependencies>
    
    
      <build>
        <plugins>
          
          <plugin>
            <groupId>org.apache.maven.pluginsgroupId>
            <artifactId>maven-assembly-pluginartifactId>
            <version>3.2.0version>
            <configuration>
              <archive>
                <manifest>
                  
                  <mainClass>org.hbin.AppmainClass>
                manifest>
              archive>
              <descriptorRefs>
                
                <descriptorRef>jar-with-dependenciesdescriptorRef>
              descriptorRefs>
            configuration>
            <executions>
              <execution>
                <id>make-assemblyid>
                <phase>packagephase>
                <goals>
                  <goal>singlegoal>
                goals>
              execution>
            executions>
          plugin>
        plugins>
      build>
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61

    在这里插入图片描述

    执行maven package

    执行maven package命令,target目录将生成一个以jar-with-dependencies结尾的可直接执行jar。
    运行命令:

    > java -jar info-1.0-SNAPSHOT-jar-with-dependencies.jar
    
    • 1

    在这里插入图片描述

    文档包和源码包

    
    <plugin>
        <groupId>org.apache.maven.pluginsgroupId>
        <artifactId>maven-javadoc-pluginartifactId>
        <executions>
            <execution>
                <id>attach-javadocsid>
                <goals>
                    <goal>jargoal>
                goals>
                
                <configuration>
                    <additionalJOption>-Xdoclint:noneadditionalJOption>
                configuration>
            execution>
        executions>
    plugin>
    
    
    <plugin>
        <groupId>org.apache.maven.pluginsgroupId>
        <artifactId>maven-source-pluginartifactId>
        <executions>
            <execution>
                <id>attach-sourcesid>
                <goals>
                    <goal>jargoal>
                goals>
            execution>
        executions>
    plugin>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    下次再有测试、运营或者其他部门的同事找你做工具,知道怎样快速制作可执行jar了吧?

  • 相关阅读:
    Dapr在Java中的实践 之 服务调用
    理解透C语言一维数组,二维数组这一篇就够啦!
    如何禁用Windows 10快速启动(以及为什么要这样做)
    企业可以自己建立大数据平台吗?有哪些好处?
    (附源码)计算机毕业设计JavaJava毕设项目社区物业管理系统
    v-if的使用
    前端权限管理方案之精确到按钮级别
    【JavaEE】网络编程
    docker 基础
    【Docker】利用Docker构建motion planner运动规划的开发环境
  • 原文地址:https://blog.csdn.net/binbinxyz/article/details/133349178