打包成jar有很多方式,直接命令,使用eclipse
但這些方式在使用到第三方包的時候都不是很方便,尤其對於MANIFEST.MF格式難以手寫,經常出錯,相當不方便
此時可以使用ant來完成這些工作
寫道
以下是需要注意的各個要點:
1. Manifest-Version、Main-Class和Class-Path後面跟着一個英文的冒號,冒號後面必須跟着一個空格,然後纔是版本號、類和ClassPath。
2. Class-Path中的各項應使用空格分隔,不是逗號或分號。
3. Class-Path中如果有很多項,寫成一行打包的時候會報錯line too long,這時需要把Class-Path分多行寫。注意:從第二行開始,必須以兩個空格開頭,三個以上我沒試過,不過不用空格開頭和一個空格開頭都是不行的,我已經試過了。
4. Class-Path寫完之後最後一定要有一個空行。
5. jar包內有些配置文件想放在jar包外面,比如文件config.properties:如果這個文件是以路徑方式載入的,比如new file("./config/config.properties"),那麼將config.properties放在jar包相同目錄下的 config目錄下即可,也就是說“./”路徑等價於jar包所在目錄;如果這個文件是以ClassPath下的文件這種方式載入的,比如在Spring 中載入classpath:config.properties,則在MF文件的配置文件的ClassPath中添加“./”,然後將這個配置文件與 jar包放在同一個目錄即可,當然也可以在MF文件的配置文件的ClassPath中添加“./config/”,然後把配置文件都放在jar包相同目錄下的config目錄下。
1. Manifest-Version、Main-Class和Class-Path後面跟着一個英文的冒號,冒號後面必須跟着一個空格,然後纔是版本號、類和ClassPath。
2. Class-Path中的各項應使用空格分隔,不是逗號或分號。
3. Class-Path中如果有很多項,寫成一行打包的時候會報錯line too long,這時需要把Class-Path分多行寫。注意:從第二行開始,必須以兩個空格開頭,三個以上我沒試過,不過不用空格開頭和一個空格開頭都是不行的,我已經試過了。
4. Class-Path寫完之後最後一定要有一個空行。
5. jar包內有些配置文件想放在jar包外面,比如文件config.properties:如果這個文件是以路徑方式載入的,比如new file("./config/config.properties"),那麼將config.properties放在jar包相同目錄下的 config目錄下即可,也就是說“./”路徑等價於jar包所在目錄;如果這個文件是以ClassPath下的文件這種方式載入的,比如在Spring 中載入classpath:config.properties,則在MF文件的配置文件的ClassPath中添加“./”,然後將這個配置文件與 jar包放在同一個目錄即可,當然也可以在MF文件的配置文件的ClassPath中添加“./config/”,然後把配置文件都放在jar包相同目錄下的config目錄下。
第一步,定義一些屬性
1 <!-- name of the output .jar file -->
2 < property name ="jar.name" value ="ourjarfile.jar" />
3 <!-- base directory for distribution target -->
4 < property name ="dist.home" value ="dist" />
5 <!-- base directory for compilation targets -->
6 < property name ="build.home" value ="target" />
7 <!-- The base directory for all libraries (jar) files -->
8 < property name ="lib.home" value ="lib" />
9
3 <!-- base directory for distribution target -->
4 < property name ="dist.home" value ="dist" />
5 <!-- base directory for compilation targets -->
6 < property name ="build.home" value ="target" />
7 <!-- The base directory for all libraries (jar) files -->
8 < property name ="lib.home" value ="lib" />
9
第二步,創建主程序的jar文件,這裏會創建Class-Path的屬性,並生成相應的MANIFEST.MF
1 < target name ="jar" depends ="compile" description="Create jar and MANIFEST.MF" >
2
3
4
5 <!-- create a property containing all .jar files, prefix lib/, and seperated with a space -->
6
7 < pathconvert property ="libs.project" pathsep =" " >
8
9 < mapper >
10
11 < chainedmapper >
12 <!-- remove absolute path -->
13 < flattenmapper />
14 <!-- add lib/ prefix -->
15 < globmapper from ="*" to ="lib/*" />
16 </ chainedmapper >
17 </ mapper >
18 < path >
19 <!-- lib.home contains all jar files, in several subdirectories -->
20 < fileset dir ="${lib.home}" >
21 < include name ="**/*.jar" />
22 </ fileset >
23 </ path >
24 </ pathconvert >
25 <!-- create the jar -->
26 < jar jarfile ="${build.home}/${jar.name}" basedir ="${build.home}/classes" >
27 <!-- define MANIFEST.MF -->
28 < manifest >
29 < attribute name ="Built-By" value ="${user.name}" />
30 < attribute name ="Main-Class" value ="my.path.to.the.main.Application" />
31 < section name ="common" >
32 < attribute name ="Specification-Title" value ="${component.name}" />
33 < attribute name ="Specification-Version" value ="${component.version}" />
34 < attribute name ="Specification-Vendor" value ="${component.vendor}" />
35 < attribute name ="Implementation-Title" value ="${component.name}" />
36 < attribute name ="Implementation-Version" value="${component.version} ${TODAY}" />
37 < attribute name ="Implementation-Vendor" value ="${component.vendor}" />
38 </ section >
39 <!-- finally, use the magically generated libs path -->
40 < attribute name ="Class-Path" value ="${libs.project}" />
41 </ manifest >
42 </ jar >
43 </ target >
44
2
3
4
5 <!-- create a property containing all .jar files, prefix lib/, and seperated with a space -->
6
7 < pathconvert property ="libs.project" pathsep =" " >
8
9 < mapper >
10
11 < chainedmapper >
12 <!-- remove absolute path -->
13 < flattenmapper />
14 <!-- add lib/ prefix -->
15 < globmapper from ="*" to ="lib/*" />
16 </ chainedmapper >
17 </ mapper >
18 < path >
19 <!-- lib.home contains all jar files, in several subdirectories -->
20 < fileset dir ="${lib.home}" >
21 < include name ="**/*.jar" />
22 </ fileset >
23 </ path >
24 </ pathconvert >
25 <!-- create the jar -->
26 < jar jarfile ="${build.home}/${jar.name}" basedir ="${build.home}/classes" >
27 <!-- define MANIFEST.MF -->
28 < manifest >
29 < attribute name ="Built-By" value ="${user.name}" />
30 < attribute name ="Main-Class" value ="my.path.to.the.main.Application" />
31 < section name ="common" >
32 < attribute name ="Specification-Title" value ="${component.name}" />
33 < attribute name ="Specification-Version" value ="${component.version}" />
34 < attribute name ="Specification-Vendor" value ="${component.vendor}" />
35 < attribute name ="Implementation-Title" value ="${component.name}" />
36 < attribute name ="Implementation-Version" value="${component.version} ${TODAY}" />
37 < attribute name ="Implementation-Vendor" value ="${component.vendor}" />
38 </ section >
39 <!-- finally, use the magically generated libs path -->
40 < attribute name ="Class-Path" value ="${libs.project}" />
41 </ manifest >
42 </ jar >
43 </ target >
44
第三步,執行主程序,在控制檯中輸入 java -jar 主程序.jar -Xms128M -Xmx256m.在這裏我們就不用一一輸入主程序引用的第三方包了,我們已經在主程序jar中的MANIFEST.MF文件中定義了Class- Path屬性,這裏列出了所有的第三方包.