在NetBeans&amp ;;编译之前过滤源代码。蚂蚁

时间:2011-03-02 17:43:16

标签: java netbeans ant filter

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

所以,有一天我发现需要在编译之前预处理我的源文件,并遇到了一个大问题。不,我没有立刻跑到SO,我做了一些研究,但失败了。所以,这是我悲伤的故事...

我找到了“复制”任务的“过滤器”选项,在build-impl.xml文件中覆盖了macrodef“j2seproject3:javac”,并在其中间添加了过滤器。我得到了理想的结果,是的,但现在我的测试无法正常工作,因为他们也使用了macrodef。

接下来,我厌倦了覆盖“-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告诉我,有问题的macrodef不存在!

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

这很奇怪,因为build-impl.xml包含macrodef,而build-impl.xml被导入到主构建文件中。

顺便说一句,我无法直接编辑build-impl.xml,因为NetBeans会在其他所有版本上重写它。

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

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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中的代码是在该标记之外。