Maven generated source folder document - maven

I saw lots of code generation plugins in Maven put generated code in target/generated-source/*plugin-name* directory. But I didn't find it documented anywhere in Maven standard directory layout. http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html
Where is it documented in Maven? Or it's just a convention that everyone follows?

This is not described as part of the standard directory layout, since these files are generated( hence, they end up in the target/ directory). By the target/generated-source/plugin-name convention the plugin-writer is pretty sure it won't clash with generated sources of other plugins. The plugin-writer is also responsible for binding this source folder to the Maven project.
As a user you shouldn't notice much of it.

Related

maven archetype generation with custom root folder name

I am using mvn archetype:generate and I am supplying as input parameters the artifactId as well as the repositoryName that I want to use for git. I would like that the root folder name of the project generated is the repositoryName and not the artifactId, which is the default behavior.
According to the documentation, you can create a post generation groovy script that does different actions and moving the project to a new folder is also possible, as mentioned here. This solution worked out for me. However, right after the output of my script, there is the default log line that says
[INFO] Project created from Archetype in dir: <old_dir>
This is very confusing and I'm wondering if there is a way to change that. Maybe changing the request.outputDirectory would provide a better solution, but I didn't manage to get that to work and I am also wondering what is a good structural solution.

Maven war plugin copy arbitrary files

I apologize that this is surely basic maven/war plugin stuff, but I'm totally not a maven user. I just have to hack this one thing into a project that is maven based.
What I need to do is to copy an (essentially arbitrary) directory and files into the root of my war. I need them to appear as resources available via HTTP when the war deploys.
I cannot simply put them in the "right place" in the source tree. This is because the files in question are actually the source files of my project. I realize this is a bit odd, but this is a documentation project, and I need to show the source and the effect all in the same war.
So, in the general case, how would I configure the maven war plugin to copy a given directory, along with it's contents, to the root of my war? (BTW, I have tried to make sense of the documentation of this tool, but it seems to be predicated on so much understanding of maven that it feels like I'll never understand it without learning maven first, and I'm a bit too pressed for time to do that just now!)
Many TIA
Toby.
You could try:
use the copy-resources plugin. Use the plugin to copy your source files under target prior to when the war is packaged.
or, configure the maven-war-plugin to include additional resources.
Thanks Drew, that got me where I needed to go. Bottom line, I added the sample pom fragment from the link you gave for copy-resources to my pom.xml with the following changes:
<outputDirectory>target/${project.name}-${project.version}/sources ...
<directory>src/main/java ...
I copied this from the link you gave, then edited the element to point at src/main/java, which picked up my files, and the outputDirectory to the aggregate target/${project.name}-${project.version}/sources . I found that ${project.name} mapped to the project, and the version came from ${project.version}, which seemed to resolve the last little bits of issue.
Thanks again.
Toby

Custom Maven Plugin: Programmatically Adding Source Directory to Project

As a Maven end-user, it is simple to add an additional directory to the list of source directories that will be compiled during the "compile" phase. I would use the build-helper-maven-plugin approach.
However, in my own custom plugin I would like to do this programmatically. My plugin will generate some java code. I would subsequently like to add the output directory (containing generated .java files) to the list of source paths.
At the moment I’m manually having to set the build-helper-maven-plugin config in all of my POMs to get the files I’m generating to be compiled.
Any pointers on what part of the Maven API to be looking at? My searches have only yielded queries from end-users, which are solved with the build-helper-maven-plugin approach.
To find my answer I took a look at the source code for the ANTLR maven plugin, which I know adds sources to the path. See AbstractAntlrMojo.
The solution is to add a MavenProject member variable to your Mojo with an expression to bind it to the project:
#Parameter(defaultValue="${project}")
private MavenProject project;
Once one has a reference to the project, it's a simple method invocation:
project.addCompileSourceRoot("<DIRECTORY-PATH-HERE>");
This will ensure that the new directories housing generated code will be compiled.

Where in the Maven Standard Directory Layout should generated resources go?

I'd like to generate some resources (JavaHelp search index in this case), but I can't seen to see where those generate files should go to get into the jar. I put them in target/generated-sources, but they are ignored. Should it be target/classes?
/generated-sources directory is used by various tools generating sources (duh!), like xjc or wsdl2java. This directory is later included in the compilation phase.
/target/classes is everything that should be included in the final JAR, which answers your question. Also the contents of /src/main/resources is included, but this directory is typically part of version control and is not meant for generated artifacts.
It turns out that generated-source directories are not automatically included in the jar. However, Intellj assumes there are and treats them as such, hence my confusion.
You need to use the Maven build helper plugin to fix this issue, for example:
https://github.com/alexec/javahelp-skeleton/blob/master/pom.xml

IntelliJ automagically mark a target sub directory as "source"

I use IntelliJ since a few months back now for my Java development. I using IntelliJ as IDE and build my projects using Maven. A couple of my Maven projects generates code which my other Maven projects depends upon, the generated code ends up in a target/src-generated directory with "Maven-subdirectories" main/java, main/resource etc. Is it possible to make IntelliJ automagically mark the target/src-generated/main/java directory as source?
Thanks in advance.
Please refer to the IntelliJ IDEA Maven FAQ:
In order to get generated sources automatically imported as source
folders configure corresponding plugins so that they put them into
target/generated-sources/<subdir>, where subdir is any folder name you
prefer. The subdir folder is necessary to distinguish sources from
different tools and also to exclude some special generated sources
(e.g. groovy stubs).
Please note that even if you manually configure
some source folders under target/generated-sources of this folder
itself, IDEA will rewrite them according to your pom.xml.
Any time you
want to generate sources you simply execute the corresponding goal,
bound for generation (usually generate-sources,
generate-test-sources). After that IDEA will pick up new folders and
set them up. Generated test sources/resources should be placed in
target/generated-test-sources/<subdir>.

Resources