如何用 ant 构建 jar 文件?
How to build jar-file with ant?
根据训练任务,我需要用ant
创建.jar
-文件。 src
.
中普通类的拼装没有问题
但我在项目中有 logger
、libraries
给他和 .properties
文件。
因此,错误飞出:
[javac] error: package org.apache.logging.log4j does not exist
和
error: cannot find symbol
[javac] error: private static final Logger logger = LogManager.getLogger(Hello.class);
我有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>
和这个结构
如何以及在哪里收集 Java 文件中的库和 属性 到 jar 文件是否可执行?
首先,您需要将所需的库文件夹包含到 class 路径
<path id="class.path">
<fileset dir="lib">
<include name="**/*.jar" />
</fileset>
</path>
并在编译目标中添加对 javac 任务的引用
<target name="compile">
<mkdir dir="${classes}"/>
<javac srcdir="${src}" destdir="${classes}" includeAntRuntime="false">
<classpath refid="class.path" />
</javac>
</target>
之后将所有属性文件包含到 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>
我想通了。
<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>
根据训练任务,我需要用ant
创建.jar
-文件。 src
.
但我在项目中有 logger
、libraries
给他和 .properties
文件。
因此,错误飞出:
[javac] error: package org.apache.logging.log4j does not exist
和
error: cannot find symbol
[javac] error: private static final Logger logger = LogManager.getLogger(Hello.class);
我有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>
和这个结构
如何以及在哪里收集 Java 文件中的库和 属性 到 jar 文件是否可执行?
首先,您需要将所需的库文件夹包含到 class 路径
<path id="class.path">
<fileset dir="lib">
<include name="**/*.jar" />
</fileset>
</path>
并在编译目标中添加对 javac 任务的引用
<target name="compile">
<mkdir dir="${classes}"/>
<javac srcdir="${src}" destdir="${classes}" includeAntRuntime="false">
<classpath refid="class.path" />
</javac>
</target>
之后将所有属性文件包含到 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>
我想通了。
<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>