情景介绍
项目应用springcloud架构,多服务划分,每个服务为一个maven子模块。项目中除架构基础服务模块外,有一个通用模块提供公用内容,其他为业务模块。由于业务模块打包后包体较大,不利于备份及上传,故需要将依赖包外置,为避免通用模块代码随业务模块新增修改之后遗漏上传jar,需将通用模块打入jar内。
方案
方案一:将通用模块的jar解压缩到业务模块的编译目录,再打成jar。最后将依赖包复制到自定义目录
<build>
<finalName>${name}</finalName>
<!--设定资源位置-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<!--打包时排除配置文件-->
<!-- <excludes>
<exclude>**/*.properties</exclude>
<exclude>**/*.xml</exclude>
<exclude>**/*.yml</exclude>
</excludes>-->
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<!--编译-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!--将通用模块jar解压缩到业务模块编译目录-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>generate-resources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.liqun.crm.common</groupId>
<artifactId>common-module</artifactId>
<version>${common.module.version}</version>
<type>jar</type>
<overWrite>true</overWrite>
<outputDirectory>./target/classes</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<!--打包-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<outputDirectory>
${jar.directory}/${name}/
</outputDirectory>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib</classpathPrefix>
<!-- 程序启动入口 -->
<mainClass>com.liqun.grade.CrmGradeApplication</mainClass>
</manifest>
<manifestEntries>
<Class-Path>../</Class-Path>
</manifestEntries>
</archive>
<!-- <excludes>
<exclude>config/**</exclude>
</excludes>-->
</configuration>
</plugin>
<!--copy依赖包到自定义目录-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>
${jar.directory}/lib
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
方案二:使用内嵌ant脚本的方式由ant打包
<build>
<finalName>${name}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>
${project.build.outputDirectory}/lib
</outputDirectory>
<excludeArtifactIds>
common-module
</excludeArtifactIds>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<property name="main.class" value="com.liqun.system.base.SystemBaseServiceApplication"/>
<property name="jar.name" value="${project.name}.jar"/>
<property name="dist.home" value="${jar.directory}/${project.name}"/>
<property name="lib.home" value="${project.build.outputDirectory}/lib"/>
<property name="lib.dest" value="${jar.directory}/lib"/>
<property name="base.home" location="${basedir}"/>
<property name="common.home" location="${basedir}/../common-module"/>
<ant antfile="${basedir}/../jar.xml">
<target name="create_run_jar"/>
</ant>
</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
jar.xml内容如下
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project default="create_run_jar"
name="Create Runnable Jar ">
<target name="create_run_jar">
<delete file="${dist.home}/${jar.name}" />
<pathconvert property="libs.project" pathsep=" ">
<path>
<!-- lib.home contains all jar files, in several subdirectories -->
<fileset dir="${lib.home}">
<include name="**/*.jar"/>
<exclude name="**/*-sources.jar"/>
<exclude name="**/*-javadoc.jar"/>
</fileset>
</path>
<map from="${lib.home}/" to="../lib/" />
</pathconvert>
<jar destfile="${dist.home}/${jar.name}">
<manifest>
<attribute name="Main-Class" value="${main.class}"/>
<attribute name="Class-Path" value=". ${libs.project}"/>
</manifest>
<fileset dir="${base.home}/target/classes" excludes="lib/*"/>
<fileset dir="${common.home}/target/classes" excludes="lib/*"/>
</jar>
<copy failonerror="false" todir="${lib.dest}">
<fileset dir="${lib.home}">
<include name="**/*.jar" />
<exclude name="**/*-sources.jar" />
<exclude name="**/*-javadoc.jar" />
</fileset>
</copy>
</target>
</project>
注意:通用模块非可执行jar,故不需要添加build配置。父pom中亦无需设置