11/07/2012

Maven package jar file, include all the dependencies.

1. If we do not have dependencies with scope other than "runtime". We can config pom.xml simply in this way:

01 <plugin>
02                 <groupId>org.apache.maven.plugins</groupId>
03                 <artifactId>maven-assembly-plugin</artifactId>
04                 <version>2.2.1</version>
05 
06                 <executions>
07 
08                     <execution>
09                         <id>package-jar-with-dependencies</id>
10                         <phase>package</phase>
11                         <goals>
12                             <goal>single</goal>
13                         </goals>
14                         <configuration>
15                             <appendAssemblyId>false</appendAssemblyId>
16                             <descriptorRefs>
17                                 <descriptorRef>jar-with-dependencies</descriptorRef>
18                             </descriptorRefs>
19                         </configuration>
20                     </execution>
21 
22                     <execution>
23                         <id>make-assembly</id>
24                         <phase>package</phase>
25                         <goals>
26                             <goal>single</goal>
27                         </goals>
28                         <configuration>
29                             <descriptors>
30                                 <descriptor>assembly/assembly.xml</descriptor>
31                             </descriptors>
32                         </configuration>
33                     </execution>
34                 </executions>
35 </plugin>

Now all the dependencies will be added into the jar file. If we want to assembly the project, we can add another assembly.xml to control the assemble.

"assembly.xml":

<assembly>
02   <id>bin</id>
03   <formats>
04     <format>zip</format>
05   </formats>
06   <dependencySets>
07     <dependencySet>
08       <useProjectArtifact>true</useProjectArtifact>
09       <excludes>
10         <exclude>log4j:log4j</exclude>
11       </excludes>
12       <outputDirectory>lib</outputDirectory>
13     </dependencySet>
14   </dependencySets>
15   <fileSets>
16     <fileSet>
17       <directory>docs</directory>
18       <outputDirectory>/docs</outputDirectory>
19       <includes>
20         <include>*.*</include>
21       </includes>
22     </fileSet>
23     <fileSet>
24       <directory>logs</directory>
25       <outputDirectory>/logs</outputDirectory>
26       <includes>
27         <include>*.*</include>
28       </includes>
29     </fileSet>
30     <fileSet>
31       <directory>scripts</directory>
32       <outputDirectory>/bin</outputDirectory>
33       <includes>
34         <include>*.*</include>
35       </includes>
36     </fileSet>
37      <fileSet>
38       <directory>config</directory>
39       <outputDirectory>/conf</outputDirectory>
40       <includes>
41         <include>*.*</include>
42       </includes>
43     </fileSet>
44   </fileSets>
45 </assembly>

Reference:

2. If we have dependencies with scope other than "runtime", e.g. "system".

<dependency>
  <groupId>sample-commons</groupId>
  <artifactId>security</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${basedir}/lib/sample-commons-security-1.0.0.jar</systemPath>
</dependency>

If we use this way, jar files with scope "system" will be ignored when assembly into jar file. We can config like this to include all the jar files. Write jar-with-dependencies Descriptor ourselves:
<plugin>
02                 <groupId>org.apache.maven.plugins</groupId>
03                 <artifactId>maven-assembly-plugin</artifactId>
04                 <version>2.2.1</version>
05 
06                 <executions>
07 
08                     <execution>
09                         <id>package-jar-with-dependencies</id>
10                         <phase>package</phase>
11                         <goals>
12                             <goal>single</goal>
13                         </goals>
14                         <configuration>
15                             <appendAssemblyId>false</appendAssemblyId>
16                             <descriptors>
17                                 <descriptor>assembly/jar-with-dependencies.xml</descriptor>
18                             </descriptors>
19 
20                         </configuration>
21                     </execution>
22 
23                     <execution>
24                         <id>make-assembly</id>
25                         <phase>package</phase>
26                         <goals>
27                             <goal>single</goal>
28                         </goals>
29                         <configuration>
30                             <descriptors>
31                                 <descriptor>assembly/assembly.xml</descriptor>
32                             </descriptors>
33                         </configuration>
34                     </execution>
35                 </executions>
36 </plugin>


jar-with-dependencies.xml:

01 <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" 
02   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
03   xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
04   
05   <id>lib</id>
06   <formats>
07     <format>jar</format>
08   </formats>
09   <includeBaseDirectory>false</includeBaseDirectory>
10   <dependencySets>
11     <dependencySet>
12       <outputDirectory>/</outputDirectory>
13       <useProjectArtifact>true</useProjectArtifact>
14       <unpack>true</unpack>
15       <scope>runtime</scope>
16     </dependencySet>
17     <dependencySet>
18       <outputDirectory>/</outputDirectory>
19       <useProjectArtifact>true</useProjectArtifact>
20       <unpack>true</unpack>
21       <scope>system</scope>
22     </dependencySet>
23   </dependencySets>
24 </assembly>

