关于maven的几个常用插件

2016/07/23 2558点热度 0人点赞 0条评论

今天没事,研究了下maven的插件使用。配置如下。

<?xml version="1.0"?><project
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
	xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<modelVersion>4.0.0</modelVersion>
	<version>1.0</version>
	<groupId>com.yxkong</groupId>
	<artifactId>yblog</artifactId>
	<name>yblog</name>
	<packaging>war</packaging>

	<dependencies>       <!-- maven打包编译的异常 :sun.misc.BASE64Decoder 是 Sun 的专用 API,可能会在未来版本中删除的解决方法 
        <dependency>
            <groupId>org.codehaus.plexus</groupId>
            <artifactId>plexus-compiler-javac</artifactId>
            <version>1.8.1</version>
        </dependency>
-->        <!-- maven工程 仓库没有的jar使用方法 <dependency> <groupId>cryptix</groupId> <artifactId>cryptix-jce-api</artifactId> 
            <version>1.0</version> <scope>system</scope> <systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/cryptix-jce-api.jar</systemPath> 
            </dependency> -->        <!-- <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> 
            <version>2.0-rc1</version> </dependency> -->

	</dependencies>
	<build>
	     <!-- 默认的目标,必须跟命令行上的参数相同例如jar:jar,或者与时期(parse)相同,例如install -->
	    <defaultGoal>compile</defaultGoal>
	    <!-- 指定build target目标的目录,默认为${basedir}/target,即项目根目录下的target -->
	    <directory>${basedir}/target</directory>
	    <!-- 指定去掉后缀名后的工程名字,例如:默认为${artifactId}-${version} -->
	    <finalName>${artifactId}-${version}</finalName>
	    <!-- 将指定的属性文件引进来,并能在pom中${key}使用  <filters></filters> -->
	    <!--resources 描述工程中资源位置  -->        <!--打包进XML等文件,排除java及版本控制文件  -->
		<resources>
		    <resource>
		        <!-- targetPath:指定build资源到哪个目的目录,默认是base directory -->
		        <!-- <targetPath></targetPath> -->
		        <!-- 指定属性文件的目录,build的过程需要找到它,并且将其放到targetPath下。默认的directory是${basedir}/src/main/resources -->                <directory>src/main/java</directory>                <!-- 指定不包含在内的patterns,如果includes与excludes有冲突,那么excludes胜利,那些符合冲突样式的文件还是不会包含进来的 -->                <excludes>                     <exclude>**/*.java</exclude>                     <exclude>**/.svn/*</exclude>                </excludes>            </resource>
			<resource>
				<directory>src/main/config</directory>
				<!-- 指定包含文件的patterns,符合样式并且在directory目录下的文件将会是包含进project的资源文件 -->
				<includes>
					<include>**/*.*</include>
				</includes>
				<!-- 指定是否将filter文件(即上面说的filters里定义的*.property文件)的变量值在这个resource文件有效 -->
				<filtering>false</filtering>
			</resource>
			<resource>
				<directory>src/main/language</directory>
				<includes>
					<include>**/*.*</include>
				</includes>
				<filtering>false</filtering>
			</resource>
		</resources>
	
		<plugins>
		    <!-- 编译插件 -->
			<plugin>
			    <groupId>org.apache.maven.plugins</groupId>
			    <artifactId>maven-compiler-plugin</artifactId>
			    <version>3.5</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
					<encoding>UTF-8</encoding>  <!-- “编码 GBK 的不可映射字符”问题的解决 -->
				</configuration>
			</plugin>
			<!-- clean插件,默认清除当前工程的target目录 -->
			<plugin>
			    <groupId>org.apache.maven.plugins</groupId>
			    <artifactId>maven-clean-plugin</artifactId>
			    <version>3.0.0</version>
			    <configuration>
			        <filesets>
			            <!-- 配置额外的清除目录 -->
			            <fileset>
			                <directory>D:/workspace/jblog/target</directory>
			                <!-- 只配置includes,将删除该目录下指定的的 include配置文件-->
			                <includes>
			                    <include>test.log</include>
			                </includes>
			                <!-- 只配置excludes,将不删除指定的配置文件 -->
			                <excludes>
			                    <exclude>test.txt</exclude>
			                </excludes>
			            </fileset>
			        </filesets>
			    </configuration>
			</plugin>
			<!-- 单元测试插件 -->            <plugin>                <groupId>org.apache.maven.plugins</groupId>
			    <artifactId>maven-surefire-plugin</artifactId>
			    <version>2.19.1</version>                <configuration>                    <includes>                        <!--不写匹配默认规则,TestSuit结尾,test开头的方法  -->                        <include>**/*TestSuit.java</include>   <!-- 两个星号**用来匹配任意路径,一个星号*用来获取除路径风格符外的0个或多个字符 -->                    </includes>                    <excludes>                        <exclude>**/CollectionBillTestCase.java</exclude>                        <exclude>**/PaymentBillTestCase.java</exclude>                    </excludes>                    <skip>true</skip>  <!-- true略过单元测试,false将单元测试放入target中 -->                    <testFailureIgnore>true</testFailureIgnore> <!-- 当Maven 遇到一个测试失败,它默认的行为是停止当前的构建。 如果你希望继续构建项目,即使 Surefire 插件遇到了失败的单元测试,你就需要设置 Surefire 的testFailureIgnore 这个配置属性为 true -->                </configuration>            </plugin>            <!-- 把依赖的jar包拷到lib目录下 -->            <plugin>                <groupId>org.apache.maven.plugins</groupId>
			    <artifactId>maven-dependency-plugin</artifactId>
			    <version>2.10</version>                <executions>                    <execution>                        <id>copy-dependencies</id>                        <phase>package</phase>                        <goals>                            <goal>copy-dependencies</goal>                        </goals>                        <configuration>                            <outputDirectory>${project.build.directory}/lib</outputDirectory>                            <overWriteReleases>false</overWriteReleases>                            <overWriteSnapshots>false</overWriteSnapshots>                            <overWriteIfNewer>true</overWriteIfNewer>                        </configuration>                    </execution>                </executions>            </plugin>            <plugin>  
	            <!-- 发布插件 -->  
	            <groupId>org.apache.maven.plugins</groupId>  
	            <artifactId>maven-deploy-plugin</artifactId>  
	            <version>2.8.2</version>  
	        </plugin>  
            <!-- 把配置文件拷到和jar包同一个路径下 -->            <!-- 以utf-8编码拷贝配置文件,拷贝过程中是可以做变量替换的,也就是说你的配置文件可以是个模板,里面的${}所包含的内容是可以拷贝过程中替换的 -->            <!--   -->            <plugin>                <groupId>org.apache.maven.plugins</groupId>
			    <artifactId>maven-resources-plugin</artifactId>
			    <version>3.0.1</version>                <executions>                    <execution>                        <id>copy-resources</id>                        <phase>package</phase>                        <goals>                            <goal>copy-resources</goal>                        </goals>                        <configuration>                            <encoding>UTF-8</encoding>                            <outputDirectory>${basedir}/target/test-classes</outputDirectory>                            <resources>                                <resource>                                    <directory>${basedir}/src/main/config/</directory>                                    <filtering>true</filtering>                                </resource>                            </resources>                        </configuration>                    </execution>                </executions>            </plugin>           
            <!-- 打jar包时需要把配置文件给排除在外 -->            <plugin>                <groupId>org.apache.maven.plugins</groupId>
			    <artifactId>maven-jar-plugin</artifactId>
			    <version>3.0.2</version>                <configuration>                    <archive>                        <manifest>                            <addClasspath>true</addClasspath>  <!-- 在jar包中增加依赖jar路径说明 -->                            <classpathPrefix>lib/</classpathPrefix>                            <mainClass>com.yxkong.core.Main</mainClass><!-- 如果是可运行的jar,配置主入口 -->                        </manifest>                        <!-- 用maven在MANIFEST.MF资料中的Class-Path中增加当前目录(.)  -->
	                    <manifestEntries>
	                       <Class-Path>.</Class-Path>
	                    </manifestEntries>                    </archive>                    <classifier>lib</classifier>                </configuration>            </plugin>           
            <!-- 打war包的插件和jetty 6 一起用-->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
			    <artifactId>maven-war-plugin</artifactId>
			    <version>2.6</version>
				<configuration>
			        <warName>/${artifactId}</warName>
			        <includeEmptyDirectories>true</includeEmptyDirectories>
			        <webResources>
			            <resource>
			                <!-- 配置web资源目录默认找的是 src\main\webapp -->
			                <directory>/src/main/WebRoot</directory>
			                <!-- 和发布的contextPath保持一致 -->
			                <targetPath>/${artifactId}</targetPath>
			                <excludes>
			                    <exclude>**/.svn</exclude>
			                    <exclude>**/pom.xml</exclude>
			                </excludes>
			            </resource>
			        </webResources>
			        <!-- 缺失webXml继续 -->
			        <failOnMissingWebXml>false</failOnMissingWebXml>
			    </configuration>
			</plugin>            <!-- 源码插件 -->            <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>
			<plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat6-maven-plugin</artifactId>
				<version>2.2</version>
			</plugin>

			<plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<version>2.2</version>
			</plugin>
			<!--  jetty 9 jetty:run  可选jetty6或9-->
			<plugin>
		        <groupId>org.eclipse.jetty</groupId>
		        <artifactId>jetty-maven-plugin</artifactId>
		        <version>9.2.8.v20150217</version>
		        <configuration>
		           <!-- WEB上下文 -->
		           <webApp>
		             <!-- contextPath 的默认值的 /,${project.artifactId} 引用了 <artifactId> 节点的值,即项目的名称 -->
				     <contextPath>/${artifactId}</contextPath>
				     <!-- 项目的静态资源文件目录默认是 src/main/webapp,如果静态资源目录有多个,或者不在默认的 src/main/webapp 目录下,可增加路径 -->
				     <resourceBases>
				        <resourceBase>${basedir}/src/main/WebRoot</resourceBase>
				     </resourceBases>
				   </webApp>
		           <!-- 端口配置 -->
		           <httpConnector>
				      <port>80</port>
				   </httpConnector>
				   <!-- 自动热部署 -->
				   <scanIntervalSeconds>2</scanIntervalSeconds>
				   <!-- 手动重加载 reload 的可选值 :[automatic|manual]
				     默认值为 automatic,它与大于 0 的 scanIntervalSeconds 节点一起作用,实现自动热部署的工作。设为 manual 的好处是,当你改变文件
