0

我正在创建我的 build.xml 文件来用 ant 构建我的 jar。

除了将外部资源作为图像加载之外,一切都很完美。

我想做的就是了解如何以正确的方式创建 jar。实际上我的代码是:

我知道问题出在包含以下内容的每一行: this.getClass().getResource("/img/logo150.png") (我对此很担心,因为我通过日志记录和调试单独测试了每一行,问题出在那)

但我不明白为什么。我尝试了数百种可能性,罐子的许多不同设置,我在 stackoverflow 中尝试了许多不同的答案,但没有解决方案奏效。

我正在使用eclipse,文件夹结构是:

src -> source code
res -> img -> images
res -> config.files.txt
db -> database files
lib -> libs

有什么想法吗?如果我运行项目,代码可以完美运行,但是当我运行 ant 构建(运行方式)时它不起作用。

编辑:哦,是的,我终于可以发布代码了!

<?xml version="1.0" encoding="UTF-8"?>
<project name="Garby Gestore Clienti" default="try_jar" basedir=".">
    <description>
    This buildfile is used to build the jar of the program.
    </description>

    <!-- ================== Property Definitions ===================== -->
    <property file="${user.home}/Garby.properties" />
    <property file="${user.home}/.Garby.properties" />
    <property file="${basedir}/Garby.properties" />

    <!-- ================= File and Directory Names ==================== -->
    <property name="src" location="${basedir}/src" />
    <property name="build" location="${basedir}/build" />
    <property name="dist" location="${basedir}/dist" />
    <property name="app.name" value="Garby" />
    <property name="dist.jarHome" value="${user.home}/Garby" />
    <property name="app.version" value="1.0" />
    <tstamp />
    <property name="jar.name" value="${app.name}_${app.version}.${DSTAMP}.jar" />
    <property name="jar.completePath" value="${dist.jarHome}/${jar.name}" />



    <!-- Here you must specify the directory in which jar files needed by this plugin to run are stored  -->

    <property name="shared.lib" value="${basedir}/lib" />

    <!-- =============== Custom Ant Task Definitions =================== -->
    <property name="compile.debug" value="true" />
    <property name="compile.deprecation" value="false" />
    <property name="compile.optimize" value="true" />

    <!-- ================== External Dependencies ======================= -->
    <property name="Database" value="hsqldb.jar" />

    <!-- ================== Compilation Classpath ======================= -->

    <path id="compile.classpath">
        <!--<pathelement location="${src.home}"/>-->
        <fileset dir="${src}">
            <include name="**/*.java" />
        </fileset>
        <fileset dir="${shared.lib}">
            <include name="**/*.jar" />
        </fileset>
    </path>

    <!-- =================== All Target ================================ -->

    <!-- ================== Try_jar Target ============================ -->
    <target name="try_jar" depends="compile, dist, signjar, clean_class_files, run" description="Clean build and dist directories, then compile, create and sign jar and finally run Core.jar" />

    <!-- ================== Clean Target ============================== -->

    <target name="clean" description="Delete old build and dist directories">
        <delete dir="${build}" />
        <delete dir="${dist}" />
    </target>

    <!-- ================== Prepare Target ============================= -->

    <target name="prepare" depends="clean">
        <mkdir dir="${build}" />
        <mkdir dir="${build}/classes" />
        <mkdir dir="${build}/lib" />
        <copy todir="${build}/lib">
            <fileset dir="${shared.lib}" includes="${Database}" />
        </copy>
    </target>

    <!-- ================== Compile Target =========================== -->

    <target name="compile" depends="prepare" description="Compile Java sources">

        <mkdir dir="${build}/classes" />
        <javac srcdir="${src}" destdir="${build}/classes" encoding="8859_1" debug="${compile.debug}" deprecation="${compile.deprecation}" optimize="${compile.optimize}" source="1.7" target="1.7">
            <classpath refid="compile.classpath" />
        </javac>
    </target>

    <!-- =================== Dist Target ================================ -->
    <target name="dist" description="Creates Jar archive">
        <!-- Create the time stamp -->
        <tstamp>
            <format property="compile.timestamp" pattern="yyyyMMddHHmm" />
        </tstamp>
        <!-- update core version in manifest -->
        <replaceregexp file="${basedir}/manifest" match="Implementation-Version: .*" replace="Implementation-Version: ${app.version}.${compile.timestamp}" />
        <!-- Create Jar file -->
        <jar destfile="${jar.completePath}" manifest="${basedir}/manifest">
            <fileset dir="${build}/classes" excludes="**/*.bak" />
            <fileset dir="${basedir}/bin"/>
            <zipfileset src="${shared.lib}/${Database}" />
        </jar>

        <copy todir="${dist.jarHome}/db">
            <fileset dir="${basedir}/db" includes="**" />
        </copy>
        <copy todir="${dist.jarHome}/res">
            <fileset dir="${basedir}/res" includes="**" />
        </copy>


    </target>

    <!-- ================== SignJar Target ============================ -->
    <target name="signjar" description="Signs jar with keystore taken from your shared.lib folder">
        <!--<signjar jar="${jar.completePath}" keystore="${shared.lib}/paripari.jks" alias="irc" storepass="${keystore.password}" keypass="${private.password}"/>-->
    </target>

    <!-- ================== Javadoc Target ============================ -->

    <target name="javadoc" depends="compile" description="Create Javadoc API documentation">

        <mkdir dir="${dist}/docs/api" />
        <javadoc sourcepath="${src}" destdir="${dist}/docs/api" packagenames="*">
            <classpath refid="compile.classpath" />
        </javadoc>

    </target>

    <!-- ================== RmBak Target ============================= -->

    <target name="rmbak" description="Delete *.bak files everywhere">

        <delete>
            <fileset dir="${docs}" includes="**/*.bak" />
            <fileset dir="${src}" includes="**/*.bak" />
        </delete>

    </target>

    <!-- ================== RmLog Target ============================= -->

    <target name="rmlog" description="Delete *.log files from build directory">

        <delete>
            <fileset dir="${build.home}" includes="**/*.log*" />
        </delete>

    </target>
    <!-- =================== Delete .class Target===================== -->
    <target name="clean_class_files" description="Delete .class files stored inside build directory and dist folder">
        <delete dir="${build}" />
        <delete dir="${dist}" />
    </target>

    <!-- ================== Run Target =============================== -->
    <target name="run" description="Run Garby">
        <java jar="${jar.completePath}" fork="true">
        </java>
    </target>

</project>

第二个问题:

encoding= 8859_1

这行不是我写的。但我的编码有问题(在罐子里我看不到欧元符号)我应该设置什么价值之王?

第三

java项目(也在eclipse中)是否有任何“正确”或“标准”文件夹结构,或者我的项目进展顺利?(还有许多不同的路径)

4

1 回答 1

2

使用这样的东西:

<copy todir="${yourJarFolder}/folder">
    <fileset dir="${basedir}/folder" includes="**" />
</copy>

此代码将您在工作区中的文件夹直接复制到 jar 的文件夹中,而不是在 jar 中,而是在同一个文件夹中。

之后,您可以使用不使用 getResource() 的标准方法访问任何资源。

希望它对你有用。

于 2012-10-16T15:35:16.270 回答