基本Java,它显示一个带有图像的GUI窗口(picture.jpg)和从app.properties读取的文本标签,也使用log4j库。
它具有以下结构(构建和dist目录由ant'build'和'dist'目标添加):
The entire app as an Eclipse project
基本功能:
'build'目标将所有源文件编译到'build'目录,并将运行应用程序所需的其他资源复制到'build'目录。然后可以使用'run'目标运行。
'dist'目标只是将整个'build'目录拉成jar文件并将其放入'dist'文件夹。
问题
我希望jar完全独立。目前它使用'runjar'目标运行,也只是点击jar。
但是,如果我将jar文件移动到其他地方,它将无法启动,因此它仍然依赖于jar之外的文件,尽管jar包含所有必需的文件。
这也可以从jar的 MANIFEST.MF 文件中看到。
Main-Class: com.my.demo.app.Main
Class-Path: ../build/classes/ ../build/img/ ../build/conf/ ../build/ .
./build/lib/log4j-1.2.17.jar
此外,如果我做错了其他事情,欢迎任何其他反馈。
的build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project basedir="." name="DemoApp" default="build">
<property file="app.properties"/>
<path id="compile.classpath" description="Classpath for compiling classes to 'build' dir">
<fileset dir="lib">
<include name="*.jar"/>
</fileset>
</path>
<path id="run.classpath" description="Classpath for running compiled app in 'build' dir">
<pathelement location="${build.classes.dir}"/>
<pathelement location="${build.dir}/img"/>
<pathelement location="${build.dir}/conf"/>
<pathelement location="${build.dir}"/>
<fileset dir="${build.dir}/lib">
<include name="*.jar"/>
</fileset>
</path>
<target name="clean" description="Remove files generated by 'build' and 'dist'">
<delete dir="${build.dir}"/>
<delete dir="${dist.dir}"/>
</target>
<target name="compile" description="Compiles the source files">
<mkdir dir="${build.classes.dir}"/>
<javac
srcdir="${src.dir}"
destdir="${build.classes.dir}"
debug="true"
debuglevel="lines,vars,source"
encoding="utf-8"
compiler="modern"
target="1.7"
source="1.7"
includeantruntime="false">
<classpath refid="compile.classpath"/>
</javac>
</target>
<target name="build" depends="compile" description="Builds project">
<!--Copy app.properties to build/app.properties-->
<copy todir="${build.dir}">
<fileset dir="${basedir}" includes="*.properties"/>
</copy>
<!--Copy picture.jpg to build/img/picture.jpg-->
<copy todir="${build.dir}/img">
<fileset dir="${basedir}/etc/img"/>
</copy>
<!--Copy jars in lib to build/lib-->
<copy todir="${build.dir}/lib">
<fileset dir="${lib.dir}" includes="*.jar"/>
</copy>
<!--copy log4j.xml to build/conf/log4j.xml -->
<copy todir="${build.dir}/conf">
<fileset dir="${basedir}/etc" includes="log4j.xml"/>
</copy>
</target>
<target name="run" depends="build" description="Execute application located in 'build'">
<java classname="com.my.demo.app.Main" fork="yes" classpathref="run.classpath"/>
</target>
<target name="dist" depends="clean,build" description="Create distributable jar">
<mkdir dir="${dist.dir}"/>
<jar jarfile="${dist.dir}/DemoApp.jar" basedir="${build.classes.dir}"/>
<manifestclasspath property="manifest.classpath" jarfile="${dist.dir}/DemoApp.jar">
<classpath refid="run.classpath" />
</manifestclasspath>
<jar destfile="${dist.dir}/DemoApp.jar" basedir="${build.dir}">
<manifest>
<attribute name="Main-Class" value="com.my.demo.app.Main"/>
<attribute name="Class-Path" value="${manifest.classpath}"/>
</manifest>
</jar>
</target>
<target name="runjar" description="Execute jar created with dist target">
<java jar="${dist.dir}/DemoApp.jar" fork="true"/>
</target>
</project>
app.properties
build.dir=${basedir}/build
build.classes.dir=${build.dir}/classes
src.dir=${basedir}/src
lib.dir=${basedir}/lib
dist.dir=${basedir}/dist
read.from.app.properties=This is read from app.properties
谢谢。