2011-03-02 90 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