maven打包总结

      maven最主要的作用有两个方面,一个是对jar包的依赖解决功能,自己管理jar包,另一个功能就是项目的构建,打包部署
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ielpm.manage</groupId>
<artifactId>manage</artifactId>
<version>1.0.0</version>
<url>http://maven.apache.org</url>
<packaging>war</packaging>
<name>manage</name>
<description>内部管理系统</description>


<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!--指定配置文件路径  这样在下面通过${profiles.dir}/product就可以访问到product下的配置文件了-->
                   <!--profiles.dir为自定义变量-->
<profiles.dir>src/main/profiles</profiles.dir>
</properties>
          <!--需从maven仓库中下载的jar-->
<dependencies>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.ielpm.common</groupId>
<artifactId>payutil</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10.2.0.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/struts/struts -->
<dependency>
<groupId>struts</groupId>
<artifactId>struts</artifactId>
<version>1.2.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/dom4j/dom4j -->
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>com.buybal.util</groupId>
<artifactId>buybalUtil</artifactId>
<version>1.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.ibatis/ibatis -->
<dependency>
<groupId>com.ibatis</groupId>
<artifactId>ibatis</artifactId>
<version>2.3.4.726</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.ibatis/ibatis2-dao -->
<dependency>
<groupId>com.ibatis</groupId>
<artifactId>ibatis-dao-2</artifactId>
<version>2.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.57</version>
</dependency>
<dependency>
<groupId>com.eetrust</groupId>
<artifactId>sid-plugin</artifactId>
<version>1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.zxing/core -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.zxing/core -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.rabbitmq/amqp-client -->
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>3.6.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/jexcelapi/jxl -->
<dependency>
<groupId>jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.2.1</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.jhlabs/filters -->
<dependency>
<groupId>com.jhlabs</groupId>
<artifactId>filters</artifactId>
<version>2.0.235</version>
</dependency>
<dependency>
<groupId>com.buybal.qrcode</groupId>
<artifactId>buybal-qrcode</artifactId>
<version>1.0.0</version>
</dependency>


<!-- https://mvnrepository.com/artifact/com.lowagie/itext -->
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.5</version>
</dependency>


<!-- https://mvnrepository.com/artifact/com.lowagie/itext-rtf -->
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext-rtf</artifactId>
<version>2.1.5</version>
</dependency>


<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.4</version>
</dependency>


<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.7</version>
</dependency>


<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.7</version>
</dependency>


<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-email -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-email</artifactId>
<version>1.4</version>
</dependency>




<!-- https://mvnrepository.com/artifact/org.jfree/jfreechart -->
<dependency>
<groupId>org.jfree</groupId>
<artifactId>jfreechart</artifactId>
<version>1.0.14</version>
</dependency>




<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.1.2</version>
</dependency>


<!-- https://mvnrepository.com/artifact/taglibs/standard -->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>


<!-- https://mvnrepository.com/artifact/net.sf.json-lib/json-lib -->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier><!--指定jdk版本 -->
</dependency>


</dependencies>


          <!-- Project Build-->
          <!--1,对主资源目录、测试资源目录过滤 (src/main/resources、src/test/resources)
                 <filtering>false</filtering>指定为false时,maven构建打包时不被过滤掉,为true是就被过滤掉了
           -->
<build>
<finalName>manage</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
                             <!--通过向Profiles中输入指定名称(product)来打包不同环境(开发,测试,生产)下的war包-->
<resource>
<directory>${profile.dir}</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.tld</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
                             <!--maven打包时去除不需要转码的资源文件-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<configuration>
