首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在生成源阶段需要src/main/resources的文件用于批注处理器配置。

在生成源阶段需要src/main/resources的文件用于批注处理器配置。
EN

Stack Overflow用户
提问于 2014-09-03 13:41:31
回答 1查看 1.3K关注 0票数 2

我有一个注释处理器,我需要给它一些配置,告诉它一些关于我希望它如何生成源代码的细节。我花了很长时间试图理解为什么在构建之后文件处于目标/类中,但是在注释处理过程中我得到了一个异常,说明该文件实际上并不存在。

经过深入研究,我终于明白了为什么文件(存储在src/main/resources/config中)没有被复制到target/classes/config中,以便我的注释处理器读取-- generate-sources发生在构建生命周期中的process-resources之前,因此文件没有及时被复制,以便注释处理器在运行时看到它。(maven构建生命周期参考:http://maven.apache.org/ref/3.2.2/maven-core/lifecycles.html)

以下是我所要做的事情的高度概述:

我已经构建了一个jar,它可以处理注释,并根据注释中的信息生成接口类,从而将客户机api作为基础。其思想是,将jar作为编译时依赖项应该会自动为使用这些注释的任何项目生成此代码(在客户端项目的pom.xml中尽可能少地进行额外配置)。

我该怎么做呢?

  1. 在生成源之前获取(至少)进程资源的config.xml部分
  2. 以其他方式将该文件添加到注释处理器的类路径中(我们在输出存档中不需要此文件,因此这可能更好)
  3. 如果我没有想到更好的方法,我也可以使用其他(干净的)方法将配置信息输入注释处理器。

如果可能的话,我不希望为此编写完整的maven插件。

编辑:这里是我的客户端pom的<build>部分的相关部分,每个请求:

代码语言:javascript
复制
<build>
    <finalName>OurApp</finalName>
    <resources>
        <resource>
            <!-- My config.xml file is located here -->
            <directory>src/main/resources</directory>
        </resource>
        <resource>
            <directory>src/main/webapp</directory>
            <includes>
                <include>*.*</include>
            </includes>
            <excludes><exclude>${project.build.directory}/generated-sources/**</exclude></excludes>
        </resource>
    </resources>
    <plugins>
        <!-- Omit Annotation Processor lib from the compilation phase because the code generated is destined for another, separate jar -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven-compiler-plugin.version}</version>
            <executions>
              <execution>
                <id>annotation-processing</id>
                <phase>generate-sources</phase>
                <goals>
                  <goal>compile</goal>
                </goals>
                <configuration>
                  <proc>only</proc>
                </configuration>
              </execution>
              <!-- Compile the rest of the code in the normal compile phase -->
              <execution>
                <id>compile-without-generated-source</id>
                <phase>compile</phase>
                <goals>
                  <goal>compile</goal>
                </goals>
                <configuration>
                  <excludes><exclude>${project.build.directory}/generated-sources/**</exclude></excludes>
                  <proc>none</proc>
                  <!-- http://jira.codehaus.org/browse/MCOMPILER-230 because this doesn't
                       work in the opposite direction (setting failOnError in the other execution 
                       and leaving the default top-level value alone) -->
                  <failOnError>true</failOnError>
                </configuration>
              </execution>
            </executions>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
                <proc>only</proc>
                <failOnError>false</failOnError>
            </configuration>
        </plugin>
        <!-- package generated client into its own SOURCE jar -->
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4.1</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>generated-client-source</descriptorRef>
                </descriptorRefs>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>com.package</groupId>
                    <artifactId>our-api</artifactId>
                    <version>${our-api.version}</version>
                </dependency>
            </dependencies>
            <executions>
                <execution>
                    <id>client-source</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-08 14:59:50

因此,由于只有在编译时才需要这个文件,而且由于任何直接从类路径提取它的尝试都失败了,所以我决定将其插入项目根目录中,并在Maven中添加一个编译器参数以指向该文件。

代码语言:javascript
复制
  <compilerArgs>
      <compilerArg>-AconfigFilePath=${project.basedir}/config.xml</compilerArg>
  </compilerArgs>

不像自动将其从类路径中取出那样优雅,但仍然比将所有配置元素作为单独的属性提供更好。

谢谢你的建议

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25645944

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档