zoukankan      html  css  js  c++  java
  • framwork maven的配置及使用

    maven的配置及使用

    一.什么是maven:

    我们在开发项目的过程中,会使用一些开源框架、第三方的工具等等,这些都是以jar包的方式被项目所引用,并且有些jar包还会依赖其他的jar包,我们同样需要添加到项目中,所有这些相关的jar包都会作为项目的依赖。通常,一个Java EE项目所依赖的jar包会有很多。然而,这还并非是主要问题,在管理这些jar包过程中,jar包的版本往往是最令人头疼的问题。选择一个jar包的版本,需要考虑它所依赖的jar包是否支持这个版本,并且还需要确认依赖它的jar包能不能对这个版本兼容。所以,在过去的构建项目过程中,整理出这么一堆能让项目正常运行的jar包之后,这个lib目录就会变为禁区。jar包版本更新了,我们也很少会自找麻烦去触碰它。至于是不是存在冗余的jar包?能正常运行就好了嘛。。。。
    Maven的出现,解决了开发过程中的这一难题。它可以对项目依赖的jar包进行管理,可以让你的项目保持基本的依赖,排除冗余jar包,并且可以让你非常轻松的对依赖的jar包进行版本升级。而这些仅仅是Maven最基本的功能,它可以在这基础上对项目进行清理、编译、测试、打包、发布等等构建项目的工作。

    参考百度百科:

    http://baike.baidu.com/link?url=Kdew1Be1-rDr08gp2ulNCae2aTB1L1HGP5GLJ4vGR6s4POL3YvAtI4Wz870Wc_l79tkxZc5kcwXG9kPQyq3-__

    二.创建maven项目:

    1.创建Maven Project:

    2.勾选Create a simple project:

    3.maven setting:

    groupId: 相当于这个project的所有者或者机构的一个标识,一般是com.company.xxx这种格式

    artifactId:  这个project最后所生成的文档(jar、war)的名字,比如对于junit这个开源的project,它的artifactId就是junit

    packaging: 这个project的打包的类型,一般是war、jar等值

    version: project的版本

    name: project的名字,生成文档等内容的时候会用的这个名字

    4.创建后maven项目的目录结构:

     

    三.配置maven:

    创建完项目后自动会创建pom.xml文件

    <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/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.zlp</groupId>
      <artifactId>mavenTest</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>war</packaging>
      <name>mavenTest</name>
      <description>test</description>
      <build>
        <plugins>
          <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
              <source>1.6</source>
              <target>1.6</target>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </project>

    maven配置就是修改pom.xml文件,在<dependencies>放如下代码</dependencies>增加如下配置:

     <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-test</artifactId>

            <version>3.0.0.RELEASE</version>

            <type>jar</type>

            <scope>compile</scope>

    </dependency>    

    注意:如果不存在<dependencies>自己写个,位置在</project>上面就行!

    这只是一个,可以放多个。spring、spring mvc 、junit等等。会自动下载所需要的包及相关文件,在本机的这个位置:user下面的用户下面的.m2文件夹下的repository。不知道user在哪里,你也可以直接搜这个文件夹。下载有时会失败,那么只能用笨方法,从别人哪里拷贝,放到下载所在的文件夹中。

    配置完后如下:

    pom.xml

      1 <?xml version="1.0"?>
      2 <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"
      3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      4   <modelVersion>4.0.0</modelVersion>
      5   <groupId>com.zlp</groupId>
      6   <artifactId>mavenTest</artifactId>
      7   <version>0.0.1-SNAPSHOT</version>
      8   <packaging>war</packaging>
      9   <name>mavenTest</name>
     10   <dependencies>
     11     <dependency>
     12       <groupId>org.springframework</groupId>
     13       <artifactId>spring-test</artifactId>
     14       <version>3.0.0.RELEASE</version>
     15       <scope>compile</scope>
     16     </dependency>
     17   </dependencies>
     18   <repositories>
     19     <repository>
     20       <snapshots>
     21         <enabled>false</enabled>
     22       </snapshots>
     23       <id>central</id>
     24       <name>Maven Repository Switchboard</name>
     25       <url>http://repo1.maven.org/maven2</url>
     26     </repository>
     27   </repositories>
     28   <pluginRepositories>
     29     <pluginRepository>
     30       <releases>
     31         <updatePolicy>never</updatePolicy>
     32       </releases>
     33       <snapshots>
     34         <enabled>false</enabled>
     35       </snapshots>
     36       <id>central</id>
     37       <name>Maven Plugin Repository</name>
     38       <url>http://repo1.maven.org/maven2</url>
     39     </pluginRepository>
     40   </pluginRepositories>
     41   <build>
     42     <sourceDirectory>C:workmavenTestsrcmainjava</sourceDirectory>
     43     <scriptSourceDirectory>C:workmavenTestsrcmainscripts</scriptSourceDirectory>
     44     <testSourceDirectory>C:workmavenTestsrc	estjava</testSourceDirectory>
     45     <outputDirectory>C:workmavenTest	argetclasses</outputDirectory>
     46     <testOutputDirectory>C:workmavenTest	arget	est-classes</testOutputDirectory>
     47     <resources>
     48       <resource>
     49         <directory>C:workmavenTestsrcmain
    esources</directory>
     50       </resource>
     51     </resources>
     52     <testResources>
     53       <testResource>
     54         <directory>C:workmavenTestsrc	est
    esources</directory>
     55       </testResource>
     56     </testResources>
     57     <directory>C:workmavenTest	arget</directory>
     58     <finalName>mavenTest-0.0.1-SNAPSHOT</finalName>
     59     <pluginManagement>
     60       <plugins>
     61         <plugin>
     62           <artifactId>maven-antrun-plugin</artifactId>
     63           <version>1.3</version>
     64         </plugin>
     65         <plugin>
     66           <artifactId>maven-assembly-plugin</artifactId>
     67           <version>2.2-beta-5</version>
     68         </plugin>
     69         <plugin>
     70           <artifactId>maven-dependency-plugin</artifactId>
     71           <version>2.1</version>
     72         </plugin>
     73         <plugin>
     74           <artifactId>maven-release-plugin</artifactId>
     75           <version>2.0</version>
     76         </plugin>
     77       </plugins>
     78     </pluginManagement>
     79     <plugins>
     80       <plugin>
     81         <artifactId>maven-compiler-plugin</artifactId>
     82         <version>2.3.2</version>
     83         <executions>
     84           <execution>
     85             <id>default-testCompile</id>
     86             <phase>test-compile</phase>
     87             <goals>
     88               <goal>testCompile</goal>
     89             </goals>
     90             <configuration>
     91               <source>1.6</source>
     92               <target>1.6</target>
     93             </configuration>
     94           </execution>
     95           <execution>
     96             <id>default-compile</id>
     97             <phase>compile</phase>
     98             <goals>
     99               <goal>compile</goal>
    100             </goals>
    101             <configuration>
    102               <source>1.6</source>
    103               <target>1.6</target>
    104             </configuration>
    105           </execution>
    106         </executions>
    107         <configuration>
    108           <source>1.6</source>
    109           <target>1.6</target>
    110         </configuration>
    111       </plugin>
    112       <plugin>
    113         <artifactId>maven-clean-plugin</artifactId>
    114         <version>2.4.1</version>
    115         <executions>
    116           <execution>
    117             <id>default-clean</id>
    118             <phase>clean</phase>
    119             <goals>
    120               <goal>clean</goal>
    121             </goals>
    122           </execution>
    123         </executions>
    124       </plugin>
    125       <plugin>
    126         <artifactId>maven-install-plugin</artifactId>
    127         <version>2.3.1</version>
    128         <executions>
    129           <execution>
    130             <id>default-install</id>
    131             <phase>install</phase>
    132             <goals>
    133               <goal>install</goal>
    134             </goals>
    135           </execution>
    136         </executions>
    137       </plugin>
    138       <plugin>
    139         <artifactId>maven-resources-plugin</artifactId>
    140         <version>2.4.3</version>
    141         <executions>
    142           <execution>
    143             <id>default-resources</id>
    144             <phase>process-resources</phase>
    145             <goals>
    146               <goal>resources</goal>
    147             </goals>
    148           </execution>
    149           <execution>
    150             <id>default-testResources</id>
    151             <phase>process-test-resources</phase>
    152             <goals>
    153               <goal>testResources</goal>
    154             </goals>
    155           </execution>
    156         </executions>
    157       </plugin>
    158       <plugin>
    159         <artifactId>maven-surefire-plugin</artifactId>
    160         <version>2.7.1</version>
    161         <executions>
    162           <execution>
    163             <id>default-test</id>
    164             <phase>test</phase>
    165             <goals>
    166               <goal>test</goal>
    167             </goals>
    168           </execution>
    169         </executions>
    170       </plugin>
    171       <plugin>
    172         <artifactId>maven-war-plugin</artifactId>
    173         <version>2.1.1</version>
    174         <executions>
    175           <execution>
    176             <id>default-war</id>
    177             <phase>package</phase>
    178             <goals>
    179               <goal>war</goal>
    180             </goals>
    181           </execution>
    182         </executions>
    183       </plugin>
    184       <plugin>
    185         <artifactId>maven-deploy-plugin</artifactId>
    186         <version>2.5</version>
    187         <executions>
    188           <execution>
    189             <id>default-deploy</id>
    190             <phase>deploy</phase>
    191             <goals>
    192               <goal>deploy</goal>
    193             </goals>
    194           </execution>
    195         </executions>
    196       </plugin>
    197       <plugin>
    198         <artifactId>maven-site-plugin</artifactId>
    199         <version>2.0.1</version>
    200         <executions>
    201           <execution>
    202             <id>default-site</id>
    203             <phase>site</phase>
    204             <goals>
    205               <goal>site</goal>
    206             </goals>
    207             <configuration>
    208               <outputDirectory>C:workmavenTest	argetsite</outputDirectory>
    209               <reportPlugins>
    210                 <reportPlugin>
    211                   <groupId>org.apache.maven.plugins</groupId>
    212                   <artifactId>maven-project-info-reports-plugin</artifactId>
    213                 </reportPlugin>
    214               </reportPlugins>
    215             </configuration>
    216           </execution>
    217           <execution>
    218             <id>default-deploy</id>
    219             <phase>site-deploy</phase>
    220             <goals>
    221               <goal>deploy</goal>
    222             </goals>
    223             <configuration>
    224               <outputDirectory>C:workmavenTest	argetsite</outputDirectory>
    225               <reportPlugins>
    226                 <reportPlugin>
    227                   <groupId>org.apache.maven.plugins</groupId>
    228                   <artifactId>maven-project-info-reports-plugin</artifactId>
    229                 </reportPlugin>
    230               </reportPlugins>
    231             </configuration>
    232           </execution>
    233         </executions>
    234         <configuration>
    235           <outputDirectory>C:workmavenTest	argetsite</outputDirectory>
    236           <reportPlugins>
    237             <reportPlugin>
    238               <groupId>org.apache.maven.plugins</groupId>
    239               <artifactId>maven-project-info-reports-plugin</artifactId>
    240             </reportPlugin>
    241           </reportPlugins>
    242         </configuration>
    243       </plugin>
    244     </plugins>
    245   </build>
    246   <reporting>
    247     <outputDirectory>C:workmavenTest	argetsite</outputDirectory>
    248   </reporting>
    249 </project>

     四.maven创库及查找方式:

    1.仓库地址:http://mvnrepository.com

    2.查找方式:

    下面以servlet为例:

    打开创库地址:

    搜素需要的包:

    如下图,会搜素到多个版本,自己选择把,然后点击 servlet-api

    拷贝maven下的内容:

                                                                                                          成功找方法,失败找借口。

                                                                                                                             ---------心态

  • 相关阅读:
    spring execution表达式用法
    spring2.0中使用Aspect方式进行AOP如何得到method对象
    mybatis if标签判断字符串相等
    Date和Timestamp区别
    正向代理与反向代理[转载]
    oracle中的对象创建及删除语句【原创】
    使用IDEA打war包并导出war包详细步骤
    Java面试题
    电脑上怎么登录几个微信(微信多开)
    批处理快速合并多分Excel文件并将指定列的数据去重复
  • 原文地址:https://www.cnblogs.com/zlp520/p/4617567.html
Copyright © 2011-2022 走看看