内容并保存时,不会马上触发自动扫描和重部署的动作,你还可以继续的修改,直至你在 Console 或命令行中敲回车键(Enter)的时候才触发重
新加载的动作。这样可以更加的方便调试修改。命令行的方式是:mvn -Djetty.reload=manual jetty:run -->
				   <reload>manual</reload>
		           <stopKey>shutdown</stopKey>                   <stopPort>9966</stopPort>                   <!-- 访问日志 -->                   <requestLog implementation="org.eclipse.jetty.server.NCSARequestLog">
					    <filename>target/access-yyyy_mm_dd.log</filename>
					    <filenameDateFormat>yyyy_MM_dd</filenameDateFormat>
					    <logDateFormat>yyyy-MM-dd HH:mm:ss</logDateFormat>
					    <logTimeZone>GMT+8:00</logTimeZone>
					    <append>true</append>
					    <logServer>true</logServer>
					    <retainDays>120</retainDays>
					    <logCookies>true</logCookies>
				   </requestLog>
		        </configuration>
		    </plugin>
		        <!-- jetty 配置时,需要先通过打war包的方式,要不然非 src\main\webapp将报Webapp source directory D:\workspace\yblog\src\main\webapp does not exist-->
			<plugin>
				<groupId>org.mortbay.jetty</groupId>
				<artifactId>maven-jetty-plugin</artifactId>
				<version>6.1.26</version>
				<configuration>
			        <!-- 配置扫描时间 -->
			        <scanIntervalSeconds>10</scanIntervalSeconds>
			        <!-- 配置项目在容器中的根路径 -->
			        <contextPath>/</contextPath>
			        <!-- 配置jetty容器中的jndi -->
			        <!--<jettyEnvXml>src/main/config/jetty.xml</jettyEnvXml>-->
			        <connectors>
			            <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
			                <!-- 端口 -->
			                <port>80</port>
			                <maxIdleTime>60000</maxIdleTime>
			            </connector>
			        </connectors>
			        <!-- 按照官网上说的是配置停止容器的快捷键和端口,至今不知怎么在idea中如何使用,有知道的麻烦告知下,3Q -->
			        <stopKey>foo</stopKey>
			        <stopPort>8888</stopPort>
			    </configuration>
			   
			    <dependencies>
			        <!-- 这个插件依赖的几个包 -->
			        <dependency>
			            <groupId>org.eclipse.jetty</groupId>
			            <artifactId>jetty-io</artifactId>
			            <version>7.6.6.v20120903</version>
			        </dependency>
			        <dependency>
			            <groupId>org.eclipse.jetty</groupId>
			            <artifactId>jetty-server</artifactId>
			            <version>7.6.6.v20120903</version>
			        </dependency>
			    </dependencies>
			</plugin>
		</plugins>
		<!-- plugin Management: 用于管理plugin,与pom build里的plugins区别是,这里的plugin是列出来,然后让子pom来决定是否引用的,例如后面的引用方法。 -->
		 <!-- POM文件报错maven-dependency-plugin (goals "copy-dependencies", "unpack") is not supported by m2e., 
        在</build>标签前添加如下内容  -->
		<pluginManagement>           
		  <plugins>                <plugin>                    <groupId>org.eclipse.m2e</groupId>                    <artifactId>lifecycle-mapping</artifactId>                    <version>1.0.0</version>                    <configuration>                        <lifecycleMappingMetadata>                            <pluginExecutions>                                <pluginExecution>                                    <pluginExecutionFilter>                                        <groupId>org.apache.maven.plugins</groupId>                                        <artifactId>maven-dependency-plugin</artifactId>                                        <versionRange>[2.0,)</versionRange>                                        <goals>                                            <goal>copy-dependencies</goal>                                            <goal>unpack</goal>                                        </goals>                                    </pluginExecutionFilter>                                    <action>                                        <ignore />                                    </action>                                </pluginExecution>                            </pluginExecutions>                        </lifecycleMappingMetadata>                    </configuration>                </plugin>            </plugins>        
		  </pluginManagement>
	</build>
	<!-- 配置远程仓库 -->
	<repositories>
	    <!-- 阿里云镜像 -->
	    <repository>  
	       <id>aliyun</id>  
	       <name>aliyun Central</name>  
	       <url>http://maven.aliyun.com/nexus/content/groups/public/</url>  
	       <releases>  
	          <enabled>true</enabled>  
	       </releases>  
	       <snapshots>  
	          <enabled>false</enabled>  
	       </snapshots>  
	    </repository>  
	    <!-- maven 中央仓库 -->
	    <repository>
	    	<id>maven</id>
		    <name>maven Central</name>
			<url>https://repo1.maven.org/maven2/</url>
			<releases>
			    <enabled>true</enabled>
			</releases>
			<snapshots>  
	            <enabled>false</enabled>  
	        </snapshots>  
	    </repository>
	    <repository>
	    	<id>uk</id>
		    <name>uk Central</name>
			<url>http://uk.maven.org/maven2</url>
			<releases>
			    <enabled>true</enabled>
			</releases>
			<snapshots>  
	            <enabled>false</enabled>  
	        </snapshots>  
	    </repository>
	</repositories>
	
	
	</project>

jetty.xml配置

            jdbc/core                                    jdbc:mysql://localhost:3306/message                root                123456

参考如下:http://my.oschina.net/sunhaojava/blog/177092

jetty9自动部署 http://www.blogjava.net/fancydeepin/archive/2012/06/23/maven-jetty-plugin.html

tomcat 自动部署配置:http://www.cnblogs.com/candle806/p/3785708.html

 

yxkong

这个人很懒,什么都没留下

文章评论