maven 学习第二步 构建项目

首先:我们使用集成了maven 的eclipse 创建一个普通的porject .

在左边的工作项目空白处右键New-->other 选择maven project.如图。

maven 学习第二步 构建项目_第1张图片

第一个多选框是创建一个普通典型的项目,第二个是工作地址。

maven 学习第二步 构建项目_第2张图片

finsh 如图:

maven 学习第二步 构建项目_第3张图片

说明:

  • src/main/java 存放 项目原代码,而 src/main/resources 存放资源文件
  • src/test/java   存放项目测试代码,而src/test/resource  存放测试资源文件。
  • target 存放编译 文件
  • src 下有两个文件夹,main 文件夹下存放对src/main/java  的引用,但大部分时候 看上去是空的,而test 则存放对src/test/java   存放项目测试代码引用。
  • pom.xml maven 的配置文件,主要用来配置对包的引用。

接下来,我们写测试代码。
在src/main/java 下创建一个java文件。
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 管理,
修改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.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 加上一个插件。最终的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.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

接下来,我们使用maven 的功能为我们编译代码,如图:
maven 学习第二步 构建项目_第4张图片

然后如果不出异外,就可以编译成功了。结果如图:
[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 中也有了内容 。
maven 学习第二步 构建项目_第5张图片

这个testmaven.jar 是包 是student.class 文件,和maven 的pom.xml 和pom.properties 文件。

如果 是java web 项目 就会 使用 maven install 打成 XXX.war 包。 结构如图:

maven 学习第二步 构建项目_第6张图片


你可能感兴趣的:(maven 学习第二步 构建项目)