简介:学习《Java Persistence with Hibernate》时,书中使用Maven来管理项目。
一、环境搭建
1 - 安装Maven至/usr/local
2 - 软映射
3 - 效果图
4 - 配置PATH
#PATH
export CATALINA_HOME=/usr/local/tomcat
export M2_HOME=/usr/local/maven
export ANT_HOME=/usr/local/ant
export MYSQL_HOME=/usr/local/mysql
export PATH=$PATH:$CATALINA_HOME/bin:$M2_HOME/bin:$MYSQL_HOME/bin:$ANT_HOME/bin
5 - 查看配置
6 - 查看仓库
二、项目搭建
1 - 创建模版
mvn archetype:generate #核心命令,负责目录生成
-DarchetypeCatalog=internal #读取内置archetype-catalog.xml文件
-DgroupId=com.ray.helloworld #公司域名倒写
-DartifactId=Ray #项目名称
-DarchetypeArtifactId=maven-archetype-quickstart #模版名称(Java应用)
-Dversion=0.0.1-snapshot #项目版本号
2 - 初次创建项目会下载依赖
3 - 生成结构图(源代码和测试)
4 - 生成的pom.xml
三、编写源代码
1 - 应用类:HelloWorld.java
package com.ray.helloworld;
/**
* Hello world!
*
*/
public class HelloWorld
{
public static String getHello()
{
return "Hello World!";
}
}
2 - 测试类
package com.ray.helloworld;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class TestHelloWorld
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public TestHelloWorld( String testName )
{
super( testName );
}
/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( TestHelloWorld.class );
}
/**
* Rigourous Test :-)
*/
public void testApp()
{
assertTrue( true );
}
public void testHelloWorld()
{
String result = HelloWorld.getHello();
assertEquals(result, "Hello World!");
}
}
四、运行
1 - 编译应用类
2 - 结果
3 - 编译测试类
4 - 结果
5 - 目录中新生成的文件
- target | classes :源代码的类文件
- target | test-classes :测试代码的类文件
- maven-status :对代码文件及类文件的描述文件
- surefire-status :运行时报告