Home > Java > javaTutorial > What is the method for packaging jar in Maven project in Java?

What is the method for packaging jar in Maven project in Java?

WBOY
Release: 2023-04-29 18:31:14
forward
2594 people have browsed it

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>
Copy after login

The plug-ins introduced by default in the Maven project:

What is the method for packaging jar in Maven project in Java?

1. General jar (cannot Run )

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>
Copy after login

Method 1: Command line method

Console execution mvn clean package

Method 2: Mouse operation

             Maven->Plugins->clean->package  

2. Executable Jar

This kind of generated jar can generally be run directly through java -jar.

Method 1: SpringBoot framework (depending on the jar)

This method is to use the spring-boot-maven-plugin plugin for packaging.

What is the method for packaging jar in Maven project in Java?

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>
Copy after login
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>
Copy after login

The console command only needs to be executed

mvn clean package

What is the method for packaging jar in Maven project in Java?

Method 2: Dependency package and project package Separation (depending on the external directory lib)

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>
Copy after login

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>
Copy after login

and then console execution

mvn clean package

What is the method for packaging jar in Maven project in Java?

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>
Copy after login

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

What is the method for packaging jar in Maven project in Java?

Summary: It can only be used for springboot projects Method 1 and 2 can start normally.

3. Scala project packaging

The first step: add

<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>
Copy after login

to the build>plugins of pom.xml The second step: Console execution

maven clean package

or mouse execution maven project (right column)->Select Lifecycle->Click package or install package

4 . Groovy project packaging

The first step: Add

<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>
Copy after login

to the build>plugins of pom.xmlThe second step: Console execution

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template