使用 Ant 进行 Java 6 注解处理配置

发布于 2024-09-17 01:26:46 字数 152 浏览 11 评论 0原文

我有一个自定义注释及其处理器和处理器工厂。如何配置我的 Ant 构建文件,以便:

  1. 注释处理器应用于带注释的类并在“gen”文件夹内生成源文件

  2. 生成的源文件(来自注释处理)可以被项目中的其他源文件使用。

I have a custom annotation and it's processor & processorFactory. How do I configure my Ant build file such that:

  1. The annotation processor is applied on annotated classes and generates source files inside "gen" folder

  2. The generated source files(from annotation processing) could be used by other source files in project.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

帥小哥 2024-09-24 01:26:46

这并不漂亮,但这就是我所做的。 (来源 javac ant 任务 javac 手册页) 使用compilerarg 属性我可以传入与注释处理相关的参数,这些参数不是直接的由 javac ant 任务支持。

<javac srcdir="${src}" destdir="${classes}" ... > 
     ....
     <compilerarg line="-processorpath ${processorpath}"/>
     <compilerarg line="-processor ${processor}"/>
     <compilerarg line="-s ${whereToPutGeneratedClassFiles}"/>
</javac>

我不使用 APT 工具,因为文档指出

请注意,Apt 工具似乎确实是 JDK 框架的不稳定部分,因此在未来版本中可能会发生根本性变化。特别是它在 JDK 6 中可能已过时,JDK 6 可以将注释处理器作为 javac 的一部分运行。

如果你真的不关心编译器参数,你可以像这样打包你的注释处理器

<jar destfile="${annotationprocessorjar}" ... >
     ...
     <service type="javax.annotation.processing.Processor" provider="${your.annotation.processor.fully.qualified.name}"/>
</jar>

然后你可以这样做

 <javac ... make sure ${annotationprocessorjar} is in classpath>
 </javac>

This is not pretty, but it is what I do. (Sources javac ant task javac man page) Using the compilerarg attribute I can pass in the annotation processing related arguments that are not directly supported by the javac ant task.

<javac srcdir="${src}" destdir="${classes}" ... > 
     ....
     <compilerarg line="-processorpath ${processorpath}"/>
     <compilerarg line="-processor ${processor}"/>
     <compilerarg line="-s ${whereToPutGeneratedClassFiles}"/>
</javac>

I do not use the APT tool because the documentation states

Be advised that the Apt tool does appear to be an unstable part of the JDK framework, so may change radically in future versions. In particular it is likely to be obsolete in JDK 6, which can run annotation processors as part of javac.

If you really don't care for compiler args, you can jar your annotation processors like this

<jar destfile="${annotationprocessorjar}" ... >
     ...
     <service type="javax.annotation.processing.Processor" provider="${your.annotation.processor.fully.qualified.name}"/>
</jar>

Then you can do

 <javac ... make sure ${annotationprocessorjar} is in classpath>
 </javac>
北恋 2024-09-24 01:26:46

我发现其他一些示例有点令人困惑,因为某些关键位是无法解释的变量。这就是我最终得到的结果:

构建处理器 jar:

<target name="build-aux">
    <mkdir dir="${build.dir}" />
    <mkdir dir="${build.classes}" />
    <javac destdir="${build.classes}" source="1.6" target="1.6">
        <src path="${src.java}" />
        <include name="com/acme/cli/Program.java" />
        <include name="com/acme/cli/ProgramProcessor.java" />
    </javac>

    <jar jarfile="acme-aux.jar" update="true">
        <manifest>
            <attribute name="Main-Class" value="${main.class}" />
            <attribute name="Implementation-Title" value="acme-aux" />
            <attribute name="Implementation-Version" value="${version}" />
            <attribute name="Implementation-Vendor" value="ACME, Inc" />
            <attribute name="Built-By" value="${user.name}" />
            <attribute name="Build-Date" value="${TODAY}" />
        </manifest>
        <fileset dir="${build.classes}">
            <!-- the annotation -->
            <include name="com/acme/cli/Program.class" />
            <!-- the annotation processor -->
            <include name="com/acme/cli/ProgramProcessor.class" />
        </fileset>
        <service type="javax.annotation.processing.Processor"
            provider="com.acme.cli.ProgramProcessor" />
    </jar>
</target>

然后编译代码并运行处理器:

