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

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

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



完成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();
    }
}
java 复制代码
public class App
{
    public static void main( String[] args )
    {
        EventQueue.invokeLater(() -> {
            new InfoFrame().setVisible(true);
        });
    }
}

代码结构如下:

引入maven-assembly-plugin插件打包

xml 复制代码
<?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>org.hbin</groupId>
  <artifactId>info</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>info</name>
  <url>www.binbin.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>


  <build>
    <plugins>
      <!-- 使用maven-assembly-plugin插件打包 -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.2.0</version>
        <configuration>
          <archive>
            <manifest>
              <!--主类 -->
              <mainClass>org.hbin.App</mainClass>
            </manifest>
          </archive>
          <descriptorRefs>
            <!-- 可执行jar名称结尾-->
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

执行maven package

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

运行命令:

shell 复制代码
> java -jar info-1.0-SNAPSHOT-jar-with-dependencies.jar

文档包和源码包

xml 复制代码
<!--生成doc jar包-->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-javadoc-plugin</artifactId>
    <executions>
        <execution>
            <id>attach-javadocs</id>
            <goals>
                <goal>jar</goal>
            </goals>
            <!-- 不让像@Param 这种后面没写值的东西 报错。-->
            <configuration>
                <additionalJOption>-Xdoclint:none</additionalJOption>
            </configuration>
        </execution>
    </executions>
</plugin>

<!--生成源码jar包-->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-source-plugin</artifactId>
    <executions>
        <execution>
            <id>attach-sources</id>
            <goals>
                <goal>jar</goal>
            </goals>
        </execution>
    </executions>
</plugin>

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

相关推荐
一只笨菜鸟21 小时前
IDEA如何让多个项目同时显示
java·ide·intellij-idea
devilnumber2 天前
避坑_IDEA 提取方法(Extract Method)工具
java·ide·intellij-idea
梁云亮7 天前
在 IDEA 中运行 Tomcat 时,直接在 server.xml 中配置的 <Context> 无效
tomcat·intellij-idea·虚拟路径
开开心心_Every8 天前
内存清理工具定时自动清理开机自启动
java·开发语言·spring·计算机外设·ocr·intellij-idea·excel
小蜗牛狂飙记10 天前
Windows 双击 Jar 包无反应,不用写 bat 脚本!Jarfix 一键修复(以 Struts2 扫描工具为例)
windows·struts·jar
开开心心就好10 天前
视频里的图片怎么提取?双击一下就导出
java·前端·人工智能·spring·智能手机·intellij-idea·excel
省钱兄--zs10 天前
西安24小时自助健身房解决方案实战:系统开发与部署全流程指南
java·spring boot·系统架构·intellij-idea·需求分析
GoodStudyAndDayDayUp11 天前
一个简单的java jar跑docker
java·docker·jar
开开心心_Every11 天前
电脑OCR识别软件支持表格识别图片转Excel
java·开发语言·微信·计算机外设·ocr·intellij-idea·excel
MandalaO_O12 天前
IDEA 开发(快捷键 + 调试 + 序列化)
java·ide·intellij-idea