有没有办法用 Eclipse 生成的 ant 脚本制作一个 jar?

is there a way to make a jar with the Eclipse generated ant script?

令我惊讶的是,Eclipse (Neon) 为 Java 生成的 build.xml 文件没有包含 jar 任务调用的元素。与代码生成的情况一样,我认为您必须使用它并且不进行任何编辑,以便您可以重新生成 - 或者 - 完全避免代码生成。生成的文件中的评论表明可以通过导入扩展功能来避免编辑。

<!-- WARNING: Eclipse auto-generated file.
              Any modifications will be overwritten.
              To include a user specific buildfile here, simply create one in the same
              directory with the processing instruction <?eclipse.ant.import?>
              as the first entry and export the buildfile again. -->

我想我可以在名为 export.xml 的第二个文件中使用 <?eclipse.ant.import?> 元素。在 ant 脚本中,每个构建文件应该有一个项目,所以现在有第二个项目依赖于第一个项目中的目标。

重新生成 build.xml 显示它包含预期的 "import"。

<import file="export.xml"/>

不幸的是,这不起作用。 运行 ant,我从命令行执行的操作似乎导致导出/jar 项目被忽略。

带有import元素的生成脚本(嵌套在第7行)...

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." default="build" name="ohana1">
    <property environment="env"/>
    <property name="debuglevel" value="source,lines,vars"/>
    <property name="target" value="1.8"/>
    <property name="source" value="1.8"/>
    <import file="export.xml"/>
    <path id="ohana1.classpath">
        <pathelement location="bin"/>
        <pathelement location="../export/ohana1/commons-collections-3.2.1.jar"/>
    </path>
    <target name="init">
        <mkdir dir="bin"/>
        <copy includeemptydirs="false" todir="bin">
            <fileset dir="src">
                <exclude name="**/*.launch"/>
                <exclude name="**/*.java"/>
            </fileset>
        </copy>
    </target>
    <target name="clean">
        <delete dir="bin"/>
    </target>
    <target depends="clean" name="cleanall"/>
    <target depends="build-subprojects,build-project" name="build"/>
    <target name="build-subprojects"/>
    <target depends="init" name="build-project">
        <echo message="${ant.project.name}: ${ant.file}"/>
        <javac debug="true" debuglevel="${debuglevel}" destdir="bin" includeantruntime="false" source="${source}" target="${target}">
            <src path="src"/>
            <classpath refid="ohana1.classpath"/>
        </javac>
    </target>
    <target description="Build all projects which reference this project. Useful to propagate changes." name="build-refprojects"/>
    <target description="copy Eclipse compiler jars to ant lib directory" name="init-eclipse-compiler">
        <copy todir="${ant.library.dir}">
            <fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
        </copy>
        <unzip dest="${ant.library.dir}">
            <patternset includes="jdtCompilerAdapter.jar"/>
            <fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
        </unzip>
    </target>
    <target description="compile project with Eclipse compiler" name="build-eclipse-compiler">
        <property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>
        <antcall target="build"/>
    </target>
</project>

export.xml 文件用于制作 jar...

<?eclipse.ant.import?>
<project basedir="." default="export" name="ohana1Export">
    <target depends="build,make-jar" name="export"/>
    <target name="make-jar">
        <jar destfile="../export/ohana1/${ant.project.name}.jar" basedir="bin"/>
    </target>
</project>

请注意,Eclipse Ant 编辑器抱怨此 export.xml 文件,因为名为 build 的目标是一个依赖项,在此 project/buildfile 中不存在。 build 目标在生成的 build.xml 中。该错误可能来自 "dumb" 编辑器,因此我继续执行 ant 的 运行。从命令行调用 ant 我发现没有生成 jar 文件。

我是否应该得出结论,如果您需要导出 .jar 文件,Eclipse 的 ant 脚本生成器是无用的,并且人类应该维护满足所有要求的 ant 脚本?

是的,在我看来导出的 build.xml 是无用的,对于 Eclipse Neon,如果打算制作 .jar.

具体执行以下操作。

  • 手动编写导出 .jar 的简单 ant 脚本。此 post 底部的 link 逐字记录了脚本的外观。您可以通过 New > Other > XML > XML File 使用 built-in Xml Editor 来创建这个可能被称为 makeJar.xml 的新文件并保存它。如果 Package Explorer 中显示的图标仍然是普通的 XML 文件图标,则刷新项目可能会将图标更改为 Ant 文件图标。将来,您可以使用 Open With 来获取 Ant Editor 而不是 XML Editor。该脚本将取代用户通过 Eclipse 执行的手动导出 .jar
  • 可以将此脚本添加到 Project > Properties > Builders。它将在 Builders 列表中排在第二位。 Builders 列表中的第一个是应该已经存在的 Java Builder。当调用 Eclipse 构建时,整个构建器列表将按照构建器列表中显示的顺序进行处理。因此,不仅会生成 .class 个文件,还会生成 .jar 个文件。

由于现在集成了 .class 代和 .jar 代,因此实现了更高的自动化,这可以说是在失败的生成尝试中使用导出的 build.xml .jar.

这是 Project > Properties > Builders 处的对话框,您可以使用它来创建新的生成器。 Select New 然后 select Ant Builder。我给新的Builder取了名字makeJar

这是新 Ant 构建器的对话框,您可以通过它浏览到 buildfile,这是您手动编写的 Ant 脚本,用于创建 .jar文件。在此示例中,脚本为 makeJar.xml。它还允许您浏览到脚本为 运行.

时要使用的 base directory

设置新生成器后,项目 "clean" 或项目 "build" 将创建 .class 个文件以及 .jar.


Eclipse 关于此主题的文档位于 link。请注意,似乎不可能 link 包含说明的确切页面,因此您必须向下浏览文档树至有关 "Ant buildfiles as project builders".

的部分

Link to Eclipse and Ant