赞
踩
目录
声明:实验环境idea2022+SpringBoot 2.1.3。
这种方式适用于SpringBoot项目中仅有一个模块的场景。
需求:
1、启停可执行jar包的shell脚本单独打包到某个目录下,例如bin目录;
2、项目resources下的配置从jar包分离出来,单独打包到某个目录下,例如config目录;
3、项目的所有依赖jar包单独打到某个目录下,例如lib目录。
打包完成后,可直接执行bin目录下的启停shell脚本运行程序。
项目整体结构如图:
build结构如图:
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-jar-plugin</artifactId>
- <configuration>
- <archive>
- <manifest>
- <mainClass>马赛克DevopsBrainYmlAutomationApplication
- </mainClass>
- <addClasspath>true</addClasspath>
- <classpathPrefix>lib</classpathPrefix>
- </manifest>
- </archive>
- <!-- 排除resources下配置文件 -->
- <excludes>
- <exclude>*.*</exclude>
- </excludes>
- </configuration>
- </plugin>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-assembly-plugin</artifactId>
- <executions>
- <execution>
- <phase>package</phase>
- <goals>
- <goal>single</goal>
- </goals>
- </execution>
- </executions>
- <configuration>
- <descriptors>
- <descriptor>assembly/assembly.xml</descriptor>
- </descriptors>
- <outputDirectory>target</outputDirectory>
- </configuration>
- </plugin>
- </plugins>
- </build>
- <profiles>
- <profile>
- <id>dev</id>
- <properties>
- <package.environment>dev</package.environment>
- </properties>
- <!--默认选择-->
- <activation>
- <activeByDefault>true</activeByDefault>
- </activation>
- </profile>
- <profile>
- <id>test</id>
- <properties>
- <package.environment>test</package.environment>
- </properties>
- </profile>
- <profile>
- <id>prod</id>
- <properties>
- <package.environment>prod</package.environment>
- </properties>
- </profile>
- </profiles>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-jar-plugin</artifactId>
- <configuration>
- <archive>
- <manifest>
- <mainClass>马斯克拉ainYmlAutomationApplication
- </mainClass>
- <addClasspath>true</addClasspath>
- <classpathPrefix>lib</classpathPrefix>
- </manifest>
- </archive>
- </configuration>
- </plugin>
它配置了jar包启动的主类(程序入口),以及外部依赖的classpath目录。
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-assembly-plugin</artifactId>
- <executions>
- <execution>
- <phase>package</phase>
- <goals>
- <goal>single</goal>
- </goals>
- </execution>
- </executions>
- <configuration>
- <descriptors>
- <descriptor>assembly/assembly.xml</descriptor>
- </descriptors>
- <outputDirectory>target</outputDirectory>
- </configuration>
- </plugin>
它主要指定assembly.xml文件完成定制化配置,包括shell脚本、配置文件等。
指定打包环境,比如开发环境、测试环境、演示环境和生产环境。配置好后,idea maven打包中会出现配置的环境,打包的时候,选择一个环境即可。如图:
- <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>${package.environment}</id>
- <formats>
- <format>dir</format>
- <format>zip</format>
- </formats>
- <includeBaseDirectory>false</includeBaseDirectory>
- <dependencySets>
- <!-- 依赖jar包 -->
- <dependencySet>
- <outputDirectory>/lib</outputDirectory>
- <excludes>
- <exclude>${project.groupId}:${project.artifactId}</exclude>
- </excludes>
- </dependencySet>
- <!-- jar包 -->
- <dependencySet>
- <outputDirectory>/</outputDirectory>
- <includes>
- <include>${project.groupId}:${project.artifactId}</include>
- </includes>
- </dependencySet>
- </dependencySets>
-
- <fileSets>
- <!-- 配置文件打包 -->
- <fileSet>
- <directory>src/main/resources</directory>
- <filtered>true</filtered>
- <includes>
- <include>application.yml</include>
- <include>application-${package.environment}.yml</include>
- </includes>
- <outputDirectory>/config</outputDirectory>
- </fileSet>
- <!-- shell脚本打包 -->
- <fileSet>
- <directory>bin/${package.environment}</directory>
- <filtered>true</filtered>
- <includes>
- <include>*.*</include>
- </includes>
- <outputDirectory>/bin</outputDirectory>
- <fileMode>0755</fileMode>
- </fileSet>
- </fileSets>
- </assembly>
(1)id:
assembly的唯一性标识,它会追加到打包后的目录或者zip压缩文件尾部,例如:
(2)format:
打包后的格式,dir表示打包后是一个目录,zip表示打包后是一个zip压缩文件。如图:
includeBaseDirectory:
是否包含基目录,如果配置为true,会在最后打包的目录下生成一个基目录,如图:
dependencySet
jar依赖配置。assembly.xml中配置了两个dependencySet,第一个dependencySet将所有依赖打包到lib下,除了本项目的jar包。第二个denpendencySet是将项目jar包打包到根目录/下,这里根目录是相对于打包后的目录而言的,如图:
fileSets:
文件复制打包,例如shell脚本文件,配置文件等。directory指定输入目录(需要复制的文件所在目录),outputDirectory指定输出目录(复制后的文件所在目录)。如图:
配置完成后,现在我们开始打包看下效果。首先选择环境,例如选择开发环境(dev),如图:
打包结果:
需求:
除了单模块场景的需求外,新增一个多模块的需求:
(1)将每个模块项目自身的jar包打成一个jar包。例如项目结构如图:
api模块依赖其他子模块(bean、dao、service),当然外部依赖还是打包到lib目录下。
这里需要在api的pom.xml中配置上面单模块的相关配置,还需要新增一个plugin配置:
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- <version>2.1.3.RELEASE</version>
- <configuration>
- <mainClass>马赛克.DevopsBrainManagerApplication</mainClass>
- <includes>
- <include>
- <groupId>马赛克</groupId>
- <artifactId>马赛克-service</artifactId>
- </include>
- <include>
- <groupId>马赛克</groupId>
- <artifactId>马赛克-dao</artifactId>
- </include>
- <include>
- <groupId>马赛克</groupId>
- <artifactId>马赛克-bean</artifactId>
- </include>
- </includes>
- </configuration>
- <executions>
- <execution>
- <goals>
- <goal>repackage</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
其中includes部分就是将bean、dao、service模块项目自身的jar包打到api模块项目jar中。打包后的效果如图:
下面给出pom.xml和assembly.xml整体配置:
assembly.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>${package.environment}</id>
- <formats>
- <format>dir</format>
- <format>zip</format>
- </formats>
- <includeBaseDirectory>false</includeBaseDirectory>
- <dependencySets>
- <!-- 依赖jar包 -->
- <dependencySet>
- <outputDirectory>/lib</outputDirectory>
- <excludes>
- <exclude>${project.groupId}:*</exclude>
- </excludes>
- </dependencySet>
- <!-- jar包 -->
- <dependencySet>
- <outputDirectory>/</outputDirectory>
- <includes>
- <include>${project.groupId}:${project.artifactId}</include>
- </includes>
- </dependencySet>
- </dependencySets>
-
- <fileSets>
- <!-- 配置文件打包 -->
- <fileSet>
- <directory>src/main/resources</directory>
- <filtered>true</filtered>
- <includes>
- <include>application.yml</include>
- <include>logback-spring.xml</include>
- <include>whiteList.properties</include>
- <include>application-${package.environment}.yml</include>
- </includes>
- <outputDirectory>/config</outputDirectory>
- </fileSet>
- <!-- shell脚本打包 -->
- <fileSet>
- <directory>bin/${package.environment}</directory>
- <filtered>true</filtered>
- <includes>
- <include>*.*</include>
- </includes>
- <outputDirectory>/bin</outputDirectory>
- <fileMode>0755</fileMode>
- </fileSet>
- </fileSets>
- </assembly>
-
pom.xml
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-jar-plugin</artifactId>
- <configuration>
- <archive>
- <manifest>
- <addClasspath>true</addClasspath>
- <classpathPrefix>lib</classpathPrefix>
- </manifest>
- </archive>
- <!-- 排除resources下配置文件 -->
- <excludes>
- <exclude>*.*</exclude>
- </excludes>
- </configuration>
- </plugin>
-
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- <version>2.1.3.RELEASE</version>
- <configuration>
- <mainClass>马赛克nManagerApplication</mainClass>
- <includes>
- <include>
- <groupId>马赛克</groupId>
- <artifactId>马赛克-service</artifactId>
- </include>
- <include>
- <groupId>马赛克</groupId>
- <artifactId>马赛克-dao</artifactId>
- </include>
- <include>
- <groupId>马赛克</groupId>
- <artifactId>马赛克-bean</artifactId>
- </include>
- </includes>
- </configuration>
- <executions>
- <execution>
- <goals>
- <goal>repackage</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
-
- <!-- 使用assembly.xml定制化打包 -->
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-assembly-plugin</artifactId>
- <executions>
- <execution>
- <id>make-assembly</id>
- <phase>package</phase>
- <goals>
- <goal>single</goal>
- </goals>
- </execution>
- </executions>
- <configuration>
- <descriptors>
- <descriptor>assembly/assembly.xml</descriptor>
- </descriptors>
- <outputDirectory>target</outputDirectory>
- </configuration>
- </plugin>
- </plugins>
- </build>
-
- <!-- 各种环境配置 默认开发环境 -->
- <profiles>
- <!-- 开发环境 -->
- <profile>
- <id>dev</id>
- <properties>
- <package.environment>dev</package.environment>
- </properties>
- <!--默认选择-->
- <activation>
- <activeByDefault>true</activeByDefault>
- </activation>
- </profile>
- <!-- 测试环境 -->
- <profile>
- <id>test</id>
- <properties>
- <package.environment>test</package.environment>
- </properties>
- </profile>
- <!-- 演示环境 -->
- <profile>
- <id>demo</id>
- <properties>
- <package.environment>demo</package.environment>
- </properties>
- </profile>
- <!-- 生产环境 -->
- <profile>
- <id>prod</id>
- <properties>
- <package.environment>prod</package.environment>
- </properties>
- </profile>
- </profiles>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。