目录
一、maven-compiler-plugin
1.用来设置maven打包时使用的jdk版本,maven是个java框架,所以只针对jdk,scala的要另外设置。
2.usage
2.1.设置插件
<plugin>
<!-- 指定maven编译的jdk版本,如果不指定,maven3默认用jdk 1.5 maven2默认用jdk1.3 -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<!-- 一般而言,target与source是一致的,但有时为了让程序能在其他版本的jdk中运行(对于低版本目标jdk,源代码中不能使用低版本jdk中不支持的语法),会存在target不同于source的情况 -->
<source>1.8</source> <!-- 源代码使用的JDK版本 -->
<target>1.8</target> <!-- 需要生成的目标class文件的编译版本 -->
<encoding>UTF-8</encoding><!-- 字符集编码 -->
<skipTests>true</skipTests><!-- 跳过测试 -->
<verbose>true</verbose>
<showWarnings>true</showWarnings>
<fork>true</fork><!-- 要使compilerVersion标签生效,还需要将fork设为true,用于明确表示编译版本配置的可用 -->
<executable><!-- path-to-javac --></executable><!-- 使用指定的javac命令,例如:<executable>${JAVA_1_4_HOME}/bin/javac</executable> -->
<compilerVersion>1.3</compilerVersion><!-- 指定插件将使用的编译器的版本 -->
<meminitial>128m</meminitial><!-- 编译器使用的初始内存 -->
<maxmem>512m</maxmem><!-- 编译器使用的最大内存 -->
<compilerArgument>-verbose -bootclasspath ${java.home}\lib\rt.jar</compilerArgument><!-- 这个选项用来传递编译器自身不包含但是却支持的参数选项 -->
</configuration>
</plugin>
2.2.除了在build中设置plugin,还可以直接在中设置
<properties>
<!-- maven-compiler-plugin 将会使用指定的 JDK 版本对源代码进行编译(针对编译运行环境) -->
<maven.compiler.source>1.8</maven.compiler.source>
<!-- maven-compiler-plugin 将会使用指定的 JDK 版本将 java 文件编译为 class 文件(针对编译运行环境) -->
<maven.compiler.target>1.8</maven.compiler.target>
<properties>
2.3.SBoot项目可以通过java.version
<properties>
<java.version>1.8</java.version>
</properties>
2.4.全局设置,上面的3种设置只会对pom所在项目和子项目生效,如想全局设置。要改setting.xml
<profile>
<!-- 定义的编译器插件 ID,全局唯一 -->
<id>jdk-1.8</id>
<!-- 插件标记, activeByDefault 是否为默认编译器, jdk 提供编译器版本 -->
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<!-- 配置信息 source-源信息, target-字节码信息, compilerVersion-编译过程版本 -->
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
二、资源拷贝插件
1.Maven 在打包时默认只将 src/main/resources和src/main/java 里的配置文件打包到jar,像mybatis的mapper.xml这种需要打包到类路径下的配文就需要特殊处理,要么把XxxMapper.xml放到resources中,要么指定打包目录。虽然插件,但不用plugin用resources/resource
2.usage
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>
2.1.**表示任意包的任意层次下的包,include表示源,directory表示目的地
三、maven-enforcer-plugin
1.用来设置打包时的验证规则。指定要检查的KV并配置策略。比如规定jdk为1.8,如果不为1.8时,是忽略还是失败。
2.usage
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.4.1</version>
<executions>
<execution>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<!--1个要校验的项目对应1个标签,比如jdk版本就是requireJavaVersion标签-->
<requireJavaVersion>
<version>1.8</version>
</requireJavaVersion>
<requireMavenVersion>
<version>3.3</version>
</requireMavenVersion>
</rules>
</configuration>
</execution>
</executions>
</plugin>
2.1.检查的项目:http://maven.apache.org/enforcer/enforcer-rules/index.html
{1} 是否检查传递依赖
{2} jdk版本
{3} maven版本
{4} jar版本冲突检查
<dependencyConvergence>
<uniqueVersion>false</uniqueVersion>
</dependencyConvergence>
四、maven-source-plugin
1.默认会在target下自动生成source的jar
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
五、maven-javadoc-plugin
1.会在target下自动生成doc的jar。只能抽取生成java文件对应的文档,scala文件经测试无法抽取
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.0.1</version>
<configuration>
<javadocVersion>1.8</javadocVersion>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
六、maven-release-plugin
1.版本管理工具
七、maven-shade-plugin
- http://maven.apache.org/plugins/maven-shade-plugin/index.html
跟assembly都是打包插件,但shade功能要多:- Selecting Contents for Uber JAR
- Relocating Classes
- Attaching the Shaded Artifact
- Executable JAR
- Resource Transformers
1.通过版本的exclution无法解决jar冲突的问题, 打包的时候由shade自动修改代码中的依赖jar包名
relocation配置会强制修改代码中的依赖包名 ==>
项目中不同jar依赖同1个jar的不同版本,并且严格对应,不能使用exclude,类加载器默认会加载遇到的第1个依赖这样同1个jar多版本并存时,默认会依赖同1个版本。此时要用shade-plugin把优先级高的放到阴影中,实现各回各家的目的
spark-pmml中,集群本地的sparkmllib已经包含了pmml-mode包,且版本很低,又不能把集群上的pmml-mode给排除了,所以用shade把本地的pmml依赖打到shade阴影中,相当于给重命名,更精确的说是重定位relocation,把项目中对依赖的import重新换个位置。(class文件中的import语句会跟着修改为com.shade.xxx)。这样在集群上跑时,2个依赖互不影响。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>${project.artifactId}-shade-${project.version}</finalName>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<relocations>
<relocation>
<pattern>com.google</pattern>
<shadedPattern>com.shade.google</shadedPattern>
</relocation>
<relocation>
<pattern>org.jpmml</pattern>
<shadedPattern>org.shade.jpmml</shadedPattern>
</relocation>
<relocation>
<pattern>org.dmg</pattern>
<shadedPattern>org.shade.dmg</shadedPattern>
</relocation>
</relocations>
</configuration>
</execution>
</executions>
</plugin>
八、maven-scala-plugin
1.用来把scala文件编译成class文件并打到jar的类路径中
<plugin>
<!--scala原始在sbt(类似java maven)上做开发,现可以用这个插件来在maven中进行开发-->
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<version>2.15.2</version>
<executions>
<execution>
<id>scala-compile-first</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<includes>
<!--<include>用来设置源-->
<include>**/*.scala</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
九、maven-assembly-plugin
1.用来生成全量jar和只有class文件的jar
2.usage
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<appendAssemblyId>true</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>cn.dc.SparkWithPMMLAndCluster</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
十、maven-dependency-plugin
1.用来把项目的依赖复制到指定目录,${project.build.directory}表示jar的类路径。一般用于打全量包或者在线转离线环境。
<!-- 将项目依赖包复制到<outputDirectory>指定的目录下 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>
${project.build.directory}/lib
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
十一、maven-jar-plugin
1.用来把指定目录下的jar打到最终的jar中,一般用于离线环境下打包。
<!-- 将项目依赖包的路径(与上一项中的<outputDirectory>对应)添加到classPath中 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>cn.dc.SparkWithPMMLAndCluster</mainClass>
</manifest>
<manifestEntries>
<Class-Path>./</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>