A simple method for creating JAR packages for MAVEN projects in IDEA, ideamaven
Idea has its own method for creating Jar packages for general non-Web projects. You can find a lot on the Internet.
However, it is very easy to create a Jar package for a Maven project, because maven itself has a command to create a Jar package.
The simplest method
First, add the packaging plug-in to the pom. xml file of the maven project. There are many methods. The simplest method is to use only the maven-compiler-plugin and maven-jar-plugin plug-ins, and specify the program entry <mainClass>. The related code is 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> <useUniqueVersions>false</useUniqueVersions> <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. In Idea, all Maven project commands are made into a visual operation interface. You only need to perform the following operations:
Under the Maven Project directory, click package
In this case, the Jar package of this project will be generated under the target directory.
Run the java-jar command to run the Jar package and output "Hello World"
Notes
It should be noted that if a maven project contains multiple sub-directories, and the pom. xml in each sub-directory corresponds to a project, it takes effect only in this sub-directory. For example, if you want to scan the configuration file in a sub-directory pom. xml to scan the configuration file in another sub-directory, that is not possible. When creating a jar package, only the current pom. xml file is run.
Of course, there are other packaging methods, such as using the spring-boot-maven-plugin plug-in to introduce the dependency package when creating a Jar package.
Its pom. xml file is configured:
<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 for your learning and support for helping customers.