使用 Ant 的 Java 6 注释处理配置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3644069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:48:28  来源:igfitidea点击:

Java 6 annotation processing configuration with Ant

javaantbuildannotationsapt

提问by user18943

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

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

  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.

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

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

采纳答案by emory

This is not pretty, but it is what I do. (Sources javac ant taskjavac 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 ant 任务javac 手册页) 使用 compilerarg 属性,我可以传入 javac ant 任务不直接支持的注释处理相关参数。

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

I do not use the APT toolbecause the documentation states

我不使用APT 工具,因为文档说明

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.

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

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

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

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

回答by Brad Mace

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:

构建处理器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>

回答by kranasian

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

这是我在 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 beforeyou compile the actual code (with or without the generated code).
  • 处理器路径包含在 *classpath_ref_id* 中
  • 编译实际代码之前运行你的处理器(有或没有生成的代码)。

回答by Davy Meers

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

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

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

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