Maven packaging can generally generate two types of packages: one is a package that can be run directly, and the other is a dependent package (just a compiled package). Maven packages jar by default, if you need to modify other types. You can modify pom.xml
<packaging>jar/ear/ejb</packaging>
The plug-ins introduced by default in the Maven project:
The generated jar is just a compiled package and does not package dependent jar packages. It can be seen from the compilation log of the console that the maven-jar-plugin:2.4 plug-in is used.
Java compilation plug-in, you can set the jdk version, etc. (If you don’t set it, use the default, you don’t need to set it
<!-- java编译插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin>
Console execution mvn clean package
Maven->Plugins->clean->package
This kind of generated jar can generally be run directly through java -jar.
This method is to use the spring-boot-maven-plugin plugin for packaging.
The first step: add
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.4.0</version> <configuration> <fork>true</fork> <!--指定启动的入口,可以省略,springboot会自动查找启动类--> <mainClass>com.lx.buildjarpackage.BuildJarPackageApp</mainClass> </configuration> </plugin>to pom.xml
Second step: Console execution
mvn clean package spring-boot:repackage
Note: If parent (optional) is specified in the current pom file
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.0</version> </parent>
The console command only needs to be executed
mvn clean package
Output the dependent JAR package to the lib directory (the packaging method is common for JAVA projects). This method requires the introduction of the maven-jar-plugin plug-in .
The first step: Add pom.xml
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>com.lx.buildjarpackage.BuildJarPackageApp</mainClass> </manifest> </archive> </configuration> </plugin>
The second step: Console execution
mvn clean dependency :copy-dependencies -DoutputDirectory=target/lib package
Note: If we want to simplify the console execution command, we can add plug-in configuration
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib</outputDirectory> </configuration> </execution> </executions> </plugin>
and then console execution
mvn clean package
Method 3: Dependent packages and projects in one jar (non-springboot project)
This method uses the maven-assembly-plugin plug-in, but This method does not copy the dependent jar to the final jar, but copies the class file that depends on the jar to the final jar. Therefore, some resource files will be lost in this method, so the springboot project will fail to start.
First step: Add pom.xml
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.5.5</version> <configuration> <archive> <manifest> <mainClass>com.lx.buildjarpackage.BuildJarPackageApp</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin>
Second step: Console execution
mvn clean assembly:single
Or operate the maven project with the mouse (right column)->Select Plugins->Select assembly->Click assembly:assembly
Summary: It can only be used for springboot projects Method 1 and 2 can start normally.
<plugin> <groupId>org.scala-tools</groupId> <artifactId>maven-scala-plugin</artifactId> <executions> <execution> <goals> <goal>compile</goal> <goal>testCompile</goal> </goals> </execution> </executions> <configuration> <scalaVersion>${scala.version}</scalaVersion> <args> <arg>-target:jvm-1.5</arg> </args> </configuration> </plugin>
maven clean package
or mouse execution maven project (right column)->Select Lifecycle->Click package or install package
<plugin> <groupId>org.codehaus.gmavenplus</groupId> <artifactId>gmavenplus-plugin</artifactId> <version>1.2</version> <executions> <execution> <goals> <goal>addSources</goal> <goal>addStubSources</goal> <goal>compile</goal> <goal>execute</goal> </goals> </execution> </executions> </plugin>
maven clean package
Or execute maven project with the mouse (right column)->Select Lifecycle->Click package or install to package
The above is the detailed content of What is the method for packaging jar in Maven project in Java?. For more information, please follow other related articles on the PHP Chinese website!