Maven的高级功能(强大的插件管理)
写在前面一、Maven Archetype二、Maven plugin2.1、spring-boot-maven-plugin2.2、docker-maven-plugin2.3、maven-assembly-plugin2.4、maven-jetty-plugin2.5、maven-dependency-plugin2.6、maven-jar-plugin2.7、maven-checkstyle-plugin2.8、maven-resource-plugin2.9、maven-war-plugin2.10、maven-deploy-plugin2.11、maven-failsafe-plugin写在前面
这里我简单记下,突然觉得 Maven 的一些隐藏功能,所蕴含的强大之处,接下来,一起学习,共同进步。
一、Maven Archetype
这个东西是初始化Maven项目的时候,可以引入已提供的模板工具包,包括项目的目录结构,打包和起始依赖等,这个问题的发现,是我想用Springboot进行JSP开发的时候,直接复制或者简单Maven工程,要配置成支持JSP的项目,遇到过太多的坑,关于资源文件的管理,资源目录的区别等等,后来找到一种方式,配置成功,并把它制作成模板,下次New springboot + JSP 的 Maven 工程的时候,就可以直接,基于这个模板生成初始目录结构。
二、Maven plugin
可以找一下,maven 插件实在太多了,你接触过的有哪些,你都知道这些插件做了什么工作吗?
其实,对于基本的业务开发中,根本不需要任何的插件,也能保证项目的完成。
但是,插件存在的意义,就像开挂脚本一样,当我们遇到一些问题的时候,基于工具永远是最快的,插件就像是某种工具, 当试着发现的时候,就会发现他的强大。
关于插件,我目前只发现两个问题
插件的存在形式插件的使用2.1、spring-boot-maven-plugin
<build><plugins><plugin><groupId>.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>
2.2、docker-maven-plugin
<plugin><groupId>.spotify</groupId><artifactId>docker-maven-plugin</artifactId><version>0.2.3</version><configuration><baseImage>openjdk:8-jre-alpine</baseImage><imageName>${docker.image.prefix}/${project.artifactId}</imageName><exposes>8761</exposes><entryPoint>["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/${project.build.finalName}.jar"]</entryPoint><resources><resource><targetPath>/</targetPath><directory>${project.build.directory}</directory><include>${project.build.finalName}.jar</include></resource></resources></configuration></plugin>
2.3、maven-assembly-plugin
这个插件,可以自定义打包结构,当然打包方式很多,即使使用Maven或者其他编译工具,手动封包也是可以的,这里
我演示下,使用这个插件和一般的Maven打包的区别。
需要注意的是,需要手动配置一些参数和封包结构。也可以参照我下文中的示例中的配置打包。
示例,参照这里
<plugin><groupId>.apache.maven.plugins</groupId><artifactId>maven-assembly-plugin</artifactId><configuration><descriptors><descriptor>src/main/assembly/release.xml</descriptor></descriptors></configuration><executions><execution><id>make-assembly</id> <!-- this is used for inheritance merges --><phase>package</phase> <!-- bind to the packaging phase --><goals><goal>single</goal></goals></execution></executions></plugin>
2.4、maven-jetty-plugin
<build><plugins><plugin><groupId>.eclipse.jetty</groupId><artifactId>jetty-maven-plugin</artifactId><version>9.2.8.v20150217</version></plugin></plugins>
</build>
这个插件集成了比 Tomcat 更轻量级的 Web服务器Jetty,基于Springboot的开发中,基本不使用,但在War包的开始中可以考虑使用。
2.5、maven-dependency-plugin
<plugin><groupId>.apache.maven.plugins</groupId><artifactId>maven-dependency-plugin</artifactId><version>3.0.1</version><executions><execution><id>copy</id><phase>package</phase><goals><goal>copy</goal></goals><configuration><artifactItems><artifactItem><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><outputDirectory>${project.build.directory}/lib/lib1</outputDirectory></artifactItem><artifactItem><groupId>.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.7</version><outputDirectory>${project.build.directory}/lib/lib1</outputDirectory></artifactItem></artifactItems></configuration></execution></executions></plugin>
2.6、maven-jar-plugin
<plugin><groupId>.apache.maven.plugins</groupId><artifactId>maven-jar-plugin</artifactId><version>2.6</version><configuration><archive><manifest><addClasspath>true</addClasspath><classpathPrefix>lib/</classpathPrefix><mainClass>.lala.shop.App</mainClass></manifest></archive></configuration></plugin>
2.7、maven-checkstyle-plugin
<build><plugins><plugin> <groupId>io.spring.javaformat</groupId><artifactId>spring-javaformat-maven-plugin</artifactId></plugin><plugin><groupId>.apache.maven.plugins</groupId><artifactId>maven-checkstyle-plugin</artifactId></plugin></plugins><reporting><plugins><plugin><groupId>.apache.maven.plugins</groupId><artifactId>maven-checkstyle-plugin</artifactId></plugin></plugins></reporting>
</build>
2.8、maven-resource-plugin
<plugin><artifactId>maven-resources-plugin</artifactId><version>3.0.2</version><configuration><outputDirectory>output-resources</outputDirectory><resources><resource><directory>input-resources</directory><excludes><exclude>*.png</exclude></excludes><filtering>true</filtering></resource></resources>
</configuration>
</plugin>
2.9、maven-war-plugin
<build><plugins><plugin><groupId>.apache.maven.plugins</groupId><artifactId>maven-war-plugin</artifactId><version>3.2.3</version><configuration><webResources><resource><!-- this is relative to the pom.xml directory --><directory>resource2</directory></resource></webResources></configuration></plugin></plugins></build>
2.10、maven-deploy-plugin
<distributionManagement><snapshotRepository><id>nexus-snapshots</id><url>localhost:8081/nexus/content/repositories/snapshots</url></snapshotRepository>
</distributionManagement>
<plugin><artifactId>maven-deploy-plugin</artifactId><version>2.8.1</version><executions><execution><id>default-deploy</id><phase>deploy</phase><goals><goal>deploy</goal></goals></execution></executions>
</plugin>
2.11、maven-failsafe-plugin
<plugin><artifactId>maven-failsafe-plugin</artifactId><version>2.21.0</version><executions><execution><goals><goal>integration-test</goal><goal>verify</goal></goals><configuration>...</configuration></execution></executions>
</plugin>
更多推荐
插件,强大,高级,功能,Maven
发布评论