前言:网上有很多这个例子,讲的也很透彻,但看不如实际敲一遍,加深理解,这里根据步骤一步步进行,只是学习的小过程而已。
Coordinate 任何一个构件都可以使用maven坐标唯一标识。
几个关健要素:
groupId artifactId version packaging classifier maven中央库:http://repo.maven.org/maven2
groupId:项目 通常用域名的反向来表示如:com.help18.helloartifactId:Mavne项目模块 hello-mvn01version:版本packaging: jar或war 即打包方式classifier:定义构建输出一些附属构件
一、Account-Email项目结构
![](https://images2015.cnblogs.com/blog/1112012/201704/1112012-20170406181239113-912678080.png)
二、创建POM文件,具体POM文件内容:
这里面涉及spring的依赖,junit的坐标,icegreen的包依赖;邮件的发送我们利用javax.mail来实现,详细pom内容如下:
4.0.0 com.help18.mvn.account account-email 0.0.1-SNAPSHOT jar account-email http://maven.apache.org UTF-8 org.springframework spring-core 4.2.4.RELEASE org.springframework spring-beans 4.2.4.RELEASE org.springframework spring-context 4.2.4.RELEASE org.springframework spring-context-support 4.2.4.RELEASE javax.mail 1.5.0-b01 junit junit 4.10 test com.icegreen greenmail 1.3.1b test
注:
由于在项目打包编译时会报如下错误,所以在pom文件中加入,这样会避免出错。
运行Maven是报错: No goals have been specified for this build
compile
三、编写程序代码
1.在src/main/java中创建AccountEmailService接口
package com.help18.account.email;public interface AccountEmailService { public void sendMail(String to,String subject,String htmlText) throws AccountEmailException;}
2.创建异常处理类AccountEmailException ,此类继承了Exception类
package com.help18.account.email;public class AccountEmailException extends Exception { private static final long serialVersionUID = -2541187591129155636L; public AccountEmailException(String message){ super(message); } public AccountEmailException(String message,Throwable throwable){ super(message,throwable); }}
3.创建接口实现类AccountEmailServiceImpl
这里引入了javamail,我们是依赖于此进行邮件发送的
package com.help18.account.email;import javax.mail.MessagingException;import javax.mail.internet.MimeMessage;import org.springframework.mail.javamail.JavaMailSender;import org.springframework.mail.javamail.MimeMessageHelper;public class AccountEmailServiceImpl implements AccountEmailService { private JavaMailSender javaMailSender; private String systemEmail; public void sendMail(String to, String subject, String htmlText) throws AccountEmailException { // TODO Auto-generated method stub try{ MimeMessage msg = javaMailSender.createMimeMessage(); MimeMessageHelper msgHelper = new MimeMessageHelper(msg); msgHelper.setFrom(systemEmail); msgHelper.setTo(to); msgHelper.setSubject(subject); msgHelper.setText(htmlText,true); /*发送邮件*/ javaMailSender.send(msg); } catch(MessagingException e){ throw new AccountEmailException("Faild to send mail.",e); } } public JavaMailSender getJavaMailSender() { return javaMailSender; } public void setJavaMailSender(JavaMailSender javaMailSender) { this.javaMailSender = javaMailSender; } public String getSystemEmail() { return systemEmail; } public void setSystemEmail(String systemEmail) { this.systemEmail = systemEmail; }}
四、创建spring的配置文件Account-Email.xml
文件位置:src/main/resources下
这里面定义了几个Bean,如下内容:
1.propertyConfigurer : 用于读取properties属性文件,它读取service.properties的文件
2.javaMailServer:配置邮件服务器的相关信息,地址、端口、用户名、密码等
3.accountEmailSeervice:是我们第三步中创建的接口实现类
${email.auth}
五、创建properties属性文件
email.protocol=smtpemail.host=smtp.163.comemail.port=25email.username=XXXXX@163.comemail.password=passwordemail.auth=trueemail.systemEmail=email
六、创建单元测试类
package com.help18.account.email;import static junit.framework.Assert.assertEquals;import javax.mail.Message;import org.junit.After;import org.junit.Before;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.icegreen.greenmail.util.GreenMail;import com.icegreen.greenmail.util.GreenMailUtil;import com.icegreen.greenmail.util.ServerSetup;public class AccountEmailServiceTest { private GreenMail greenMail; @Before public void startMailServer() throws Exception { greenMail = new GreenMail(ServerSetup.SMTP); greenMail.setUser("xxxx@163.com", "xxxxx"); greenMail.start(); } @Test public void testSendMail() throws Exception{ @SuppressWarnings("resource") ApplicationContext ctx = new ClassPathXmlApplicationContext("account-email.xml"); AccountEmailService accountEmailService = (AccountEmailService)ctx.getBean("accountEmailService"); String subject = "Test Subject 20170401"; String htmlText = "Test mail.163.com
"; System.out.println("test-------"); accountEmailService.sendMail("xxxx@163.com", subject, htmlText); System.out.println("------test send mail finished-------"); greenMail.waitForIncomingEmail(2000,1); Message[] msgs = greenMail.getReceivedMessages(); System.out.println("msgs.lengh="+msgs.length); assertEquals(1,msgs.length); assertEquals(subject,msgs[0].getSubject()); assertEquals(htmlText,GreenMailUtil.getBody(msgs[0]).trim()); } @After public void stopMailServer() throws Exception{ greenMail.stop(); }}
二、MAVEN依赖的经验
1.排除依赖
2.归类依赖
UTF-8 4.2.4.RELEASE org.springframework spring-core ${springframework.version}
总结:
maven坐标与依赖是maven中一个比较重要的知识点,它标识了你的jar包或war包的位置,以及依赖的各个包,由于依赖的关系,无需手动添加这个项目中所用到的资源,配置好pom文件后会自动从私服或中央仓库下载包到本地,这样会省去很多的麻烦,这也是maven构建项目最大的优点。
创建一个maven项目,主要按照maven要求的目录结构进行创建,最重要的是pom.xml文件的构建。
上面的例子是基于spring框架实现的,之前没有用maven构建项目时,需要一个个找jar包下载,再编辑项目把包添进来,太麻烦了,现在非常简单,执行了mvn compile可以自动下载依赖的包。