Maven构建可执行的jar包(包含依赖jar包)之maven-assembly-plugin介绍

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/russle/article/details/81840365
  • 1, 简介

先看官方文档:http://maven.apache.org/plugins/maven-assembly-plugin/index.html maven-assembly-plugin可以将依赖的第三方jar包打包到jar中,这样方便我们发布可执行的jar包。

  • 2, 用法

用法部分的官方文档:http://maven.apache.org/plugins/maven-assembly-plugin/index.html

      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.1.0</version>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id> <!-- this is used for inheritance merges -->
            <phase>package</phase> <!-- bind to the packaging phase -->
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
  • 3,具体示例

具体工程代码在这里。欢迎fork加星,谢谢!
我的pom文件如下

<?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>com.yq</groupId>
    <artifactId>MavenPluginDemo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>


    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- fastjson-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.1.33</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.5</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.7.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-spring-web</artifactId>
            <version>2.7.0</version>
        </dependency>
    </dependencies>


    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <build>
        <plugins>
            <!--<plugin>-->
                <!--<groupId>org.apache.maven.plugins</groupId>-->
                <!--<artifactId>maven-jar-plugin</artifactId>-->
                <!--<version>2.4</version>-->
                <!--<configuration>-->
                    <!--<archive>-->
                        <!--<manifest>-->
                            <!--<addClasspath>true</addClasspath>-->
                            <!--<classpathPrefix>lib/</classpathPrefix>-->
                            <!--<mainClass>com.yq.WebUserApplication</mainClass>-->
                        <!--</manifest>-->
                    <!--</archive>-->
                <!--</configuration>-->
            <!--</plugin>-->
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id> <!-- this is used for inheritance merges -->
                        <phase>package</phase> <!-- bind to the packaging phase -->
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!--<plugin>-->
                <!--<groupId>org.springframework.boot</groupId>-->
                <!--<artifactId>spring-boot-maven-plugin</artifactId>-->
            <!--</plugin>-->
        </plugins>
    </build>

</project>

执行mvn package

D:\E\workspaceGitub\springboot\MavenPluginDemo>mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.yq:MavenPluginDemo >-----------------------
[INFO] Building MavenPluginDemo 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.0.1:resources (default-resources) @ MavenPluginDemo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ MavenPluginDemo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 4 source files to D:\E\workspaceGitub\springboot\MavenPluginDemo\target\classes
[INFO]
[INFO] --- maven-resources-plugin:3.0.1:testResources (default-testResources) @ MavenPluginDemo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\E\workspaceGitub\springboot\MavenPluginDemo\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:testCompile (default-testCompile) @ MavenPluginDemo ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.20.1:test (default-test) @ MavenPluginDemo ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ MavenPluginDemo ---
[INFO] Building jar: D:\E\workspaceGitub\springboot\MavenPluginDemo\target\MavenPluginDemo-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-assembly-plugin:3.1.0:single (make-assembly) @ MavenPluginDemo ---
[INFO] Building jar: D:\E\workspaceGitub\springboot\MavenPluginDemo\target\MavenPluginDemo-1.0-SNAPSHOT-jar-with-dependencies.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 13.613 s
[INFO] Finished at: 2018-08-19T19:45:56+08:00
[INFO] ------------------------------------------------------------------------

D:\E\workspaceGitub\springboot\MavenPluginDemo>

这里写图片描述
此时执行>java -jar MavenPluginDemo-1.0-SNAPSHOT-jar-with-dependencies.jar
会报错误:MavenPluginDemo-1.0-SNAPSHOT-jar-with-dependencies.jar中没有主清单属性

修改pom文件中maven-assembly-plugin的设置

            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>com.yq.WebUserApplication</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id> <!-- this is used for inheritance merges -->
                        <phase>package</phase> <!-- bind to the packaging phase -->
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

注意: **

非常重要。

**
本文示例代码是个spring boot工程,因此一般使用spring-boot-maven-plugin最适合。

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

关于spring-boot-maven-plugin请参考https://docs.spring.io/spring-boot/docs/current/reference/html/build-tool-plugins-maven-plugin.html

本文使用了maven-assembly-plugin,生成的jar启动时报如下错误。导致项目无法正常运行(这是因为我们的项目是springboot的项目,如果是普通的以main函数所在类启动的,不涉及springboot特有东西的不会出现如下错误)
Caused by: java.lang.IllegalArgumentException: No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.
at org.springframework.util.Assert.notEmpty(Assert.java:450) ~[MavenPluginDemo-1.0-SNAPSHOT-jar-with-dependencies.jar:na]

猜你喜欢

转载自blog.csdn.net/russle/article/details/81840365