关键词

详解Spring Boot使用Maven自定义打包方式

下面是详解Spring Boot使用Maven自定义打包方式的攻略:

1. 配置打包插件

  • 在pom.xml文件中添加如下插件和依赖:
<build>
    <plugins>
        <!-- 配置打包插件 -->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <mainClass>${start-class}</mainClass>
                <layout>ZIP</layout>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

<dependencies>
    <!-- 添加Maven依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

其中,spring-boot-starter-web是示例依赖,可根据实际情况进行修改。

  • 运行命令mvn clean package进行打包。打包后将生成名为xxx.jar的可执行jar包。

2. 自定义打包类型

如果我们需要自定义打包类型,例如生成tar.gz格式的压缩包,我们可以按照以下步骤进行配置:

  • 添加插件和依赖:
<build>
    <plugins>
        <!-- 配置Tar打包插件 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.1.1</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>tar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <finalName>${project.artifactId}-${project.version}</finalName>
                <appendAssemblyId>false</appendAssemblyId>
            </configuration>
            <executions>
                <execution>
                    <id>package-tar</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.apache.maven.shared</groupId>
                    <artifactId>maven-common-artifact-filters</artifactId>
                    <version>3.0.1</version>
                </dependency>
                <dependency>
                    <groupId>org.codehaus.plexus</groupId>
                    <artifactId>plexus-utils</artifactId>
                    <version>3.2.1</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

<dependencies>
    <!-- 添加Maven依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- 添加Tar依赖 -->
    <dependency>
        <groupId>org.apache.maven.shared</groupId>
        <artifactId>maven-common-artifact-filters</artifactId>
        <version>3.0.1</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.codehaus.plexus</groupId>
        <artifactId>plexus-utils</artifactId>
        <version>3.2.1</version>
        <scope>runtime</scope>
    </dependency>
</dependencies>
  • src/main/assembly目录下创建一个名为tar.xml的文件,内容如下:
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
    <id>tar</id>
    <formats>
        <format>tar.gz</format>
    </formats>
    <fileSets>
        <fileSet>
            <includes>
                <include>*.jar</include>
            </includes>
            <directory>${project.build.directory}</directory>
            <outputDirectory>/</outputDirectory>
            <filtered>true</filtered>
            <fileMode>0755</fileMode>
        </fileSet>
    </fileSets>
</assembly>
  • 修改pom.xml,增加如下插件和配置:
<build>
    <plugins>
        <!-- 配置打包插件 -->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <mainClass>${start-class}</mainClass>
                <layout>ZIP</layout>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <!-- 配置Tar打包插件 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.1.1</version>
            <configuration>
                <finalName>${project.artifactId}-${project.version}</finalName>
                <descriptors>
                    <descriptor>src/main/assembly/tar.xml</descriptor>
                </descriptors>
                <appendAssemblyId>false</appendAssemblyId>
            </configuration>
            <executions>
                <execution>
                    <id>package-tar</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.apache.maven.shared</groupId>
                    <artifactId>maven-common-artifact-filters</artifactId>
                    <version>3.0.1</version>
                </dependency>
                <dependency>
                    <groupId>org.codehaus.plexus</groupId>
                    <artifactId>plexus-utils</artifactId>
                    <version>3.2.1</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>
  • 运行命令mvn clean package进行打包。打包后将生成名为xxx.tar.gz的tar.gz格式压缩包。

以上是关于如何自定义打包方式的详解,下面给出两个示例:

示例一

tar.xml文件中,我们可以通过修改fileMode属性来修改打包后的文件权限:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
    <id>tar</id>
    <formats>
        <format>tar.gz</format>
    </formats>
    <fileSets>
        <fileSet>
            <includes>
                <include>*.jar</include>
            </includes>
            <directory>${project.build.directory}</directory>
            <outputDirectory>/</outputDirectory>
            <filtered>true</filtered>
            <fileMode>0777</fileMode>
        </fileSet>
    </fileSets>
</assembly>

上面的配置将会使生成的文件具有读、写、执行权限。

示例二

tar.xml文件中,我们可以通过增加assembly元素的includeBaseDirectory属性来强制打包完成后将jar包文件所在的目录也一起打包进去:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
    <id>tar</id>
    <formats>
        <format>tar.gz</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <includes>
                <include>*.jar</include>
            </includes>
            <directory>${project.build.directory}</directory>
            <outputDirectory>/</outputDirectory>
            <filtered>true</filtered>
        </fileSet>
    </fileSets>
</assembly>

上面的配置将会使生成的压缩包中包含jar所在的目录。

本文链接:http://task.lmcjl.com/news/13127.html

展开阅读全文