打包成jar有很多方式,直接命令,使用eclipsehttp://www.java1995.cn/blog/item/448
.
但这些方式在使用到第三方包的时候
都不是很方便,尤其对于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
<!--
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
第二步,创建主程序的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
第三步,执行主程序,在控制台中输入 java -jar 主程序.jar -Xms128M
-Xmx256m.在这里我们就不用一一输入主程序引用的第三方包了,我们已经在主程序jar中的MANIFEST.MF文件中定义了Class-
Path属性,这里列出了所有的第三方包.
参考:http://www.java1995.cn/blog/item/449
ANT资料:
http://www.java1995.cn/java1995?pager.offset=0&pageNo=1&query=ant&type=article
分享到:
相关推荐
Java 读写MANIFEST.MF文件中的属性,采用maven打包MANIFEST.MF配置项,通过io流读取配置项
android apk文件里MANIFEST.MF、CERT.SF、CERT.RSA三者的关系(csdn)————程序
webservice CXF 报错:java.lang.NoClassDefFoundError: org/apache/neethi/builders/AssertionBuilder 需要用到此jar文件 通过apache官网可获得。 文件位置\apache-cxf-2.7.13\lib
NULL 博文链接:https://wv1124.iteye.com/blog/573020
cxf-manifest.jar cxf-services-sts-core-2.6.3.jar cxf-services-wsn-api-2.6.3.jar cxf-services-wsn-core-2.6.3.jar cxf-xjc-boolean-2.6.0.jar cxf-xjc-bug671-2.6.0.jar cxf-xjc-dv-2.6.0.jar cxf-xjc-runtime-...
hhclass汉化 用来修改MANIFEST.MF
MANIFEST.MF
META-INF/MANIFEST.MF gnu.io.CommDriver.class gnu.io.CommPort.class gnu.io.CommPortEnumerator.class gnu.io.CommPortIdentifier.class gnu.io.CommPortOwnershipListener.class gnu.io.Configure.class gnu.io....
需要测试的springboot项目,过程中遇到了idea打jar包,应为MANIFEST.MF 文件夹和 MANIFEST.MF 文件不在项目根目录下,导致多次 在本地 java -jar 运行jar包失败,当然放到docker 里作为容器启动也是失败的,后来在 ...
MANIFEST.MF.mf
META-INF/MANIFEST.MFMETA-INF/LICENSE.txtMETA-INF/NOTICE.txtorg.apache.commons.lang.ArrayUtils.class org.apache.commons.lang.BitField.class org.apache.commons.lang.BooleanUtils.class org.apache....
antlr-2.7.6.jar,antlr-2.7.7.jar,aopalliance-1.0.jar,asm-3.3.1.jar,cglib-2.2.jar,classes12.jar,commons-codec-1.7.jar,commons-collections-3.1.jar,commons-collections-3.2.1.jar,commons-lang-2.6.jar,...
java.lang.IllegalStateException: Failed to read Class-Path attribute from manifest of jar file:/XXX 出现这个问题的解决方案就是将原有的jar删除 然后重新下载过一遍就可以使用
当我们需要运行一个与其他jar文件有关联jar文件时,如果jar文件里面的MANIFEST.MF文件没有配置好,那在运行时就要报异常:Exception in thread "main" java.lang.NoClassDefFoundError。原因:未找到关联jar文件里面...
详解MANIFEST.MF文件
META-INF/MANIFEST.MF commonj.timers.CancelTimerListener.java commonj/timers/package.html commonj.timers.StopTimerListener.java commonj.timers.Timer.java commonj.timers.TimerListener.java commonj....
META-INF/MANIFEST.MF commonj.sdo.ChangeSummary.class commonj.sdo.DataGraph.class commonj.sdo.DataObject.class commonj.sdo.helper.CopyHelper.class commonj.sdo.helper.DataFactory.class commonj.sdo....
github上下载cas服务端4.2的CAS项目的时候在用maven构建war包时候,需要远程下载依赖的jar包,在工程中下载太慢啦。请将该资源解压到一下目录: 你自己的maven本地库的根目录\org\jasig\cas,例如我解压后我的本地库...
META-INF/MANIFEST.MF META-INF/NOTICE META-INF/maven/org.mybatis.caches/mybatis-ehcache/pom.properties META-INF/maven/org.mybatis.caches/mybatis-ehcache/pom.xml org.mybatis.caches.ehcache.EhcacheCache....
META-INF/MANIFEST.MF junit.extensions.ActiveTestSuite.class junit.extensions.RepeatedTest.class junit.extensions.TestDecorator.class junit.extensions.TestSetup.class junit.extensions.package-info....