<!-- 防止二进制文件被编译 -->
<nonFilteredFileExtensions>
<nonFilteredFileExtension>dat</nonFilteredFileExtension>
<nonFilteredFileExtension>swf</nonFilteredFileExtension>
<nonFilteredFileExtension>xml</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
                             <!--maven编译器插件的引入,用于对java文件进行编译 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<!--指定jdk版本 -->
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
                            <!--
                               对Web项目的资源目录(如css、js,引用的jar目录)进行过滤。这时需要对maven-war-plugin插件进行配置
                               <filtering>true</filtering> 
                            -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<!-- 
                                      <configuration> 
                                          <webResources>  
                                               <resource> 
                                                    <directory>${project.basedir}/src/main/webapp/WEB-INF/lib</directory> 
   <targetPath>WEB-INF/lib</targetPath> 
                                                    <filtering>true</filtering> 
                                                    <includes> 
         <include>**/*.jar</include> 
                                                    </includes>
                                               </resource> 
                                          </webResources> 
                                      </configuration> 
                                     -->
</plugin>
</plugins>
</build>
         <!--Maven Profile 
            每个Profile可以看作是POM的一部分配置,我们可以根据不同的环境应用不同的Profile,从而达到不同环境使用不同的POM配置的目的。
          -->
<profiles>
<!--测试环境配置文件 -->
<profile>
<id>test</id>
<properties>
<profile.dir>${profiles.dir}/test</profile.dir>
</properties>
</profile>
<!--开发环境配置文件 -->
<profile>
<id>dev</id>
        <activation>
<property>
<name>environment.type</name>
<value>dev</value>
</property>
</activation>
<properties>
<profile.dir>${profiles.dir}/dev</profile.dir>
</properties>
</profile>
<!--生产环境配置文件 -->
<profile>
<id>product</id>
<properties>
<profile.dir>${profiles.dir}/product</profile.dir>
</properties>
</profile>
</profiles>


</project>


maven内置属性:事实上有六种类型的Maven属性
1)自定义属性:在pom中<properties>元素下自定义的Maven属性。例如:
        <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!--指定配置文件路径  这样在下面通过${profiles.dir}/product就可以访问到product下的配置文件了-->
<profiles.dir>src/main/profiles</profiles.dir>
</properties>
          <properties>
         <profile.dir>${profiles.dir}/product</profile.dir>
          </properties>
      其中:profiles.dir为自定义属性,可以通过${profiles.dir}来引用src/main/profiles。
               profile.dir为自定义属性,可以通过${profile.dir}来引用src/main/profiles/product
  2)环境变量属性:所有环境变量都可以使用以env.开头的Maven属性引用。例如${env.JAVA_HOME}指代了JAVA_HOME环境变量的值。也可以        通过命令行mvn help:system查看所有环境变量。
  3)Java系统属性:所有Java系统属性都可以使用Maven属性引用,例如${user.home}指向了用户目录
  4)Settings属性:与POM属性同理。如${settings.localRepository}指向用户本地仓库的地址,用户 settings.xml 用户目录下的.m2/             settings.xml, 对当前用户的所有项目有效。
  5)内置属性:主要有两个常用内置属性——${basedir}表示项目根目录,即包含pom.xml文件的目录;${version}表示项目版本。
  6)POM属性:pom中对应元素的值。例如${project.artifactId}对应了<project><artifactId>元素的值,针对当前项目有效
       常用的POM属性包括:                              
       ${project.build.sourceDirectory}:项目的主源码目录,默认为src/main/java/.           
       ${project.build.testSourceDirectory}:项目的测试源码目录,默认为/src/test/java/.                                                                                          ${project.build.directory}:项目构建输出目录,默认为target/.                                                                                                                        ${project.build.outputDirectory}:项目主代码编译输出目录,默认为target/classes/.                                                                                        ${project.build.testOutputDirectory}:项目测试代码编译输出目录,默认为target/testclasses/.                                                                          ${project.groupId}:项目的groupId.                                                                                                                                                            ${project.artifactId}:项目的artifactId.                                                                                                                                                          ${project.version}:项目的version,于${version}等价                                                                                                                                        ${project.build.finalName}:项目打包输出文件的名称,默认为${project.artifactId}${project.version}.                                                                  
项目中开发配置文件。测试配置文件,生产配置文件配置如下:




指定配置文件进行打包,这样可以灵活的打包(开发war包,测试war包,生产war包)


       clean install:该命令调换用clean生命周期的clean阶段和default生命周期的instal阶段。
       product:指定配置生产环境信息的配置文件
       User settings中指定maven的setting.xml配置文件
       选中Skip Tests  打包的时候跳过测试(maven中的test中的代码就不编译打包了)


maven的生命周期和插件机制
   maven的核心仅仅定义了抽象的生命周期,具体的任务是交由插件完成的,插件以独立的形式存在。maven的生命周期与插件相互绑定,用以完成实际的构建任务。具体而言,是生命周期的阶段与插件的目标相互绑定,已完成某个具体的构建任务。例如项目编译这一任务,它对应了default生命周期的compile阶段,而maven-compiler-plugin这一插件的compile目标能够完成该任务,因此将他们绑定。从命令行执行maven任务的最主要方式就是调用maven的生命周期阶段。需要注意的是,各个生命周期是相互独立的,而一个生命周期的阶段是有前后依赖关系的。例子如下:
     1、$mvn clean :该命令调用clean生命周期的clean阶段。实际执行的阶段为clean生命周期的pre-clean和clean阶段。
     2、$mvn test:该命令调用default生命周期的test阶段。实际调用的是default生命周期的validate、initialize等,直到test的所有阶段。
     3、$mvn clean install:该命令调换用clean生命周期的clean阶段和default生命周期的instal阶段。
========================================maven生命周期===============================
       对于maven的生命周期来说,共有三个相互独立的生命周期,分别是clean、default、site。clean生命周期目的是清理项目,default生命周期目的是构建项目,而site生命周期目的是建立项目站点。
        每个生命周期分别包含一些阶段,这些阶段是有顺序的,并且后面的阶段依赖于前面的阶段。如clean生命周期包含pre-clean、clean和post-clean三个阶段,如果执行clean阶段,则会先执行pre-clean阶段。
        较之于生命周期阶段有前后依赖关系,三套生命周期本身是相互独立的,用户可以仅调用clean生命周期的某个阶段,也可以不执行clean周期,而直接执行default生命周期的某几个阶段。
        clean生命周期
        clean生命周期包含三个阶段,主要负责清理项目,如下:
 
pre-clean
executes processes needed prior to the actual project cleaning
clean
remove all files generated by the previous build
post-clean
executes processes needed to finalize the project cleaning
        default生命周期
        default生命周期定义了真正构建时所需要执行的所有步骤,包含的阶段如下:
 
validate
validate the project is correct and all necessary information is available.
initialize
initialize build state, e.g. set properties or create directories.
generate-sources
generate any source code for inclusion in compilation.
process-sources
process the source code, for example to filter any values.
generate-resources
generate resources for inclusion in the package.
process-resources
copy and process the resources into the destination directory, ready for packaging.
compile
compile the source code of the project.
process-classes
post-process the generated files from compilation, for example to do bytecode enhancement on Java classes.
generate-test-sources
generate any test source code for inclusion in compilation.
process-test-sources
process the test source code, for example to filter any values.
generate-test-resources
create resources for testing.
process-test-resources
copy and process the resources into the test destination directory.
test-compile
compile the test source code into the test destination directory
process-test-classes
post-process the generated files from test compilation, for example to do bytecode enhancement on Java classes. For Maven 2.0.5 and above.
test
run tests using a suitable unit testing framework. These tests should not require the code be packaged or deployed.
prepare-package
perform any operations necessary to prepare a package before the actual packaging. This often results in an unpacked, processed version of the package. (Maven 2.1 and above)
package
take the compiled code and package it in its distributable format, such as a JAR.
pre-integration-test
perform actions required before integration tests are executed. This may involve things such as setting up the required environment.
integration-test
process and deploy the package if necessary into an environment where integration tests can be run.
post-integration-test
perform actions required after integration tests have been executed. This may including cleaning up the environment.
verify
run any checks to verify the package is valid and meets quality criteria.
install
install the package into the local repository, for use as a dependency in other projects locally.
deploy
done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.
       site生命周期
     siet生命周期的目的是建立和发布项目站点,maven能够基于POM所包含的信息,自动生成一个友好的站点,方便团队交流和发布项目信息,包含的阶段如下:
    
pre-site
executes processes needed prior to the actual project site generation
site
generates the project's site documentation
post-site
executes processes needed to finalize the site generation, and to prepare for site deployment
site-deploy
deploys the generated site documentation to the specified web server


================================maven插件配置============================================
 除了内置绑定以为,用户可以配置插件目标的参数,进一步调整插件目标所执行的任务。
案:1:项目编译使用1.7版本的源文件,生成与JVM1.7兼容的字节码文件,如下:
           <plugin>  
                <groupId>org.apache.maven.plugins</groupId>  
                <artifactId>maven-compiler-plugin</artifactId>  
                <version>2.3.2</version>  
                <configuration>  
                    <source>1.6</source>  
                    <target>1.6</target>  
                </configuration>  
            </plugin>  
案例2:<!-- 自动复制资源文件件到根目录 -->  
            <plugin>  
                <groupId>org.apache.maven.plugins</groupId>  
                <artifactId>maven-resources-plugin</artifactId>  
                <version>2.6</version>  
                <configuration>  
                    <includeEmptyDirs>true</includeEmptyDirs>  
                             <encoding>GBK</encoding>  
                             <nonFilteredFileExtensions>  
                             <nonFilteredFileExtension>exe</nonFilteredFileExtension>  
                             <nonFilteredFileExtension>zip</nonFilteredFileExtension>  
                             <nonFilteredFileExtension>vbs</nonFilteredFileExtension>  
                             <nonFilteredFileExtension>sh</nonFilteredFileExtension>  
                             </nonFilteredFileExtensions>  
                </configuration>  
                <executions>  
                    <execution>  
                       <id>copy-resources</id>  
                        <phase>validate</phase>  
                        <goals>  
                          <goal>copy-resources</goal>  
                        </goals>  
                        <configuration>  
                                <includeEmptyDirs>true</includeEmptyDirs>  
                                <outputDirectory>${project.build.directory}</outputDirectory>  
                                <excludes>  
                                <exclude>agentmanager.jsmooth</exclude>  
                                <exclude>assembly.xml</exclude>  
                           </excludes>  
                           <resources>  
                                <resource>  
                                    <directory>src/main/resources/</directory>  
                                    <filtering>true</filtering>  
                                </resource>  
                            </resources>  
                        </configuration>  
                    </execution>  
               </executions>  
            </plugin>  
      如上图定义了一个id为copy-resources的任务,绑定到default生命周期的validate阶段,绑定的插件为maven-resources-plugin,插件目标为copy-resources。即用插件的copy-resources功能来实现项目资源文件的拷贝。


       案例3:在maven的web项目里面,除了上面所说的资源文件(src/main/resources)之外,还有一类叫做web资源目录,即src/main/webapp下面的js、css等等。默认情况下,这些目录是不被资源过滤的,意思就是:maven构建打包的时候,不对js,css等静态文件进行编译,开启的命令如下:
     <plugin>     
           <groupId>org.apache.maven.plugins</groupId>  
           <artifactId>maven-war-plugin</artifactId>  
           <version>2.1.1</version>  
           <configuration>  
           <webResources>  
                <resource>   
                <directory>src/main/webapp</directory>  
                <filtering>true</filtering>  
                <includes>  
                <include>**/*.css</include>  
                <include>**/*.js</include>  
                </includes>                            
               </resource>      
        </webResources>  
    </configuration>  
</plugin>  


      用maven管理库依赖,有个好处就是连同库的依赖的全部jar文件一起下载,免去手工添加的麻烦,但同时也带来了同一个jar会被下载了不同版本的问题(下载下来的jar中的某个依赖包和项目中以及存在的jar包版本冲突),好在pom的配置里面允许用<exclusion>来排除一些不需要同时下载的依赖jar ,比如:maven下载secret-api包时同时把它依赖的包guava包下载排除掉,防止下载下来的guava包和我们项目中已经存在的guava包版本冲突。
            案例:  <dependency>
<groupId>com.ielpm</groupId>
<artifactId>secret-api</artifactId>
<version>1.0.0</version>
<exclusions>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
</exclusions>
   </dependency>













  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值