【问题标题】:Filtering sources before compilation in NetBeans & Ant在 NetBeans 和 Ant 中编译前过滤源
【发布时间】:2011-07-07 11:27:03
【问题描述】:

我需要在编译之前过滤 java 文件,保持原始源不变并从过滤后的源编译(基本上,我需要设置构建日期等)。 我正在使用具有出色 Ant 构建文件的 NetBeans。

所以,有一天我发现需要在编译之前对源文件进行预处理,结果遇到了一个大问题。不,我没有立即跑到 SO,我做了一些研究,但失败了。所以,我的悲伤故事来了……

我找到了“复制”任务的“过滤器”选项,覆盖了 build-impl.xml 文件中的宏定义“j2seproject3:javac”,并在其中添加了过滤器。我得到了想要的结果,是的,但是现在我的测试不起作用,因为它们也使用了那个宏定义。

接下来,我厌倦了覆盖“-do-compile”目标,将文件复制和过滤到目录 build/temp-src,并将新源目录的参数传递给“j2seproject3:javac”:

<target depends="init,deps-jar,-pre-pre-compile,-pre-compile, -copy-persistence-xml,
        -compile-depend,-prepare-sources"
        if="have.sources" name="-do-compile">
    <j2seproject3:javac gensrcdir="${build.generated.sources.dir}" srcdir="build/temp-src"/>
    <copy todir="${build.classes.dir}">
        <fileset dir="${src.dir}" excludes="${build.classes.excludes},${excludes}" includes="${includes}"/>
    </copy>
</target>

现在 Ant 对我说,有问题的宏定义不存在!

The prefix "j2seproject3" for element "j2seproject3:javac" is not bound.

这很奇怪,因为 build-impl.xml 包含那个宏定义,并且 build-impl.xml 被导入到主构建文件中。

顺便说一下,我不能直接编辑 build-impl.xml,因为 NetBeans 会在其他构建时重写它。

所以,我的问题是:如何在 NetBeans 中编译之前自动过滤源代码,并且不中断构建过程?

【问题讨论】:

  • 我认为在源文件中设置构建日期没有意义。更常见的是将该信息添加到从构建生成的 jar 文件中的清单中。
  • @Kevin Stembridge - 是的,最好在清单中这样做。但是,例如,如果您需要预处理文件、扩展代码模板、使用代码生成器等 - 您将需要其他东西。

标签: java netbeans ant filter


【解决方案1】:

查看默认的 build.xml,它包含一条注释,内容如下(部分):

There exist several targets which are by default empty and which can be 
used for execution of your tasks. These targets are usually executed 
before and after some main targets. They are: 

  -pre-init:                 called before initialization of project properties
  -post-init:                called after initialization of project properties
  -pre-compile:              called before javac compilation
  -post-compile:             called after javac compilation
  -pre-compile-single:       called before javac compilation of single file
  -post-compile-single:      called after javac compilation of single file
  -pre-compile-test:         called before javac compilation of JUnit tests
  -post-compile-test:        called after javac compilation of JUnit tests
  -pre-compile-test-single:  called before javac compilation of single JUnit test
  -post-compile-test-single: called after javac compilation of single JUunit test
  -pre-jar:                  called before JAR building
  -post-jar:                 called after JAR building
  -post-clean:               called after cleaning build products

因此,要注入一些预编译处理,您需要为-pre-compile 提供一个定义。

FWIW,你得到的错误是因为 j2seprojectX 前缀是在 build-impl.xml 的 project 标签上定义的,而 build.xml 中的代码在该标签之外。

【讨论】:

  • 是的,我注意到了所有这些事情。但是 -pre-compile 的问题是它不会移动源目录,因此我需要损坏初始源。
【解决方案2】:

既然我找到了答案,而且似乎没有人知道答案,我将发布我的解决方案。

build.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Needed to add xmlns:j2seproject3 attribute, to be able to reference build-impl.xml macrodefs -->
<project name="Parrot" default="default" basedir="." xmlns:j2seproject3="http://www.netbeans.org/ns/j2se-project/3">
    <import file="nbproject/build-impl.xml"/>

    <target depends="init,deps-jar,-pre-pre-compile,-pre-compile, -copy-persistence-xml, -compile-depend,-prepare-sources" if="have.sources" name="-do-compile">
        <j2seproject3:javac gensrcdir="${build.generated.sources.dir}" srcdir="build/temp-src"/>
        <copy todir="${build.classes.dir}">
            <fileset dir="${src.dir}" excludes="${build.classes.excludes},${excludes}" includes="${includes}"/>
        </copy>
    </target>

    <!-- target to alter sources before compilation, you can add any preprocessing actions here -->
    <target name="-filter-sources" description="Filters sources to temp-src, setting the build date">
        <delete dir="build/temp-src" />
        <mkdir dir="build/temp-src" />
        <tstamp>
           <format property="build.time" pattern="yyyy-MM-dd HH:mm:ss"/>
        </tstamp>
        <filter token="build-time" value="${build.time}" />
        <copy todir="build/temp-src" filtering="true">
            <fileset dir="src">
                <filename name="**/*.java" />
            </fileset>
        </copy>
    </target>

</project>

【讨论】:

  • 你能告诉我你是如何编写过滤器来完成这项任务的吗?我做了一些搜索,但找不到任何样本,我想做同样的事情。
  • @KulveerSingh - -filter-sources 任务做到了。这个想法是你需要在copy任务上设置filtering=true,然后它会替换文件中的模式——在这个例子中,@build-time@被替换为当前时间。
猜你喜欢
  • 1970-01-01
  • 2012-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-20
  • 2012-07-07
相关资源
最近更新 更多