There is a way to jar packages for general non-Web projects in Idea, and you can find a lot of them by searching online.
However, if you are using Jar packages for Maven project, it is actually very simple, because maven itself has the command to use Jar packages.
The easiest way
First, add packaged plugins to the pom.xml of the maven project. There are many ways here. The easiest thing is to only use the maven-compiler-plugin and maven-jar-plugin plugins, and specify the program entry <mainClass>. The relevant codes are as follows:
The pom.xml file is:
<?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>cn.mymaven</groupId> <artifactId>test</artifactId> <version>1.0-SNAPSHOT</version> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <useUniqueVerssions>false</useUniqueVerssions> <classpathPrefix>lib/</classpathPrefix> <mainClass>cn.mymaven.test.TestMain</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> </build> </project>
The entry class TestMain.java is:
package cn.mymaven.test;public class TestMain { public static void main(String[] args){ System.out.println("Hello World"); }}Then start packaging, and make all the commands of the Maven project into a visual operation interface in Idea, just need to operate as follows:
In the Maven Project directory, click package
At this time, in the target directory, the Jar package for this project will be generated
Running this Jar package using the java -jar command will output "Hello World"
Things to note
It should be noted that if a maven project has multiple subdirectories, the pom.xml in each subdirectories corresponds to a project, and its scope of function is only in this subdirectories. For example, if you want to scan a configuration file, if you want to scan a configuration file in another subdirectory, you cannot do it. When typing jar packages, only the current pom.xml file is run.
Of course, there are other packaging methods, such as using the spring-boot-maven-plugin plugin to introduce dependency packages when using the Jar package.
Its pom.xml file is configured as:
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <useUniqueVersions>false</useUniqueVersions> <classpathPrefix>lib/</classpathPrefix> <mainClass>cn.mymaven.test.TestMain</mainClass> </manifest> <manifestEntries> <version>${project.version}</version> </manifestEntries> </archive> </configuration> </plugin> </plugins></build>The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.