简体   繁体   English

使用Eclipse中的Ant将在运行时生成的文件夹添加为源文件夹

[英]Adding a folder generated at runtime as source folder using Ant in Eclipse

I am using the following bunch of softwares: 我正在使用以下软件:

  1. Ant 1.7.1 蚂蚁1.7.1
  2. Eclipse Helios 日蚀赫利俄斯
  3. Java 1.6 Java 1.6
  4. Apache thrift 0.8.0 Apache节俭0.8.0

I am using Thrift to generate java source code in Ant using the following code block: 我正在使用Thrift使用以下代码块在Ant中生成Java源代码:

<exec executable="thrift-0.8.0.exe" osfamily="windows">
    <arg value="-out" />
    <arg value="java/src" />
    <arg value="--gen" />
    <arg value="java" />
    <arg file="Sample.thrift" />
</exec>

So, now I want to add the generated src folder automatically to the classpath of the project in Eclipse using Ant so that it is shown as a source folder when I open the project in Eclipse. 因此,现在我想in Eclipse using Ant将生成的src文件夹自动addin Eclipse using Ant中的项目的classpath中,以便在Eclipse中打开项目时将其shown as a source folder

NOTE: I understand that the classpath for a project is present in the .classpath file and adding a classpathentry to it would solve my issue. 注意:我了解项目的类路径存在于.classpath文件中,并且向其adding a classpathentry .classpath将解决我的问题。 But I want Eclipse to do that instead of me doing it in the .classpath file manually. 但是我希望Eclipse代替我在.classpath文件中手动执行此操作。

Further, I also had a look at the Ant-Eclipse project and found an Ant task to create a new Eclipse Java project and display its src folder as a source folder. 此外,我还查看了Ant-Eclipse项目 ,发现了一个Ant任务来创建一个新的Eclipse Java项目并将其src文件夹显示为源文件夹。 But I don't want a dependency on an external library. 但是我不想依赖外部库。

<project name="test" default="eclipse" basedir=".">
    <target name="eclipse">
        <taskdef name="eclipse" classname="prantl.ant.eclipse.EclipseTask" />
        <eclipse>
            <project />
            <classpath>
                <source path="src" />
                <output path="bin" />
            </classpath>
        </eclipse>
    </target>
</project>

Is there a way to do this in the already existing Ant library in Eclipse? 有没有办法在Eclipse中已经存在的Ant库中执行此操作?

Thanks a lot! 非常感谢!

The eclipse .classpath file is an XML document, so adding a source folder in there involves editing XML. eclipse .classpath文件是XML文档,因此在其中添加源文件夹涉及编辑XML。 Unfortunately, Ant doesn't have any built-in facilities for manipulating XML in any meaningful way. 不幸的是,Ant没有任何内置的功能来以任何有意义的方式操作XML。 I've used xmltask for editing .classpath and .project files myself, but that's an external library you said you don't really want. 我已经使用xmltask自己编辑.classpath和.project文件,但这是您说您并不需要的外部库。

All is not lost, however - Ant supports running scripts , including Groovy, which treats XML as a first-class citizen . 但是,一切都不会丢失-Ant支持运行的脚本 ,包括Groovy,该 脚本 将XML视为一流的公民 I wish I could say I had enough experience with Groovy to give you more than some links, but what you're after looks very doable with some calls to appendNode() . 我希望我可以说我对Groovy有足够的经验,可以为您提供更多的链接,但是您可以通过对appendNode()一些调用来appendNode()

If you really don't want to depend on anything external then you could consider using an XSLT stylesheet to edit the .classpath file (which is XML). 如果您真的不想依赖任何外部的东西,则可以考虑使用XSLT样式表来编辑.classpath文件(XML)。

add-dir.xsl 附加dir.xsl

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:param name="pathToAdd" />

  <xsl:strip-space elements="*" />
  <xsl:output method="xml" indent="yes" />

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()" />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="classpath">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()" />
      <!-- don't add a 2nd copy of the classpathentry if one already exists -->
      <xsl:if test="not(classpathentry[@kind = 'src'][@path = $pathToAdd])">
        <classpathentry kind="src" path="{$pathToAdd}" />
      </xsl:if>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

build.xml build.xml文件

<xslt in=".classpath" out=".classpath.edited" style="add-dir.xsl">
  <param name="pathToAdd" expression="java/src" />
</xslt>
<move file=".classpath.edited" tofile=".classpath" overwrite="yes" />

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

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