常用的Maven插件
常用的Maven插件
由于最近在重构项目,发现项目中有众多Maven插件,而且本人对这些也不太熟悉,因此花些时间整理一下,方便以后查找与复习
maven-war-plugin
排除不想打进war包的jar 的配置
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1-alpha-1</version>
<configuration>
<!--
打包之前过滤掉不想要被打进 .war包的jar,注意:这个地方,本来路径应该是
WEB-INF/lib/anaalyzer-2.0.4.jar,但是经过多次试验,不能这样,
至于咋回事儿,搞不清楚。。经多方查证均无结果
暂且这样吧,虽然显得很丑陋,但是总能解决问题吧
-->
<warSourceExcludes>*/lib/analyzer-2.0.4.jar</warSourceExcludes>
<webResources>
<resource>
<!-- 元配置文件的目录,相对于pom.xml文件的路径 -->
<directory>src/main/webapp/WEB-INF</directory>
<!-- 是否过滤文件,也就是是否启动auto-config的功能 -->
<filtering>true</filtering>
<!-- 目标路径 -->
<targetPath>WEB-INF</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
另一种配置:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warName>${project.artifactId}</warName>
<warSourceExcludes>nodejs/**</warSourceExcludes>
<webResources>
<resource>
<!--src/main/profile的目录结构与打包后war的目录结构一致-->
<filtering>true</filtering>
<directory>src/main/profile</directory>
<!--targetPath即要替换的目录,include为要替换的文件-->
<targetPath>WEB-INF</targetPath>
<includes>
<include>**/web.xml</include>
</includes>
</resource>
<resource>
<filtering>true</filtering>
<directory>src/main/profile/pages</directory>
<targetPath>pages</targetPath>
<includes>
<include>**</include>
</includes>
</resource>
</webResources>
<!--上面的代码会将src/main/profile替换WEB-INF下的web.xml,已经pages下的文件-->
<warSourceDirectory>src/main/webapp</warSourceDirectory>
<webXml>src/main/webapp/WEB-INF/web.xml</webXml>
</configuration>
</plugin>
maven-compiler-plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>utf-8</encoding>
</configuration>
</plugin>
maven-surefire-plugin
打包时候跳过测试
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
<includes>
<include>**/*Test*.java</include>
</includes>
</configuration>
</plugin>
maven-source-plugin与maven-javadoc-plugin
<!-- 生成javadoc文档包的插件 -->
<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>
<!-- 生成sources源码包的插件 -->
<plugin>
<artifactId>maven-source-plugin</artifactId>
<version>2.4</version>
<configuration>
<attach>true</attach>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
复制资源到制定目录
<!-- 配置文件 -->
<resources>
<!--将配置文件加入到jar包的跟路径下[默认不会加入配置文件,需手动配置]-->
<resource>
<targetPath>${project.build.directory}/classes</targetPath>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
<resource>
<!--将Spring配置文件加入到META-INF/spring路径下,供Dubbo的Main使用-->
<targetPath>${project.build.directory}/classes/META-INF/spring</targetPath>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>spring-context.xml</include>
</includes>
</resource>
</resources>
maven-dependency-plugin
<!--将Maven以来的Jar包复制到一个lib文件夹下,供jar包启动时候实用-->
<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>
<type>jar</type>
<includeTypes>jar</includeTypes>
<outputDirectory>
${project.build.directory}/lib
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
maven-jar-plugin
<!-- 打包jar文件时,配置manifest文件,加入lib包的jar依赖 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<classesDirectory>target/classes/</classesDirectory>
<archive>
<manifest>
<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>
</configuration>
</plugin>
profile+filter配置多个环境
<properties>
<env>dev</env>
</properties>
<profiles>
<profile>
<id>dev</id>
<properties>
<env>dev</env>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<env>test</env>
</properties>
</profile>
<profile>
<id>production</id>
<properties>
<env>production</env>
</properties>
</profile>
</profiles>
<build>
<finalName>war_plugin</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<filters>
<filter>src/main/filter/${env}/db.properties</filter>
</filters>
</build>
filter根据不同的profile加载相应的properties文件,因此可以在不同的profile下加载不同的配置文件。
src/main/resource/下的db.properties
username=${username}
src/main/filter/dev/下的db.properties
username=root123
src/main/filter/production/下的db.properties
username=acy
默认的打包,用的是dev的配置,如果想用production的配置
package -P production
即可加载正式环境的配置