maven插件内置信息
maven自带插件
- mvn clean 清理
- mvn compiler 编译
- mvn deploy 发布
- mvn install 构建
- mvn verifier 校验
- mvn test 测试
内置变量
- ${basedir} 表示项目根目录,即包含pom.xml文件的目录;
- ${version} 表示项目版本;
- ${project.basedir} 同{basedir};
- ${project.baseUri} 表示项目文件地址;
- ${maven.build.timestamp} 表示项目构件开始时间;
- ${maven.build.timestamp.format} 表示格式化时间 默认值为yyyyMMdd-HHmm
- ${project.build.directory} 表示主源码路径;
- ${project.build.sourceEncoding} 表示主源码的编码格式;
- ${project.build.sourceDirectory} 表示主源码路径;
- ${project.build.finalName} 表示输出文件名称;
- ${project.version} 表示项目版本,同{version};
- ${project.xxx} 当前pom文件的任意节点的内容
- ${env.xxx} 获取系统环境变量。
- ${settings.xxx} 指代了settings.xml中对应元素的值。
配置插件
Springboot插件
<!-- 这个插件还是挺有意思的,默认就会把依赖包引进来 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>cui.yao.nan.erureka.EurekaServerApplication</mainClass>
</configuration>
</plugin>
dokcer插件
-
帮助我们在Maven工程中,通过简单的配置,自动生成镜像并推送到仓库中
-
使用命令行
- mvn clean package docker:build 只执行 build 操作
- mvn clean package docker:build -DpushImage 执行 build 完成后 push 镜像
- mvn clean package docker:build -DpushImageTag 执行 build 并 push 指定 tag 的镜像
- mvn package -DskipDockerBuild 跳过构建
- mvn package -DskipDockerTag 跳过tag镜像
- mvn package -DskipDockerPush 跳过push
- mvn package -DskipDocker 跳过整个过程
-
使用POM的方式
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<imageName>mavendemo</imageName>
<baseImage>java</baseImage>
<maintainer>docker_maven docker_maven@email.com</maintainer>
<workdir>/ROOT</workdir>
<cmd>["java", "-version"]</cmd>
<entryPoint>["java", "-jar", "${project.build.finalName}.jar"]</entryPoint>
<!-- 这里是复制 jar 包到 docker 容器指定目录配置 -->
<resources>
<resource>
<targetPath>/ROOT</targetPath>
<!-- 这里project.build.directory 默认的是target文件夹 -->
<directory>${project.build.directory}</directory>
<!-- 这里就是项目名称~~finalname设置的 -->
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
- 使用dockerfile方式
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<imageName>mavendemo</imageName>
<dockerDirectory>${basedir}/docker</dockerDirectory> <!-- 指定 Dockerfile 路径-->
<!-- 这里是复制 jar 包到 docker 容器指定目录配置,也可以写到 Docokerfile 中 -->
<resources>
<resource>
<targetPath>/ROOT</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
- ${basedir}/docker/Dockerfile 配置
FROM java
MAINTAINER docker_maven docker_maven@email.com
WORKDIR /ROOT
CMD ["java", "-version"]
ENTRYPOINT ["java", "-jar", "${project.build.finalName}.jar"]
compiler-编译插件
- 使用命令: mvn -compile
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source> <!-- 源代码使用的JDK版本 -->
<target>1.8</target> <!-- 需要生成的目标class文件的编译版本 -->
<encoding>UTF-8</encoding><!-- 字符集编码 -->
<skipTests>true</skipTests><!-- 跳过测试 -->
<meminitial>128m</meminitial>
<maxmem>512m</maxmem>
</configuration>
</plugin>
jar-打包插件
- 使用命令 mvn -package
<!-- 打包jar文件时,配置manifest文件,加入lib包的jar依赖 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<classesDirectory>target/classes/</classesDirectory>
<archive>
<manifest>
<!-- 设定入口Main方法全限定名称 -->
<mainClass>com.alibaba.dubbo.container.Main</mainClass>
<!-- 打包时 MANIFEST.MF文件不记录的时间戳版本 -->
<!--自动加载META-INF/spring目录下的所有Spring配置-->
<useUniqueVersions>false</useUniqueVersions>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
<!--可以排除一些本地配置文件-->
<excludes>
<exclude>**/profiles/**</exclude>
<exclude>**/jdbc.properties</exclude>
<exclude>**/*.proto</exclude>
</excludes>
</configuration>
</plugin>
source-打包源码插件
- 使用命令 mvn -package
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<!--最好先指定过如下属性—>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
...
</properties>
shade-自动化打包jar文件
<!-- 打包可执行jar文件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>org.chench.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
dependency处理依赖问题
- 该功能已经作为idea的插件处理了,一般没什么用
- 被依赖模块jar文件中class文件提取出来放在指定位置
<!-- 将依赖模块的jar包文件提取出来放到指定位置 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>prepare-package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.xxx</groupId>
<artifactId>xxx-xxx</artifactId>
<version>1.0.0</version>
<type>jar</type>
<includes>**/*.class</includes>
<overWrite>false</overWrite>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
- 将scope为system的依赖jar包一起打包
<!-- 打包scope为system的jar包 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
</execution>
</executions>
<configuration>
<includeScope>system</includeScope>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
</configuration>
</plugin>
- 将scope为system的依赖jar包中的class文件解压出来重新打包
<!-- 将scope为system的依赖jar包中的class文件解压出来重新打包 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<id>unpack</id>
<phase>prepare-package</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
</execution>
</executions>
<configuration>
<includeScope>system</includeScope>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
</configuration>
</plugin>
resource-默认资源文件处理
- 指定打包和排除配置资源文件处理
<resources>
<resource>
<!-- 指定资源目录 -->
<directory>src/main/resources</directory>
<!-- 不打包指定类型的资源 -->
<excludes>
<exclude>**/*.svn</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources/profiles/${profile.dir}</directory>
<includes>
<include>*.properties</include>
</includes>
</resource>
</resources>
javadoc-生成javaDoc文件
- 为项目生成javadoc文件
- 执行命令
- mvn javadoc:javadoc
- mvn javadoc:jar
- mvn javadoc:aggregate
- mvn javadoc:aggregate-jar
- mvn javadoc:test-javadoc
- mvn javadoc:test-jar
- mvn javadoc:test-aggregate
- mvn javadoc:test-aggregate-jar
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.2</version>
<configuration>
<aggregate>true</aggregate>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
maven常用插件名称整理
<artifactId>maven-pmd-plugin</artifactId>
<artifactId>maven-checkstyle-plugin</artifactId>