3

I have some java 7 annotation processors (for xtend) on my class-path. Via some annotations they create java files.
This works great, in Elipse and in the Maven build.

The generated files end up in target/generated-sources/annotations as expected.
The corresponding generated .class files also end up where expected and are thus part of the final jar file.

Since I need to also include all java source files in my .jar file (there should be only one .jar file with the sources and classes) for GWT, I have specified src/main/java as a resources dir (so that Maven copies the files to the classes dir and they end up in the jar file).

the trick with the resources directory does not really work for my generated files, because Maven will first copy all resources and then start the compilation (which in turn will generate the .java files via the annotation processors).

How can I tell Maven to copy also include the generated .java files in the .jar?

TmTron
  • 17,012
  • 10
  • 94
  • 142
  • You could bind the `generate-sources` plugin to a phase prior to resources? – Anders R. Bystrup Nov 11 '13 at 11:58
  • In what location in the project structure do your generated .java files reside? – JamesB Nov 11 '13 at 12:09
  • @AndersR.Bystrup but the generate-sources plugin does produce a separate .jar called xxx-sources, right? (but maybe that's the cleaner approach anyway) @JamesB they are in `target/generated-sources/annotations` (as already mentioned?) – TmTron Nov 11 '13 at 15:24

1 Answers1

3

You can bind the maven-resources-plugin to the prepare-package phase to achieve copying annotation sources before packaging proper:

<build>
  <plugins>
    <plugin>
      <artifactId>maven-resources-plugin</artifactId>
      <executions>
        <execution>
          <id>copy-annotations</id>
          <phase>prepare-package</phase>
          <goals>
            <goal>copy-resources</goal>
          </goals>
          <configuration>
            <resources>
              <resource>
                <directory>target/generated-sources/annotations</directory>
              </resource>
            </resources>
          </configuration>
        </execution>
      </executions>
    </plugin>
   </plugins>
</build>
    ...

I hope that helps.

Cheers,

Bhuwan Prasad Upadhyay
  • 2,916
  • 1
  • 29
  • 33
Anders R. Bystrup
  • 15,729
  • 10
  • 59
  • 55
  • that's true for the generated-sources phase (where the xtend plugin will create some java files). but as mentioned above, this does already work. the problem is that the java 7 annotation processors will be run during the *compile* phase (after the xxx-resources phase). and so all .java files generated by those annotation-processors will be missing. – TmTron Nov 11 '13 at 17:39
  • Ooops! Sorry I missed that. Still, the basic idea is sound - you just need to bind `copy-resources` to a phase after compile. I have amended the answer accodingly. – Anders R. Bystrup Nov 11 '13 at 20:34
  • 2
    thanks. this works. I just had to add another configuration paramter for the output-directory like this: `${project.build.outputDirectory}` – TmTron Nov 12 '13 at 17:19