2011-03-02 95 views
0

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

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

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

接下来,我累了压倒一切 “-DO编译” 目标,复制&过滤文件目录编译/ 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> 

现在蚂蚁对我说,这个macrodef并不存在!

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

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

而且,顺便说一句,我不能直接编辑build-impl.xml,因为NetBeans会在每个其他版本上重写它。

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

+1

我不认为在源文件中设置生成日期是有意义的。将该信息添加到从构建生成的jar文件中的清单更为常见。 – 2011-03-03 08:07:10

+0

@凯文斯特布里奇 - 是的,最好在清单中这样做。但是,如果您需要预处理文件,扩展代码模板,使用代码生成器等 - 您还需要别的东西。 – Rogach 2011-03-03 16:53:49

回答

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中的代码不在该标记中。

+0

是的,我注意到了所有这些东西。但是-pre-compile的问题在于它不会移动源目录,所以我需要损坏初始源代码。 – Rogach 2011-03-03 21:53:18

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> 
+0

你能告诉我你是如何编写过滤器来完成这项任务的?我做了一些搜索,但没有找到任何样本,我想做同样的事情。 – 2013-03-04 14:04:08

+0

@KulveerSingh - '-filter-sources'任务。这个想法是,你需要在'copy'任务中设置'filtering = true',然后替换文件中的模式 - 在这个例子中,'@ build-time @'被当前时间替换。 – Rogach 2013-03-04 16:32:16