Maven编译打包
Maven
文章目录
- Maven
- 依赖范围(scope)
- plugin
- 【编译Java源代码】配置
- 【打包】配置
- 打包成一个可执行的 JAR(maven-shade-plugin)
- 将项目输出合并到单个可分发的包中(maven-assembly-plugin)
依赖范围(scope)
-
依赖范围:scope
<dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.4</version><!-- 依赖范围 --><scope>compile</scope> </dependency>
-
可选值:compile/test/provided/system/runtime/import
main目录 test目录 开发运行 打包运行 compile 有效 有效 有效 有效 test 无效 有效 有效 无效 provided 有效 有效 有效 无效 system runtime import
plugin
【编译Java源代码】配置
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><!-- 指定插件的版本 --><version>3.3</version><configuration><!-- 指定源代码的版本 --><source>1.8</source><!-- 指定生成的字节码版本 --><target>1.8</target><!-- 指定源文件的编码 --><encoding>UTF-8</encoding></configuration>
</plugin>
【打包】配置
打包成一个可执行的 JAR(maven-shade-plugin)
maven-shade-plugin 官网链接
将 Java 应用程序及其所有依赖项打包成一个可执行的 JAR 文件
<plugin><groupId>org.apache.maven.plugins</groupId><!-- 将 Java 应用程序及其所有依赖项打包成一个可执行的 JAR 文件 --><artifactId>maven-shade-plugin</artifactId><version>3.2.4</version><configuration><!-- 不创建依赖减少的 POM 文件 --><createDependencyReducedPom>false</createDependencyReducedPom><artifactSet><excludes><!-- 排除所有 org.apache.hadoop 的依赖 --><exclude>org.apache.hadoop:*</exclude></excludes></artifactSet></configuration><executions><execution><!-- 在打包阶段执行 --><phase>package</phase><goals><!-- 执行 shade 目标 --><goal>shade</goal></goals><configuration><transformers><transformerimplementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"><!--如果要打包的话,这里要换成对应的 main class--><mainClass>com.sensorsdata.analytics.DataPreProcess</mainClass></transformer></transformers><filters><filter><!-- 适用于所有工件的过滤器 --><artifact>*:*:*:*</artifact><!-- 排除所有 .SF .DSA .RSA 文件 --><excludes><exclude>META-INF/*.SF</exclude><exclude>META-INF/*.DSA</exclude><exclude>META-INF/*.RSA</exclude></excludes></filter></filters></configuration></execution></executions>
</plugin>
将项目输出合并到单个可分发的包中(maven-assembly-plugin)
maven-assembly-plugin 官网链接
将所有文件打包成一个单独的分发包
两个步骤
- 编写 assembly.xml 配置文件(将项目的工件与生成的文档一起打包到压缩包中)
- 使用 maven-assembly-plugin 打包插件并引用 assembly.xml 配置文件
pom.xml
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-assembly-plugin</artifactId><version>3.3.0</version><executions><execution><id>bin</id><phase>package</phase><goals><goal>single</goal></goals><configuration><!-- 引用 assembly.xml 配置文件 --><descriptors><descriptor>src/main/assembly/assembly.xml</descriptor></descriptors></configuration></execution></executions>
</plugin>
assembly.xml
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd"><id>bin</id><!-- 压缩格式 --><formats><format>tar.gz</format></formats><includeBaseDirectory>true</includeBaseDirectory><fileSets><!-- 将 ${project.parent.basedir}/data_export_engine-dist/src/main/bin 目录中的所有文件放到 bin 目录 --><fileSet><!-- 模块目录的绝对或相对位置 --><!-- ${project.parent.basedir} : 当前项目的父项目的基础目录 --><!-- ${project.basedir} : 当前项目的基础目录 --><directory>${project.parent.basedir}/data_export_engine-dist/src/main/bin</directory><!-- 在directory目录中选择需要打包的文件 --><includes><include>**</include></includes><!-- 相对于程序级根目录的输出目录 --><outputDirectory>bin</outputDirectory><fileMode>0755</fileMode></fileSet><!-- 将 ${project.parent.basedir}/data_export_engine-common/target 目录中的所有 jar包 放到 lib 目录 --><fileSet><directory>${project.parent.basedir}/data_export_engine-common/target</directory><includes><include>*.jar</include></includes><outputDirectory>lib</outputDirectory></fileSet></fileSets><!-- 将所有的依赖jar包放到 lib 目录(依赖范围:runtime) --><dependencySets><dependencySet><outputDirectory>lib</outputDirectory></dependencySet></dependencySets>
</assembly>