I posted this question on StackOverflow but I’m stuck and haven’t been getting 
much response so I decided to go to the “source” as it were.
Here’s a link to the question on SO, if you prefer to answer there (helpful 
answers will get up-voted, best answer accepted):
http://stackoverflow.com/questions/25645944/need-file-from-src-main-resources-in-generate-sources-phase-for-annotation-proce

The question:
I have an annotation processor that I need to give some configuration to tell 
it a few details about how I want it to generate source code.  I spent a good 
deal of time trying to understand why the file was sitting in target/classes 
after the build, but I was getting an exception during annotation processing 
stating that the file did not, in fact, exist.

After much digging I finally figured out why the file (stored in 
`src/main/resources/config`) isn't getting copied over to 
`target/classes/config` for my annotation processor to read - 
`generate-sources` happens before `process-resources` in the build lifecycle, 
so the file isn't getting copied over in time for the annotation processor to 
see it during its run. (maven build lifecycle reference: 
http://maven.apache.org/ref/3.2.2/maven-core/lifecycles.html)

Here's a high level overview of what I'm trying to do:

I have a jar that I've built that processes annotations and generates interface 
classes from the information in the annotations to base a client api off of.  
The idea is that including that jar as a compile-time dependency should 
automatically generate this code for any project that uses these annotations 
(with as little-as-possible additional configuration in the client project's 
pom.xml).

How do I go about either:

 1. Getting (at a minimum) the config.xml portion of process-resources to 
happen before generate-sources
 2. Adding the file to the classpath of the annotation processor in some other 
way (we don't need this file in the output archive, so this might be better)

I would prefer not to write a whole maven plugin for this if possible.

Here are the relevant parts of the `<build>` portion of my client 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>


Brian Trezise | Software Engineer
phone & fax +1.317.957.1806 | mobile +1.303.809.9782 | [email protected]

Interactive Intelligence
Deliberately Innovative
www.inin.com<http://www.inin.com/>

Reply via email to