首先:我们使用集成了maven 的eclipse 创建一个普通的porject .
在左边的工作项目空白处右键New-->other 选择maven project.如图。
第一个多选框是创建一个普通典型的项目,第二个是工作地址。
finsh 如图:
说明:
package com.picc; import org.junit.Assert; import org.junit.Test; public class Student { public String toSay(){ return "hello maven"; } @Test public void testStudent(){ Student stu = new Student(); String result = stu.toSay(); Assert.assertEquals("hello maven", result); } }我们引用了 junit 4.x的 包,那么我们这个时候 就应该 下载包让maven 管理,
<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.soliu</groupId> <artifactId>testmvn</artifactId> <version>0.0.1-SNAPSHOT</version> <name>com.soliu.testmvn</name> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.7</version> <!-- test 只能test 包有作用,而对main 包下没有效果,compile 可以在test 和 main 包下使用 测试结果,scope=test 运行 maven install 会报 找不到 包--> <scope>compile</scope> </dependency> </dependencies> </project>
<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.soliu</groupId> <artifactId>testmvn</artifactId> <version>0.0.1-SNAPSHOT</version> <name>com.soliu.testmvn</name> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.7</version> <!-- test 只能test 包有作用,而对main 包下没有效果,compile 可以在test 和 main 包下使用 测试结果,scope=test 运行 maven install 会报 找不到 包--> <scope>compile</scope> </dependency> </dependencies> <build> <finalName>testmaven</finalName> <defaultGoal>install</defaultGoal> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <skip>true</skip> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.5.1</version> <configuration> <source>1.6</source> <target>1.6</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build> </project>如果不加 会报一个
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.2:war (default-war) on project testmvn: Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode) -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
[INFO] Scanning for projects... [WARNING] [WARNING] Some problems were encountered while building the effective model for com.picc:mvn:jar:0.0.1-SNAPSHOT [WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-surefire-plugin is missing. @ line 21, column 18 [WARNING] [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build. [WARNING] [WARNING] For this reason, future Maven versions might no longer support building such malformed projects. [WARNING] [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building com.picc.mvn 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mvn --- [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ mvn --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ mvn --- [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ mvn --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mvn --- [INFO] Tests are skipped. [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ mvn --- [INFO] Building jar: E:\workspace\testmvn\target\testmaven.jar [INFO] [INFO] --- maven-install-plugin:2.4:install (default-install) @ mvn --- [INFO] Installing E:\workspace\testmvn\target\testmaven.jar to D:\maven\.m2\repository\com\picc\mvn\0.0.1-SNAPSHOT\mvn-0.0.1-SNAPSHOT.jar [INFO] Installing E:\workspace\testmvn\pom.xml to D:\maven\.m2\repository\com\picc\mvn\0.0.1-SNAPSHOT\mvn-0.0.1-SNAPSHOT.pom [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.281s [INFO] Finished at: Wed Aug 21 15:18:07 CST 2013 [INFO] Final Memory: 7M/17M [INFO] ------------------------------------------------------------------------项止中 target 中也有了内容 。
如果 是java web 项目 就会 使用 maven install 打成 XXX.war 包。 结构如图: