首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

maven项目构建步步深入《2》

2014-01-08 
maven项目构建步步深入《二》?public byte[] generateCaptchaImage(String captchaKey) throws Exception {p

maven项目构建步步深入《二》

?

public byte[] generateCaptchaImage(String captchaKey) throws Exception {private DefaultKaptcha productor;BufferedImage image = productor.createImage(captchaKey);ByteArrayOutputStream out = new ByteArrayOutputStream();try {ImageIO.write(image, "jpg", out);} catch (Exception e) {e.printStackTrace();}return out.toByteArray();}

?其实上一篇里面也有些基础类,这里整理一下(发送邮件和解析xml写入xml):

2.使用spring3.x注解来注入发送邮件:

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans.xsd   http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context.xsd"><context:property-placeholder location="classpath:service.properties" /><bean id="javaMailSender" p:host="${email.host}" p:port="${email.port}"p:username="${email.username}" p:password="${email.password}"><property name="javaMailProperties"><props><prop key="mail.${email.protocol}.auth">${email.auth}</prop></props></property></bean>....

?具体发送邮件实现 java:

  MimeMessage mimeMessage=javaMailSender.createMimeMessage();  MimeMessageHelper messageHelper=new MimeMessageHelper(mimeMessage);  messageHelper.setFrom(systemEmail);  messageHelper.setTo(to);  messageHelper.setSubject(subject);  messageHelper.setText(htmlText,true);  javaMailSender.send(mimeMessage);

?3.根据文件名读取xml

private Document readDocument(){  File dataFile = new File(file);  if(!dataFile.exists()){ dataFile.getParentFile().mkdirs(); Document doc=DocumentFactory.getInstance().createDocument(); Element rootElement = doc.addElement(ELEMENT_ROOT); rootElement.addElement(ELEMENT_ACCOUNTS); writeDocument(doc);  }   try {   return reader.read(new File(file));。。。private void writeDocument(Document doc) {  OutputStreamWriter out = null;  try {    out = new OutputStreamWriter(new FileOutputStream(file));  } catch (FileNotFoundException e) {  // TODO Auto-generated catch block    e.printStackTrace();  }  XMLWriter writer = new XMLWriter(out, OutputFormat.createPrettyPrint());  try {    writer.write(doc);  } catch (IOException e) {    e.printStackTrace();  }finally{     try {out.close();

?

4、读取xml并解析成对象

public Account readAccount(String id) { Document document = readDocument(); Element element = document.getRootElement().element(ELEMENT_ACCOUNTS); for (Element ele : ((List<Element>)element.elements())) { if(ele.elementText(ACCOUNT_ID).equals(id)){ return buildAccount(ele); } } return null; } private Account buildAccount(Element ele) { Account account=new Account(); account.setId(ele.elementText(ACCOUNT_ID)); account.setName(ele.elementText(ACCOUNT_NAME));。。。?聚合场景:当我们有多个模块,我们会想要一次构建多个项目,而不是分别到两个模块目录下分别执行mvn命令。实现:创建一个额外的名为account-aggregator模块,然后听过该模块构建整个项目的所有模块。aggregator作为一个maven项目,特殊的地方为<packaging>pom</packaging>
<artifactId>account-aggregator</artifactId>  ....  <packaging>pom</packaging>  <modules>  <module>../account-email</module>  <module>../account-persist</module>..
1.?对于聚合模块,其打包方式packaging的值必须为pom,否则就无法创建。
2.可以通过在一个打包方式为pom的maven项目中声明任意数量的module元素来实现模块的聚合。
3.每个module的值都是一个当前pom的相对目录。所以说这里的account-email可以和实际它对应的artifactId不一致。继承场景:多个模块pom有着很多相同的配置,需要抽出重复的配置。实现:一处声明,多出使用。和聚合模块一样,需要packaging为pom,我们定义他的artifactId为acount-parent,那么其它模块作为其子模块如下配置:?
<parent><groupId>com.ycq.account</groupId><artifactId>account-parent</artifactId><version>0.0.1-SNAPSHOT</version><relativePath>../account-aggregator/account-parent/pom.xml</relativePath></parent>
??

热点排行