码迷,mamicode.com
首页 > 其他好文 > 详细

MANEN坐标与依赖

时间:2017-04-06 19:59:20      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:ase   1.0   user   ring   scope   snapshot   aging   javamail   项目打包   

前言:网上有很多这个例子,讲的也很透彻,但看不如实际敲一遍,加深理解,这里根据步骤一步步进行,只是学习的小过程而已。
 
Coordinate 任何一个构件都可以使用maven坐标唯一标识。
几个关健要素:
groupId    artifactId   version   packaging    classifier  maven中央库:http://repo.maven.org/maven2
groupId:项目  通常用域名的反向来表示如:com.help18.hello
artifactId:Mavne项目模块 hello-mvn01
version:版本
packaging: jar或war  即打包方式
classifier:定义构建输出一些附属构件
 
一、Account-Email项目结构
技术分享
 
 
二、创建POM文件,具体POM文件内容:
这里面涉及spring的依赖,junit的坐标,icegreen的包依赖;邮件的发送我们利用javax.mail来实现,详细pom内容如下:
<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.help18.mvn.account</groupId>
  <artifactId>account-email</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>account-email</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
       <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-core</artifactId>
              <version>4.2.4.RELEASE</version>
       </dependency>
       <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-beans</artifactId>
              <version>4.2.4.RELEASE</version>
       </dependency>
       <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-context</artifactId>
              <version>4.2.4.RELEASE</version>
       </dependency>
       <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-context-support</artifactId>
              <version>4.2.4.RELEASE</version>
       </dependency>
       <dependency>
              <groupId>javax.mail</groupId>
              <artifactId>mail</artifactId>
              <version>1.5.0-b01</version>
       </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.10</version>
      <scope>test</scope>
    </dependency>
    <dependency>
       <groupId>com.icegreen</groupId>
       <artifactId>greenmail</artifactId>
       <version>1.3.1b</version>
       <scope>test</scope>
    </dependency>
  </dependencies>
</project>
注:
由于在项目打包编译时会报如下错误,所以在pom文件中加入,这样会避免出错。
运行Maven是报错:No goals have been specified for this build
  <build>
       <defaultGoal>compile</defaultGoal>
  </build>

三、编写程序代码

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:是我们第三步中创建的接口实现类 
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

       <bean id="propertyConfigurer"

       class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
              <property name="location" value="classpath:service.properties"></property>
       </bean>

       <bean id="javaMailServer" class="org.springframework.mail.javamail.JavaMailSenderImpl">
              <property name="protocol" value="${email.protocol}"/>
              <property name="host" value="${email.host}"/>
              <property name="port" value="${email.port}"/>
              <property name="username" value="${email.username}"/>
              <property name="password" value="${email.password}"/>
              <property name="javaMailProperties">
                     <props>
                           <prop key="mail.${email.protocol}.auth">${email.auth}</prop>
                     </props>
              </property>
       </bean>

       <bean id="accountEmailService"
              class="com.help18.account.email.AccountEmailServiceImpl">
              <property name="javaMailSender" ref="javaMailServer"></property>
              <property name="systemEmail" value="${email.systemEmail}"/>
       </bean>

</beans>

五、创建properties属性文件

email.protocol=smtp
email.host=smtp.163.com
email.port=25
email.username=XXXXX@163.com
email.password=password
email.auth=true
email.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 = "<H3>Test  mail.163.com </H3>";

        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.排除依赖
<exclusions>
     <exclusion>
          <groudId></groupId>
          <artifactId></artifactId>
     </exclusion>
</exclusions>

2.归类依赖

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <springframework.version>4.2.4.RELEASE</springframework.version>
  </properties>

  <dependencies>
       <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-core</artifactId>
              <version>${springframework.version}</version>
       </dependency>
  </dependencies>

总结:

maven坐标与依赖是maven中一个比较重要的知识点,它标识了你的jar包或war包的位置,以及依赖的各个包,由于依赖的关系,无需手动添加这个项目中所用到的资源,配置好pom文件后会自动从私服或中央仓库下载包到本地,这样会省去很多的麻烦,这也是maven构建项目最大的优点。

创建一个maven项目,主要按照maven要求的目录结构进行创建,最重要的是pom.xml文件的构建。

上面的例子是基于spring框架实现的,之前没有用maven构建项目时,需要一个个找jar包下载,再编辑项目把包添进来,太麻烦了,现在非常简单,执行了mvn compile可以自动下载依赖的包。

 

 

MANEN坐标与依赖

标签:ase   1.0   user   ring   scope   snapshot   aging   javamail   项目打包   

原文地址:http://www.cnblogs.com/rico2017/p/6674891.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!