开发者

Filtering sources before compilation in NetBeans & Ant

开发者 https://www.devze.com 2023-02-14 15:17 出处:网络
I need to filter java files before compilation, leaving the original sources unchanged and compiling from filtered ones (basically, I need to set build date and such).

I need to filter java files before compilation, leaving the original sources unchanged and compiling from filtered ones (basically, I need to set build date and such). I'm using NetBeans with its great Ant build-files.

So, one day I discovered the need to pre-process my source files before compilation, and ran into a big problem. No, I did not run to SO at once, I did some research, but failed. So, here comes my sad story...

I found the "filter" option of "copy" task, overrided macrodef "j2seproject3:javac" i开发者_C百科n build-impl.xml file and added filter in the middle of it. I got the desired result, yes, but now my tests are not working, since they use that macrodef too.

Next, I tired to overriding "-do-compile" target, copying&filtering files to directory build/temp-src, and passing an argument of new source directory to "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>

And now Ant says to me, that macrodef in question does not exist!

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

That's strange, since build-impl.xml contains that macrodef, and build-impl.xml is imported into main build file.

And, by the way, I cannot edit build-impl.xml directly, since NetBeans rewrites it on every other build.

So, my question is: how can I automatically filter sources before compiling in NetBeans, and do not break the build process?


Looking at the default build.xml, it contains a comment that reads (in part):

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

So, to inject some pre-compile processing, you would provide a definition for -pre-compile.

FWIW, the error you got is because the j2seprojectX prefix is defined on the project tag of build-impl.xml, and the code in build.xml is outside of that tag.


Since I found an answer, and because it seems that nobody knows the answer, I'll post my solution.

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号