3

In a custom Maven plugin, I add a file as resource by using the addResource of MavenProject.

This works well for JAR projects, but for EARs, I see that the relevant file is copied to target/classes and then ignored. It is not present in the EAR.

There is an earSourceDirectory property which I can probably use to "trick" Maven by setting it to target/classes but it seems like the wrong way.

How can I handle generated resources that should be packed into an EAR?

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
  • Can you explain what kind of generated resources you need to add ? I suppose you don't put them into the correct locations? – khmarbaise Mar 08 '19 at 17:26
  • @khmarbaise During the build, we generate a properties file (with various build properties) inside our own Maven plugin. This file should be put into the artifact (at the top level). For that, we use `addResource`. This works for jars, but not for ears. So this is about a properties file that is generated in a Maven plugin. – J Fabian Meier Mar 08 '19 at 18:33

1 Answers1

0

The Maven EAR plugin completely disregard all of the "resources" directories that can be set up for a given artifact. Instead, as you mentioned, it solely relies on the directory referenced by the earSourceDirectory property, src/main/application being the default value. (see https://maven.apache.org/plugins/maven-ear-plugin/ear-mojo.html#earSourceDirectory)

Therefore, you have two choices: either change that property value to point to target/classes as you proposed, or generate your files (as we did for one project) under src/main/application and then they will automatically be picked up by maven-ear-plugin.

Dominique Toupin
  • 411
  • 3
  • 11
  • Generating files outside `target` during the build seems to be a bad idea. – J Fabian Meier Mar 19 '19 at 11:57
  • @JFMeier we actually generate those in the `generate-resources` phase, which is directly intended for that purpose (that is generate resources before getting them in the actual artifact) – Dominique Toupin Mar 19 '19 at 12:04
  • But when you generate resources, you do not write them to `src/main/resources`, do you? You would write them to the `target` directory (or a subdirectory thereof). – J Fabian Meier Mar 19 '19 at 12:08
  • We do generate those to `src/main/resources` and ignore those by mean of an expression in `.gitignore`; in fact I did some research and a similar case was discussed and answered here: https://stackoverflow.com/a/25245051/6131265 – Dominique Toupin Mar 19 '19 at 12:12
  • Thank you, but writing to `src/main/resources` during the build is a dangerous thing. It will not be tidied up when calling `mvn clean`, developers might try to change it by hand, it may end up in SVN accidentally etc. This is no option for me. – J Fabian Meier Mar 19 '19 at 12:17
  • Why don't you also configure the maven clean plugin as well then? https://maven.apache.org/plugins/maven-clean-plugin/clean-mojo.html#filesets – Dominique Toupin Mar 19 '19 at 12:23
  • Because I try to keep with the defaults as much as possible. Special cases and non-standard directories break expectations. – J Fabian Meier Mar 19 '19 at 12:29