读书人

maven-施用入门

发布时间: 2012-09-25 09:55:59 作者: rapoo

maven-使用入门

一.? 编写pom

在项目根目录下编写

<?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/maven-v4_0_0.xsd">
?
??<modelVersion>4.0.0</modelVersion>
??<groupId>com.zqmlxl.app</groupId>
??<artifactId>hello-world</artifactId>
??<version>1.0-SNAPSHOT</version>
??<name>my hello world maven project</name>
?</project>

?

?

maven2和maven3中modelVersion必须为4.0.0

groupId一般以公司信息起名

artifactId一般以项目信息起名

version版本号,SNAPSHOT意为快照,项目还在开放中,是不稳定版本

name填写该元素以方便信息交流

?

二. 编写主代码

?package main.java.com.zqmlxl.app.helloworld;

public class HelloWorld {

?public String sayHello(){
??return "hello world!";
?}

}

?

默认情况下,maven假设项目主代码位于src/main/java

目录

在项目根目录下运行mvn clean compile编译项目

?

三. 编写测试代码

?

添加依赖,修改pom.xml,添加如下内容

<dependencies>
???<dependency>
????<groupId>junit</groupId>
????<artifactId>junit</artifactId>
????<version>4.7</version>
????<scope>test</scope>
???</dependency>
??</dependencies>

?

?

junit测试代码:

?

?package com.zqmlxl.app.helloworld;


import org.junit.Assert;
import org.junit.Test;

import com.zqmlxl.app.helloworld.HelloWorld;


public class HelloworldTest {

?@Test
?public void test() {
??HelloWorld entity = new HelloWorld();
??String result = entity.sayHello();
??Assert.assertEquals("hello world", result);
?}

}

?

在项目根目录运行

mvn clean test

?

四. 打包运行

mvn clean package

mvn clean install

?

运行jar中的main方法需修改pom.xml

<?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/maven-v4_0_0.xsd">
?
??<modelVersion>4.0.0</modelVersion>
??<groupId>com.zqmlxl.app</groupId>
??<artifactId>hello-world</artifactId>
??<version>1.0-SNAPSHOT</version>
??<name>my hello world maven project</name>
??<build>
???<plugins>
????<plugin>
?????<groupId>org.apache.maven.plugins</groupId>
?????<artifactId>maven-shade-plugin</artifactId>
?????<version>1.2.1</version>
?????<executions>
??????<execution>
???????<phase>package</phase>
???????<goals>
????????<goal>shade</goal>
???????</goals>
???????<configuration>
????????<transformers>
?????????<transfomer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
??????????<mainClass>com.zqmlxl.app.helloworld.HelloWorld</mainClass>
??????????
?????????</transfomer>
????????</transformers>
???????</configuration>
??????</execution>
?????</executions>
????</plugin>
???</plugins>
??</build>
??<dependencies>
???<dependency>
????<groupId>junit</groupId>
????<artifactId>junit</artifactId>
????<version>4.7</version>
????<scope>test</scope>
???</dependency>
??</dependencies>
?</project>

?

五. 使用archetype生成项目骨架

maven3中直接输入一下命令

mvn archetype:generate

?

maven2

mvn org.apache.maven.plugins:maven-archetype-plugin:2.0-alpha-5:generate

?

六. m2eclipse简单使用

1. 导入maven项目

file->import->maven project->next->选择项目根目录->finish

?

2. 创建maven项目

file->new->other->maven project -> next->使用默认选项,next->双击maven-archetype-quickstart->next->输入groupId .artifactId,version,package等信息->finish

?

3. 运行mvn命令

右键maven项目->Run as

如果需要自定义,就选maven build选项,填写Goals信息即可

?

?

读书人网 >软件开发

热点推荐