<target name="compile" depends="generate,build-aux">
    <mkdir dir="${build.dir}" />
    <mkdir dir="${build.classes}" />
    <javac destdir="${build.classes}" source="1.6" target="1.6">
        <src path="${src.java}" />
        <include name="com/acme/**/*.java" />
        <!-- ensure that "acme-aux.jar" is in this classpath -->
        <classpath refid="compile.class.path"/>
         <!-- pass option to annotation processor -->
        <compilerarg value="-Aacme.version=${version}" />
    </javac>
</target>

I found some of the other examples slightly confusing due to some of the key bits being unexplained variables. Here's what I ended up with:

to build the processor jar:

<target name="build-aux">
    <mkdir dir="${build.dir}" />
    <mkdir dir="${build.classes}" />
    <javac destdir="${build.classes}" source="1.6" target="1.6">
        <src path="${src.java}" />
        <include name="com/acme/cli/Program.java" />
        <include name="com/acme/cli/ProgramProcessor.java" />
    </javac>

    <jar jarfile="acme-aux.jar" update="true">
        <manifest>
            <attribute name="Main-Class" value="${main.class}" />
            <attribute name="Implementation-Title" value="acme-aux" />
            <attribute name="Implementation-Version" value="${version}" />
            <attribute name="Implementation-Vendor" value="ACME, Inc" />
            <attribute name="Built-By" value="${user.name}" />
            <attribute name="Build-Date" value="${TODAY}" />
        </manifest>
        <fileset dir="${build.classes}">
            <!-- the annotation -->
            <include name="com/acme/cli/Program.class" />
            <!-- the annotation processor -->
            <include name="com/acme/cli/ProgramProcessor.class" />
        </fileset>
        <service type="javax.annotation.processing.Processor"
            provider="com.acme.cli.ProgramProcessor" />
    </jar>
</target>

then to compile the code and run the processor:

<target name="compile" depends="generate,build-aux">
    <mkdir dir="${build.dir}" />
    <mkdir dir="${build.classes}" />
    <javac destdir="${build.classes}" source="1.6" target="1.6">
        <src path="${src.java}" />
        <include name="com/acme/**/*.java" />
        <!-- ensure that "acme-aux.jar" is in this classpath -->
        <classpath refid="compile.class.path"/>
         <!-- pass option to annotation processor -->
        <compilerarg value="-Aacme.version=${version}" />
    </javac>
</target>
一片旧的回忆 2024-09-24 01:26:46

以下是我在 eclipse/ant 中的做法:

<javac destdir="bin"
  debug="true"
  debuglevel="${debuglevel}"
  compiler="javac1.6"
  srcdir="${src}">
       <include name="**/*.java"/> <!-- I just do it this way -->
 <classpath refid="classpath_ref_id"/>
 <compilerarg value="-processor" />
 <compilerarg value="${processor}" />
 <compilerarg value="-s" />
 <compilerarg value="${gen_src_target}" />
</javac>

注释

  • 处理器路径包含在 *classpath_ref_id* 中
  • 之前运行您的处理器
    编译实际代码(使用或
    没有生成的代码)。

Here's how I did it in eclipse/ant:

<javac destdir="bin"
  debug="true"
  debuglevel="${debuglevel}"
  compiler="javac1.6"
  srcdir="${src}">
       <include name="**/*.java"/> <!-- I just do it this way -->
 <classpath refid="classpath_ref_id"/>
 <compilerarg value="-processor" />
 <compilerarg value="${processor}" />
 <compilerarg value="-s" />
 <compilerarg value="${gen_src_target}" />
</javac>

Notes

  • The processor path is included in the *classpath_ref_id*
  • Run your processor before you
    compile the actual code (with or
    without the generated code).
各空 2024-09-24 01:26:46

你可以看看注释处理工具
,它会自动编译生成的源文件

//编辑//
回复您的评论:

您可以将 apt 与 apt ant 任务结合使用

但从 jdk6 开始,javac 工具提供 直接支持注释处理,因此您应该能够使用 javac ant 任务 编译器属性指定为“javac1.6”

you can take a look at the annotation processing tool
, it automatically compiles the generated sourcefiles

//EDIT//
In reply to your comment:

You can use apt in combination with the apt ant task

But as of jdk6 the javac tool provides direct support for annotation processing, so you should be able to use the javac ant task with the compiler attribute specified as "javac1.6"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文