简体   繁体   English

如何用ant构建jar文件?

[英]How to build jar-file with ant?

According to the training task, I need to create .jar -file with ant .根据培训任务,我需要使用ant创建.jar文件。 There are no problems with the Assembly of ordinary classes that are in src . src的普通类的程序集没有问题。

But I have in project present logger , libraries to him and .properties file.但是我在项目中存在logger ,他的libraries.properties文件。

because of this, errors fly out:因此,错误飞出:

[javac] error: package org.apache.logging.log4j does not exist

and

error: cannot find symbol
[javac] error: private static final Logger logger = LogManager.getLogger(Hello.class);

I have build.xml我有build.xml

<?xml version="1.0"?>
<project name="Runner" default="run">
    <!-- define names of directories -->
    <property name="src" location="src"/>
    <property name="build" location="build"/>
    <property name="classes" location="${build}/classes"/>
    <!-- define all targets -->
    <target name="compile">
        <mkdir dir="${classes}"/>
        <javac srcdir="${src}" destdir="${classes}" includeAntRuntime="false"/>
    </target>
    <target name="run" depends="compile">
        <java classname="${ant.project.name}" classpath="${classes}"/>
    </target>
    <target name="clean">
        <delete dir="${build}"/>
    </target>
    <target name="package" depends="compile">
        <jar destfile="${build}/${ant.project.name}.jar" basedir="${classes}">
            <manifest>
                <attribute name="Main–Class" value="${ant.project.name}"/>
            </manifest>
        </jar>
    </target>
</project>

and this stracture和这个结构

在此处输入图片说明

How and where to gather library and property in Java file to jar-file was executable?如何以及在何处将 Java 文件中的库和属性收集到 jar 文件是可执行的?

I figured it out.我想到了。

 <path id="classpath"> <fileset dir="${lib}"> <include name="**/*.jar"/> </fileset> </path> <target name="init" depends="clean" description="starts"> <tstamp/> </target> <target name="clean" depends="package-to-jar" description="clean up"> <delete dir="${classes}"/> <delete file="${external-lib}"/> </target> <target name="package-to-jar" depends="package-external-lib" description="packing all project into a jar-file"> <jar destfile="${jar}/${ant.project.name}.jar" basedir="${classes}"> <manifest> <attribute name="Main-Class" value="${main-class}"/> </manifest> <zipfileset src="${external-lib}"/> </jar> <delete dir="${classes}"/> <delete file="${external-lib}"/> </target> <target name="package-external-lib" depends="compile" description="packing external libraries into a jar-file"> <jar destfile="${external-lib}"> <zipgroupfileset dir="${lib}"> <include name="**/*.jar"/> </zipgroupfileset> </jar> </target> <target name="compile" description="compile the source"> <mkdir dir="${classes}"/> <javac srcdir="${src}" destdir="${classes}" classpathref="classpath"/> <copy todir="${classes}"> <fileset dir="${src}" excludes="**/*.java"/> </copy> </target>

Firstly you need include required library folders to class path首先,您需要将所需的库文件夹包含到类路径中

<path id="class.path">
  <fileset dir="lib">
    <include name="**/*.jar" />
  </fileset>
</path>

and add reference to javac task in compile target并在编译目标中添加对 javac 任务的引用

<target name="compile">
        <mkdir dir="${classes}"/>
           <javac srcdir="${src}" destdir="${classes}" includeAntRuntime="false">
        <classpath refid="class.path" />
      </javac>
</target>

aftewards include all properties file to jar之后将所有属性文件包含到 jar 中

<target name="package" depends="compile">
        <jar destfile="${build}/${ant.project.name}.jar" basedir="${classes}">
        <include name="*.properties"/>
            <manifest>
                <attribute name="Main–Class" value="${ant.project.name}"/>
            </manifest>
        </jar>
</target>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM