生成清单类路径 在蚂蚁

发布于 2024-07-19 07:54:07 字数 1745 浏览 3 评论 0原文

在下面的构建文件中,jar 目标引用清单类路径的 jar.class.path 属性。 编译目标指的是project.class.path

这里有冗余,因为jar.class.path和project.class.path非常相似。 添加库时必须更新它们,如果库列表变得很长,这可能会很痛苦。 有没有更好的办法? 任何解决方案都必须是跨平台的,并且始终使用相对路径。

编辑:
它应该从文件集生成 JAR 类路径,而不是相反,因此我可以使用通配符来包含目录中的所有 JAR 文件。

<?xml version="1.0"?>
<project name="Higgins" default="jar" basedir=".">

    <property name="jar.class.path" value="lib/forms-1.2.0.jar lib/BrowserLauncher.jar"/>

    <path id="project.class.path">
      <pathelement location="build"/>
      <fileset dir="lib">
        <include name="forms-1.2.0.jar"/>
        <include name="BrowserLauncher.jar"/>
      </fileset>
    </path>

    <target name="prepare">
        <mkdir dir="build"/>
    </target>

    <target name="compile" depends="prepare" description="Compile core sources">
        <javac srcdir="src"
               includes="**"
               destdir="build"
               debug="true"
               source="1.5">
          <classpath refid="project.class.path"/>
        </javac>
    </target>

    <target name="jar" depends="compile" description="Generates executable jar file">
        <jar jarfile="higgins.jar">
            <manifest>
                <attribute name="Main-Class" value="nl.helixsoft.higgins.Main"/>
                <attribute name="Class-Path" value="${jar.class.path}"/>
            </manifest>
            <fileset dir="build" includes="**/*.class"/>            
            <fileset dir="src" includes="**/*.properties"/>         
        </jar>
    </target>

</project>

In the build file below, the jar target refers to the jar.class.path property for the manifest class-path. The compile target refers to project.class.path

There is redundancy here, because jar.class.path and project.class.path are very similar. They must be both updated when libraries are added, which can be a pain if the list of libraries gets very long. Is there a better way? Any solution must be cross-platform and always use relative paths.

Edit:
It should generate the JAR classpath from a fileset and not the other way around, so I can use wildcards to e.g. include all JAR files in a directory.

<?xml version="1.0"?>
<project name="Higgins" default="jar" basedir=".">

    <property name="jar.class.path" value="lib/forms-1.2.0.jar lib/BrowserLauncher.jar"/>

    <path id="project.class.path">
      <pathelement location="build"/>
      <fileset dir="lib">
        <include name="forms-1.2.0.jar"/>
        <include name="BrowserLauncher.jar"/>
      </fileset>
    </path>

    <target name="prepare">
        <mkdir dir="build"/>
    </target>

    <target name="compile" depends="prepare" description="Compile core sources">
        <javac srcdir="src"
               includes="**"
               destdir="build"
               debug="true"
               source="1.5">
          <classpath refid="project.class.path"/>
        </javac>
    </target>

    <target name="jar" depends="compile" description="Generates executable jar file">
        <jar jarfile="higgins.jar">
            <manifest>
                <attribute name="Main-Class" value="nl.helixsoft.higgins.Main"/>
                <attribute name="Class-Path" value="${jar.class.path}"/>
            </manifest>
            <fileset dir="build" includes="**/*.class"/>            
            <fileset dir="src" includes="**/*.properties"/>         
        </jar>
    </target>

</project>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

娇女薄笑 2024-07-26 07:54:07
<path id="build.classpath">
  <fileset dir="${basedir}">
     <include name="lib/*.jar"/>
  </fileset>
</path>

<pathconvert property="manifest.classpath" pathsep=" ">
  <path refid="build.classpath"/>
  <mapper>
    <chainedmapper>
       <flattenmapper/>
       <globmapper from="*.jar" to="lib/*.jar"/>
    </chainedmapper>
  </mapper>
</pathconvert>

<target depends="compile" name="buildjar">
  <jar jarfile="${basedir}/${test.jar}">
     <fileset dir="${build}" />
     <manifest>
       <attribute name="Main-Class" value="com.mycompany.TestMain"/>
       <attribute name="Class-Path" value="${manifest.classpath}"/>
     </manifest>
 </jar>
</target>

有关更多信息,请查看本文

<path id="build.classpath">
  <fileset dir="${basedir}">
     <include name="lib/*.jar"/>
  </fileset>
</path>

<pathconvert property="manifest.classpath" pathsep=" ">
  <path refid="build.classpath"/>
  <mapper>
    <chainedmapper>
       <flattenmapper/>
       <globmapper from="*.jar" to="lib/*.jar"/>
    </chainedmapper>
  </mapper>
</pathconvert>

<target depends="compile" name="buildjar">
  <jar jarfile="${basedir}/${test.jar}">
     <fileset dir="${build}" />
     <manifest>
       <attribute name="Main-Class" value="com.mycompany.TestMain"/>
       <attribute name="Class-Path" value="${manifest.classpath}"/>
     </manifest>
 </jar>
</target>

For further information check out this article.

殊姿 2024-07-26 07:54:07

假设 Ant 1.7 或更高版本,您可以使用 manifestclasspath 任务。

<path id="dep.runtime">
    <fileset dir="./lib">
        <include name="**/*.jar" />
    </fileset>
</path>
<property name="dep_cp" value="${toString:dep.runtime}" />

<target name="default">
    <manifestclasspath property="manifest_cp" jarfile="myjar.jar">
        <classpath refid="dep.runtime" />
    </manifestclasspath>
    <echo message="Build Classpath: ${dep_cp}" />
    <echo message="Manifest Classpath: ${manifest_cp}" />
</target>

Assuming Ant 1.7 or above, you can use the manifestclasspath task.

<path id="dep.runtime">
    <fileset dir="./lib">
        <include name="**/*.jar" />
    </fileset>
</path>
<property name="dep_cp" value="${toString:dep.runtime}" />

<target name="default">
    <manifestclasspath property="manifest_cp" jarfile="myjar.jar">
        <classpath refid="dep.runtime" />
    </manifestclasspath>
    <echo message="Build Classpath: ${dep_cp}" />
    <echo message="Manifest Classpath: ${manifest_cp}" />
</target>
_畞蕅 2024-07-26 07:54:07

如果您只想在两个(或更多)路径之间共享一个公共子路径,这很容易做到:

<path id="lib.path>
    <fileset dir="lib">
        <include name="forms-1.2.0.jar"/>
        <include name="BrowserLauncher.jar"/>
    </fileset>
</path>

<path id="project.class.path">
    <pathelement location="build"/>
    <path refid="lib.path"/>
</path>

<property name="jar.class.path" refid="lib.path"/>

编辑 抱歉,我误解了这个问题。 尝试这个:

<property name="jar.class.path" value="lib/forms-1.2.0.jar lib/BrowserLauncher.jar"/>

<path id="project.class.path">
    <pathelement location="build"/>
    <fileset dir="." includes="${jar.class.path}"/>
</path>

If you just want a common subpath shared between two (or more) paths, that is easy to do:

<path id="lib.path>
    <fileset dir="lib">
        <include name="forms-1.2.0.jar"/>
        <include name="BrowserLauncher.jar"/>
    </fileset>
</path>

<path id="project.class.path">
    <pathelement location="build"/>
    <path refid="lib.path"/>
</path>

<property name="jar.class.path" refid="lib.path"/>

EDIT Sorry, I misunderstood the question. Try this:

<property name="jar.class.path" value="lib/forms-1.2.0.jar lib/BrowserLauncher.jar"/>

<path id="project.class.path">
    <pathelement location="build"/>
    <fileset dir="." includes="${jar.class.path}"/>
</path>
后知后觉 2024-07-26 07:54:07

您可以使用; 将路径(可以包含文件集)转换为纯字符串。 您可能需要将该字符串复制到文件中,使用; 或砍掉前导路径位,然后最后使用; 将处理后的字符串加载到最终属性中。

实施留给读者作为练习。

You can use <pathconvert> to convert a path (which can contain a fileset) into a plain string. You'll likely need to <echo> that string to a file, use either <replace> or <replaceregexp> to chop the leading path bits, then finally use <loadfile> to load the manipulated string into the final property.

Implementation left as an exercise to the reader.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文