Maven 打包成 zip

2021-01-27 10:36:14 浏览数 (1)

代码语言:javascript复制
  <build>
    <finalName>打出来的包名</finalName>
    <plugins>

      <plugin>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <classpathPrefix>lib/</classpathPrefix>
              <mainClass>主类带包路径</mainClass>
            </manifest>
            <manifestEntries>
              <Class-Path>./</Class-Path>
            </manifestEntries>
          </archive>
          <excludes>
            <exclude>application.yml</exclude>
          </excludes>
        </configuration>
      </plugin>

      <!-- 比 maven-resources-plugin 灵活 -->
      <plugin>
        <artifactId>maven-antrun-plugin</artifactId>
        <executions>

          <!-- 拷贝配置文件和脚本文件 -->
          <execution>
            <id>base</id>
            <phase>package</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <!-- http://ant.apache.org/manual/Tasks/ -->
              <target>
                <copy todir="${project.build.directory}/" overwrite="true">
                  <fileset dir="${project.build.scriptSourceDirectory}"/>
                </copy>
                <!-- 默认false,这里加上去是为了方便临时修改 -->
                <copy todir="${project.build.directory}/" overwrite="false">
                  <fileset dir="${project.basedir}/src/main/resources">
                    <include name="application.yml"/>
                  </fileset>
                </copy>
              </target>
            </configuration>
          </execution>

          <!-- 把相关文件压缩成zip包 -->
          <execution>
            <id>zip</id>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <!-- http://ant.apache.org/manual/Tasks/ -->
              <target>
                <zip destfile="${project.build.directory}/${project.build.finalName}.zip">
                  <fileset dir="${project.basedir}/../" includes="README.md"/>
                  <fileset dir="${project.build.directory}/">
                    <include name="${project.build.finalName}.jar"/>
                    <include name="application.yml"/>
                    <include name="static/"/>
                    <include name="*.sh"/>
                    <include name="*.sql"/>
                    <include name="*.bat"/>
                    <include name="lib/"/>
                    <exclude name="lib/spring-boot-devtools*"/>
                  </fileset>
                </zip>
              </target>
            </configuration>
          </execution>

          <!-- 压缩zip包但不包含lib文件夹 -->
          <execution>
            <id>zip_no_lib</id>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <!-- http://ant.apache.org/manual/Tasks/ -->
              <target>
                <zip destfile="${project.build.directory}/${project.build.finalName}_no_lib.zip">
                  <fileset dir="${project.basedir}/../" includes="README.md"/>
                  <fileset dir="${project.build.directory}/">
                    <include name="${project.build.finalName}.jar"/>
                    <include name="application.yml"/>
                    <include name="static/"/>
                    <include name="*.sh"/>
                    <include name="*.sql"/>
                    <include name="*.bat"/>
                  </fileset>
                </zip>
              </target>
            </configuration>
          </execution>

        </executions>
      </plugin>

    </plugins>
  </build>

  <profiles>
    <!-- 拷贝依赖到 lib 目录 -->
    <profile>
      <id>lib</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <build>
        <plugins>
          <plugin>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
              <execution>
                <id>copy-dependencies</id>
                <phase>prepare-package</phase>
                <goals>
                  <goal>copy-dependencies</goal>
                </goals>
                <configuration>
                  <outputDirectory>${project.build.directory}/lib</outputDirectory>
                  <overWriteReleases>false</overWriteReleases>
                  <overWriteSnapshots>false</overWriteSnapshots>
                  <overWriteIfNewer>true</overWriteIfNewer>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>

0 人点赞