SpringBoot中使用Ant+Maven打包

7 篇文章 0 订阅
5 篇文章 0 订阅

基于JEES的项目结构,其实和SpringBoot是一致,最近几年很忙,所以没怎么维护,有点生疏了。正好前几天给项目加上了打包的内容,现在整理了下,分享出来。

大致内容分为了4个配置文件:build.xml,build.bat,pom-zip.xml,package.xml,我依次说明下文件的作用和配置内容。

1.build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="package_mod" default="package">
    <echo>==================== 模块打包 ====================</echo>
    <target name="init">
        <echo>拷贝pom文件至WebRoot模块...</echo>
        <copy todir="../../web-root/" overwrite="true">
            <fileset file="pom-zip.xml"/>
            <fileset file="package.xml"/>
        </copy>
        <echo>拷贝完成!</echo>
    </target>
    <target name="mvn-package">
        <echo>执行MVN命令: clean package -f pom-zip.xml</echo>
        <exec executable="build.bat" />
        <echo>命令执行成功!</echo>
    </target>
    <target name="publish">
        <echo>发布版本到release目录</echo>
        <copy todir="../release/" overwrite="true">
            <fileset dir="../../web-root/target/" includes="*.zip"/>
        </copy>
        <echo>发布完成!</echo>
    </target>
    <target name="destroy">
        <echo>移除临时pom文件...</echo>
        <delete file="../../web-root/pom-zip.xml"></delete>
        <delete file="../../web-root/package.xml"></delete>
        <echo>移除完成!</echo>
    </target>
    <target name="finish">
        <echo>==================== 模块打包结束 ====================</echo>
    </target>
    <target name="package" depends="init,mvn-package,publish,destroy,finish">
    </target>
</project>

这里的文件和项目结构参考JEES的内容。步骤见package的depends。我认为比较清楚,做的事情也比较少。web-root模块相当于jees-webs模块。

2.build.bat:

@echo off
CHCP 65001
cd ..\..\web-root\
echo 执行mvn打包命令,请确保maven已加入环境配置中..
mvn clean package -f pom-zip.xml
echo 执行完毕.

利用控制台输出mvn命令,并指定了pom-zip.xml文件。

3.pom-zip.xml

<?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">

    <parent>
        <groupId>com.jees.modex</groupId>
        <artifactId>modex</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.jees.modex</groupId>
    <artifactId>web-root</artifactId>
    <version>1.0.0-SNAPSHOT</version>

    <properties>
        <package.starter.version>1.0.0</package.starter.version>
        <package.starter.name>MBI</package.starter.name>
    </properties>

    <build>
        <finalName>${package.starter.name}-${package.starter.version}</finalName>
        <plugins>
            <!-- 打包jar文件时,配置manifest文件,加入lib包的jar依赖 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.2.0</version>
                <executions>
                    <execution>
                        <id>default-jar</id>
                        <phase>package</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <archive>
                        <addMavenDescriptor>false</addMavenDescriptor>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>com.jees.datascale.server.Application</mainClass>
                            <useUniqueVersions>false</useUniqueVersions>
                        </manifest>
                        <!-- (配置文件外置目录) -->
                        <manifestEntries>
                            <Class-Path>./ lib/</Class-Path>
                        </manifestEntries>
                    </archive>
                    <!--一下分别是排除源文件目录,资源文件, 资源文件目录 -->
                    <excludes>
                        <exclude>/config/</exclude>
                        <exclude>/templates/</exclude>
                    </excludes>
                    <includes>
                        <include>/com/</include>
                    </includes>
                </configuration>
            </plugin>
            <!-- 自定义发布版本包 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.3.0</version>
                <executions>
                    <execution>
                        <id>create-release-zip</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <appendAssemblyId>false</appendAssemblyId>
                    <descriptors>
                        <descriptor>package.xml</descriptor>
                    </descriptors>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <!-- Spring Boot -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>${spring.boot.version}</version>
            <type>pom</type>
            <scope>provided</scope>
        </dependency>
        <!-- Lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
            <scope>provided</scope>
        </dependency>
        <!-- Jees Webs -->
        <dependency>
            <groupId>com.github.aiyoyoyo</groupId>
            <artifactId>jees-webs</artifactId>
            <version>1.5.0-SNAPSHOT</version>
        </dependency>
        <!-- 项目需要的其他模块内容,放到了mod-example中,web-root模块仅引入基础模块,方便切换不同项目需求 -->
        <dependency>
            <groupId>com.jees.modex</groupId>
            <artifactId>mod-example</artifactId>
            <version>1.0.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

4.package.xml

<?xml version="1.0" encoding="UTF-8"?>
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>release package</id>
    <formats>
        <format>zip</format>
    </formats>
    <!-- 打zip包时,包含一层打包目录 -->
    <includeBaseDirectory>true</includeBaseDirectory>
    <!-- 包含程序运行自身所需的目录 -->
    <fileSets>
        <!-- web-root模块 资源文件 -->
        <fileSet>
            <directory>src/main/resources/</directory>
            <outputDirectory>/</outputDirectory>
        </fileSet>
        <!-- 其他模块 资源文件 -->
        <fileSet>
            <directory>../mod-example/src/main/resources/</directory>
            <outputDirectory>/</outputDirectory>
        </fileSet>
    </fileSets>
    <!-- 把编译好的jar文件包含到发布的目录中去并设置脚本文件的权限-->
    <files>
        <file>
            <source>${project.build.directory}/${project.build.finalName}.${project.packaging}</source>
            <destName>${package.starter.name}-${package.starter.version}.jar</destName>
            <outputDirectory>/</outputDirectory>
        </file>
    </files>
    <!-- 复制所有的依赖jar到发布的目录的lib目录下 -->
    <dependencySets>
        <dependencySet>
            <scope>compile</scope>
            <outputDirectory>lib</outputDirectory>
            <useProjectArtifact>false</useProjectArtifact>
            <!--忽略某些包-->
            <excludes>
            </excludes>
        </dependencySet>
    </dependencySets>
</assembly>

最终打包效果如图:

 文件结构我就不做过多说明了,感兴趣的朋友可以参观下jeesupport的github地址:

GitHub - aiyoyoyo/jeesupport: 基于SpringFramework衍生的配套开发支持。基于SpringFramework衍生的配套开发支持。. Contribute to aiyoyoyo/jeesupport development by creating an account on GitHub.https://github.com/aiyoyoyo/jeesupport

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值