Maven快速上手

Maven快速上手

博文目录

  1. Maven使用前提
  2. Maven工程的结构
  3. Maven的配置文件pom.xml
  4. Maven的单元测试
  5. 如何修改本地仓库
  6. 查找依赖的顺序
  7. 快速创建Maven工程
  8. 使用M2Eclipse及Jetty插件
  9. 其他配置

 Maven快速上手


Maven是Apache提供的项目自动化构建和项目管理软件,基于项目对象模型(POM),Maven利用一个中央信息片断管理一个项目的构建、报告和文档等步骤。

官方网站:http://maven.apache.org/

使用前提:

1,下载Maven

http://maven.apache.org/download.cgi

2,配置环境变量

M2_HOME=maven根路径:C:\server\apache-maven-3.1.0
PATH=maven的bin路径:C:\server\apache-maven-3.1.0\bin

3,执行maven命令

>mvn -v    显示maven的版本信息

maven工程的结构:
在C盘建立my-app文件夹,里面的结构如下:
my-app
|--pom.xml
|--src
   |--main
   |  |--java
   |     |--com
   |        |--mycompany
   |           |--app
   |              |--App.java
   |--test
      |--java
         |--com
            |--mycompany
               |--app
                  |--AppTest.java

pom.xml是maven的项目的配置文件
<?xml version="1.0" encoding="UTF-8"?>    
<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.mycompany.app</groupId>    
  <artifactId>my-app</artifactId>    
  <version>1.0-SNAPSHOT</version>    
    
</project> 
 

groupId:公司所做项目名称
artifactId:模块的名称
version:版本号

groupId+artifactId+version=坐标
坐标唯一标识了我们的项目的路径:
com/mycompany/app/my-app/**

App.java内容如下:
package com.mycompany.app;

public class App{
    public static void main(String[] args){
        System.out.println("hello");
    }
}
 

编译代码:
>cd myapp
>mvn compile

编译后产生target目录,里面存放的就是编译后的字节码文件

执行编译后的代码:
>cd target/classes
>java App

执行单元测试
AppTest.java
package com.mycompany.app;

import junit.framework.TestCase;

public class AppTest extends TestCase{
   
    public void testApp(){
        assertTrue( true );
    }
}
 


查询junit的jar包的坐标
http://mvnrepository.com
在pom.xml中添加junit依赖
<dependencies>    
   <dependency>    
    <groupId>junit</groupId>    
    <artifactId>junit</artifactId>    
    <version>3.8.2</version>    
   </dependency>    
</dependencies>
 

>mvn test

清除target

>mvn clean

打包

>mvn package

发布到本地仓库

>mvn install

修改本地仓库位置
默认位置在操作系统的~/.m2文件夹中,修改方法如下:
1,取消maven目录/conf/settings.xml配置文件中的注释的该行代码
<localRepository>/path/to/lacal/repo</localRepository>

2,将其中的路径修改为自己希望的路径:
<localRepositroy>d:/maven/repo</localRepository>

3,并将settings.xml文件拷贝一份到D:/maven目录中

maven查找依赖的顺序
1从本地仓库中查找:maven/repo
2从私有仓库(如果配置的话)中查找所需依赖
3从中央仓库(http://repo.maven.apache.org)中查找

使用Archetype快速创建项目

>mvn archetype:generate

根据命令行提示选择要创建的maven项目类型及坐标

我选择的是320,快速创建基本的java工程
自动创建的工程内容如下:
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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>myapp</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>myapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>
 

App.java
package com.mycompany.app;

/**
 * Hello world!
 *
 */
public class App{
    public static void main( String[] args ){
        System.out.println( "Hello World!" );
    }
}
 

AppTest.java
package com.mycompany.app;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
 * Unit test for simple App.
 */
public class AppTest extends TestCase{
    /**
     * Create the test case
     *
     * @param testName name of the test case
     */
    public AppTest( String testName ){
        super( testName );
    }

    /**
     * @return the suite of tests being tested
     */
    public static Test suite(){
        return new TestSuite( AppTest.class );
    }

    /**
     * Rigourous Test :-)
     */
    public void testApp(){
        assertTrue( true );
    }
}
 


使用M2Eclipse插件创建项目

MyEclipse较新的版本都支持了maven插件,如果没有,到这里下载插件

http://eclipse.org/m2e/

如果成功安装了插件,就可以直接使用MyEclipse创建Maven项目了,它会自动帮你组织好工程目录的。

在web项目中使用jetty服务
在pom.xml中添加jetty插件
<build>    
    <finalName>struts</finalName>    
    <plugins>    
         <plugin>    
          <groupId>org.mortbay.jetty</groupId>    
          <artifactId>jetty-maven-plugin</artifactId>    
          <configuration>    
            <scanIntervalSeconds>10</scanIntervalSeconds>    
            <webApp>    
              <contextPath>/</contextPath>    
            </webApp>    
             <connectors>    
               <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
                  <!--修改默认端口号-->    
                  <port>9090</port>    
                  <maxIdleTime>60000</maxIdleTime>    
               </connector>    
             </connectors>    
          </configuration>    
        </plugin>    
    </plugins>    
</build> 
 

为了修改版本号的方便可以使用以下配置
<properties>    
       <junit.version>4.11</junit.version>    
</properties>    
<dependencies>    
<dependency>    
  <groupId>junit</groupId>    
  <artifactId>junit</artifactId>    
  <version>${junit.version}</version>    
  <scope>test</scope>    
</dependency>
 

如果想使用私有工厂,可以在pom.xml中添加私有工厂的配置
<repositories>    
       <repository>    
            <id>local</id>    
            <name>nexus_server</name>    
            <url>http://192.168.1.11:8081/nexus/content/groups/public/</url>    
       </repository>    
</repositories>
 

或者修改全局私有工厂:.m2/settings.xml
<mirror>    
      <id>mirrorId</id>    
      <mirrorOf>*</mirrorOf>    
      <name>Human Readable Name for this Mirror.</name>    
      <url>http://xxxxx</url>    
</mirror> 
 

猜你喜欢

转载自hunthon.iteye.com/blog/1973495