How to package a runnable jar in an idea environment?

Sometimes I occasionally write some tools and small programs at work, but it is very difficult for Java programmers to create a runnable jar. Using the idea development environment, you can quickly package your own runnable jar. How does it work?

Create a blank java project and complete your own program development

Insert image description here
Insert image description here
Insert image description here
Complete the java code:

/**
 * 测试窗口
 * @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();
    }
}

public class App
{
    
    
    public static void main( String[] args )
    {
    
    
        EventQueue.invokeLater(() -> {
    
    
            new InfoFrame().setVisible(true);
        });
    }
}

The code structure is as follows:
Insert image description here

Introducing maven-assembly-plugin plug-in packaging

<?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>

Insert image description here

Execute maven package

Execute the maven package command, and the target directory will generate a directly executable jar ending with jar-with-dependencies.
Run command:

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

Insert image description here

Documentation package and source code package

<!--生成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>

Next time colleagues from testing, operations or other departments ask you to make tools, do you know how to quickly make executable jars?

Guess you like

Origin blog.csdn.net/binbinxyz/article/details/133349178