我有一个注释处理器,我需要给它一些配置,告诉它一些关于我希望它如何生成源代码的细节。我花了很长时间试图理解为什么在构建之后文件处于目标/类中,但是在注释处理过程中我得到了一个异常,说明该文件实际上并不存在。
经过深入研究,我终于明白了为什么文件(存储在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中尽可能少地进行额外配置)。
我该怎么做呢?
如果可能的话,我不希望为此编写完整的maven插件。
编辑:这里是我的客户端pom的<build>部分的相关部分,每个请求:
<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>发布于 2014-09-08 14:59:50
因此,由于只有在编译时才需要这个文件,而且由于任何直接从类路径提取它的尝试都失败了,所以我决定将其插入项目根目录中,并在Maven中添加一个编译器参数以指向该文件。
<compilerArgs>
<compilerArg>-AconfigFilePath=${project.basedir}/config.xml</compilerArg>
</compilerArgs>不像自动将其从类路径中取出那样优雅,但仍然比将所有配置元素作为单独的属性提供更好。
谢谢你的建议
https://stackoverflow.com/questions/25645944
复制相似问题