In this way, we can assemble all the dependencies into the jar file packaged.

11/06/2012

Customize Header "Message-Id" in MimeMessage, JavaMail



JavaMail's MimeMessage.saveChanges() method generates a new Message-ID for a message on every call.

While if use spring's JavaMailSender to send message,

msg.saveChanges() is called in org.springframework.mail.javamail.JavaMailSenderImpl.doSend.




01 public void saveChanges() throws MessagingException {
02     modified = true;
03     saved = true;
04     updateHeaders();
05     }
06 
07 
08 protected void updateHeaders() throws MessagingException {
09     MimeBodyPart.updateHeaders(this);    
10     setHeader("MIME-Version", "1.0");
11         updateMessageID();
12 
13     /*
14      * If we've cached a Multipart or Message object then
15      * we're now committed to using this instance of the
16      * object and we discard any stream data used to create
17      * this object.
18      */
19     if (cachedContent != null{
20         dh = new DataHandler(cachedContent, getContentType());
21         cachedContent = null;
22         content = null;
23         if (contentStream != null{
24         try {
25             contentStream.close();
26         } catch (IOException ioex{ }    // nothing to do
27         }
28         contentStream = null;
29     }
30     }
31 
32 protected void updateMessageID() throws MessagingException {
33     setHeader("Message-ID", 
34           "<" + UniqueValue.getUniqueMessageIDValue(session+ ">");
35           
36     }
So, even if we assign value to mimeMessage's "Message-ID", when this message is sent, spring will rewrite it.

In this way, if we want to customize header "Message-ID", we need to define a new class to extend the class MimeMessage and override the method saveChanges() or method updateMessageID().

For example:

01 public class CustomizedMimeMessage extends MimeMessage {
02 
03     private String messageID;
04     
05     public CustomizedMimeMessage(Session session{
06         super(session);
07     }
08     
09     @Override
10     protected void updateMessageID() throws MessagingException {
11     
12         setHeader("Message-ID", messageID);
13     }
14 
15     public String getMessageID() {
16         return messageID;
17     }
18 
19     public void setMessageID(String messageID{
20         this.messageID = messageID;
21     }
22 }





Then, we can customize the message's "Message-ID" in this way:
String temp = "<"+UniqueId+username"@sample.com>";
                    
msg.setMessageID(temp);

OR, we can just override the method updateHeaders() not to call updateMessageID().

10/30/2012

JavaScript clean the web page in Iframe

document.getElementById("IDofIframe").src = "about:blank";

OR

document.frames[ "iframe1"].src   = "about:blank ";

JavaScript let element adapt to window size


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>JavaScript let element adapt to window size</title>
<style type="text/css">
*{margin:0;padding:0;}
#bg{
position:absolute;
top:0px;
left:0px;
z-index:-999;
}
</style>
</head>
<body>
<div id="bg"><img  src="http://www.webdm.cn/images/20091016/jihyun-bestibelli06.jpg" alt="HIGH"></div>
<input type="text" id="msg" name="msg" size="50"/>
<script type="text/javascript">
window.onresize = window.onload = function(){
var w,h
if(!!(window.attachEvent && !window.opera))
{
h = document.documentElement.clientHeight;
w = document.documentElement.clientWidth;
}else{
h = window.innerHeight;
w = window.innerWidth;
}
document.getElementById('msg').value  ='窗口大小:' + 'width:' + w + '; height:'+h;
var bgImg = document.getElementById('bg').getElementsByTagName('img')[0];
bgImg.width = (w - 5);
bgImg.height= (h-5) ;

}
</script>
</body>
</html>

Submit form to Pop Up Window or Iframe


Submit to POP UP window:  
          document.form1.action = "test.jsp";
            window.open("", 'win', 'width=420px,height=300px,resizable=yes,top=50px,left=200px,toolbar=no, menubar=no, location=no, status=no');
            document.form1.target = "win";
            document.form1.submit();

Submit to IFRAME:
<body>
<form name="form1" method="post" action="test.jsp" target="save">
<input type="submit" name="Submit" value="Submit">
</form>

<iframe src="test.jsp" name="save" height="0" width="0"></iframe>
</body>

10/25/2012

Bat file basic cmd

@echo off
echo Current disk:%~d0
echo Current disk and path:%~dp0
echo Current bat file full path:%~f0
echo 当前盘符和路径的短文件名格式:%~sdp0
echo Current cmd directory:%cd%

@echo off
echo 当前盘符:%~d0
echo 当前盘符和路径:%~dp0
echo 当前批处理全路径:%~f0
echo 当前盘符和路径的短文件名格式:%~sdp0
echo 当前CMD默认目录:%cd%