我有一个注释处理器,我需要提供一些配置来告诉它一些我希望它如何生成源代码的细节。我花了很多时间试图理解为什么在构建后文件位于目标/类中,但在注释处理过程中我遇到了一个异常,指出文件实际上不存在。
经过大量的挖掘,我终于弄明白为什么文件(存储在
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中尽可能少地进行额外配置)。
我该怎么做:
-
在生成源之前获取(至少)进程资源的config.xml部分
-
以其他方式将该文件添加到注释处理器的类路径中(我们不需要在输出存档中使用该文件,因此这可能更好)
-
如果有我没有想到的更好的方法,我也愿意使用其他(干净的)方法将配置信息输入注释处理器
如果可能的话,我宁愿不写一个完整的maven插件。
编辑:
以下是
<build>
每个请求的客户端pom部分:
<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>