Why does Maven include change the structure of my required files - maven

I want to include the directory "dirA" into my .jar
The structure is the following
-mvnModule
-dirA
-dirB
someFiles
-dirC
someFiles
-src
.......
When I do this:
<resources>
<resource>
<directory>dirA/*</directory>
</resource>
</resources>
Why is it that if I investigate the .jar-File, all Files are just under the root (mvnModule). Maven removes my structure, which leads to FileNotFoundExcpetions at runtime. I do not want to change all the paths in the code...is there a way to include it with the correct/original structure?

Related

Maven-assembly-plugin: add multiple files of multiple directories to one output directory

Anyone an idea of how to add multiple files of multiple directories to one output directory without sub-directories using the maven assembly plugin.
Imagine following structure:
prjdir
|-dir1
|-target
|-file1
|-dir2
|-target
|-file2
...
|-dirN
|-target
|-fileN
I can use a fileSet to add the files ie.:
<fileSet>
<directory>../prjdir</directory>
<outputDirectory>myoutput</outputDirectory>
<includes>
<include>*/target/file*</include>
</includes>
</fileSet>
Which then creates the myoutput directory like:
myoutput
|-dir1
|-target
|-file1
|-dir2
|-target
|-file2
...
|-dirN
|-target
|-fileN
But what the hell I should do if I want only the files on myoutput without the directory structure of the source...
myoutput
|-file1
|-file2
...
|-fileN
This is only an simplified example, which means I need to do it a generic way.
Can't use <files><file> and also can't list every file explicitly, as they are generated dynamically without knowing the name beforehand ...
Thanks in advance for you support ...
Cheers m_n

Maven Filtering Include/Exclude Understanding

Hello I'm trying to understand the filtering include/exclude in Maven as I am a Maven noob. I was looking up how to read the version from the pom file in JAVA and found the solution but I have a few questions about the filtering. I understand the filtering with include and/or exclude in the same resource block, but I'm not understanding what is happening when you include/exclude in different resource blocks of the same file/folder.
My folder structure with simplified RandomFolder and its contents:
src
├── main
│ ├── resources
│ ├── RandomFolder
│ ├──aFile.dll
│ ├──someFile.txt
│ └── pom.properties
pom.properties file contains:
version=${revision}
#1
This is how I currently have it (simplified) as a working solution. I understand it's filtering the pom.properties file and replacing '${revision}' with '1.0.0' aka 'version=1.0.0'. Would the pom.properties file be filtered (aka 'version=1.0.0') when mvn package is ran and a jar file is generated? I would assume so, but wanted to make sure.
I also understand that typically you wouldn't specify the filename in the filtering and instead it would be **/*.properties, but since this was the only properties file I think it's easier and cleaner to have the filename and it shouldn't cause any issues (let me know if I am wrong on this assumption).
...
<properties>
<revision>1.0.0</revision>
</properties>
...
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/pom.properties</include>
</includes>
</resource>
</resources>
...
</build>
#2
I believe this is the way tutorial websites would normally represent include and exclude in a resource block. Really unnecessary in this example as it filters everything besides the pom.properties (found and tested that the excludes block overwrites the includes block no matter the order).
No question here. Just wanted to state that I know about this approach.
...
<properties>
<revision>1.0.0</revision>
</properties>
...
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/pom.properties</include>
</includes>
<excludes>
<exclude>**/pom.properties</exclude>
</excludes>
</resource>
</resources>
...
</build>
#3
Why does this work? I would assume its filtering the file first so that the file reads '1.0.0' then is not filtering, but I want to make sure. This stems from a similar solution I found to get the pom version (see #5 further down). I tested this and it filters the pom.properties file.
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/pom.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<excludes>
<exclude>**/pom.properties</exclude>
</excludes>
</resource>
</resources>
...
</build>
#4
The flip flop of #2 with the exclude block before the include block, which seems kinda unnecessary. I would assume its not filtering the file first then filtering the file so that the file reads '1.0.0', but I want to make sure. I tested this and it filters the pom.properties file.
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<excludes>
<exclude>**/pom.properties</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/pom.properties</include>
</includes>
</resource>
</resources>
...
</build>
#5
This I don't really know. This was a top solution found for: Maven resource filtering exclude
And the link provided in that solution I feel doesn't explain the reasoning for its' solution: https://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html
So is it filtering everything except the pom.properties file but then not filter everything including the pom.properties file? Why? What's the purpose of this? Are they both the same thing? Or a double negative? Does it impact what appears in the build/package?
This 'solution' did not filter the pom.properties file when I tested it.
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>**/pom.properties</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<includes>
<include>**/pom.properties</include>
</includes>
</resource>
</resources>
...
</build>
Please let me know if you need additional information. I would really appreciate it if you can go through each section (#1 to #5) and answer them. Thank you in advance!
Solution
Robert Scholte's response helped me understand that filtering also copied over files. This is something I did not understand before as I thought it was just modifying the file(s) in the desired folder(s).
With additional help with understanding exclusions from:
http://www.avajava.com/tutorials/lessons/how-do-i-exclude-particular-resources-from-being-processed.html
I was able to answer my questions.
#1. Still not sure, but I believe the pom.properties file would be filtered in jar.
#2. N/A. No question to answer.
#3. For #3, #4, #5 I created a test.properties in same folder as pom.properties and contains the same contents.
Testing required me to use command: mvn clean install -DskipTests to visually see the differences between #3, #4 and #5.
This would be the result after creating the test.properties file and running the above command (without the block in pom file) for a visual reference:
src
├── main
│ ├── resources
│ ├── RandomFolder
│ ├──aFile.dll
│ ├──someFile.txt
│ └── pom.properties
target
├── classes
│ ├── MultipleGeneratedFolders
│ ├── RandomFolder
│ ├──aFile.dll
│ ├──someFile.txt
│ ├── pom.properties
│ └── test.properties
So for #3, it filtered only the pom.properties and moved to a '/target/classes/' path (same level as 'src'). But because of the second resource block, it copies over all the contents in the RandomFolder and the test.properties file to '/target/classes/' path without filtering them!
#4. Exact same result as #3. Everything is copied over '/target/classes/', but only pom.properties file is filtered.
#5. Here is the interesting one. The first resource block with filtering true and excluding pom.properties is filtering everything (since there is no , it defaults to everything) and then copies to '/target/classes/' path. So this means the test.properties file gets filtered! The second resource block with filtering false and including pom.properties is not filtering the pom.properties file (so it leaves as raw text) then copies over to '/target/classes/' path. Basically, everything besides pom.properties gets filtered and copied over to '/target/classes/' path.
Summary of Conclusions
In short, filtering refers to replacing the variables in files with the properties block in the pom file, such as ${revision}. Filtering does not require or (if neither are specified it defaults to including all files) and copies selected files to the target folder.
If you filter (set to TRUE) and only a specific pattern or file, it will only filter the selected files and copy them to the target folder.
If you filter (set to TRUE) and only , it will filter everything except for the specified pattern or file and copy only the filtered files to the target folder.
If you don't filter (set to FALSE) and only a specific pattern or file, it will only copy the selected files to the target folder without filtering them.
If you don't filter (set to FALSE) and only a specific pattern or file, it will copy all files except the selected files to the target folder without filtering them.
(I could be wrong in my conclusions so take it may not be completely accurate, but 'good enough for government work' as they say)
You can run mvn process-resources -X to see more details about which files are copied.
But this is how it works:
per resource-block the fileset to copy is selected by choosing all includes (default is all), minus all excludes (default none).
If filtering is set to true, the placehoders in these files will be replaced, otherwise they will be copied as is.

Concat messages.properties with maven

We have some default message files and we want to be able to customize them by maven profiles.
Therefore we configured two resource entries
<resource>
<directory>${basedir}/resources</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>${basedir}/profiles/${additionalWebcontent}/java</directory>
<filtering>true</filtering>
</resource>
currently the files from ${basedir}/resources are overwritten by files from ${basedir}/profiles/${additionalWebcontent}/java.
Is it possible to append the content from the files from ${basedir}/profiles/${additionalWebcontent}/java to the content of the files from ${basedir}/resources ?
An example content would be
${basedir}/resources/messages/messages.properties
greeting=Hello World!
logout=Logout
login=Logout
${basedir}/profiles/${additionalWebcontent}/java/messages/messages.properties
greeting=Hello Folks!
Expected Result:
${project.build.directory}/classes/messages/messages.properties
greeting=Hello World!
logout=Logout
login=Logout
greeting=Hello Folks!

sass-maven-plugin using add_import_path option

Is it a way to use add_import_path option in maven plugin configuration or I had to use compassConfigFile ?
Setting buildDirectory to the path where are import files is a solution ?
I use this plugin :
http://www.geodienstencentrum.nl/sass-maven-plugin/update-stylesheets-mojo.html
Error :
failed: File to import not found or unreadable: theme.
Load paths:
And I don't have the directory I need in Load paths
I tried
<sassOptions>
<load_paths>${project.basedir}/src/main/webapp/theme/colors/red</load_paths>
</sassOptions>
without success
You need to specify the load_paths as an array, this seems to work for me:
<load_paths>['${project.basedir}/src/main/webapp/theme/colors/red']</load_paths>
The thing that makes it work is the [ ] around the string, making it an array it seems in the ruby-end of things, so the sass gem gets it the way it wants
So I found a solution
<configuration>
<sassOptions>
<always_update>true</always_update>
<style>${sass.OutputStyle}</style>
</sassOptions>
<useCompass>true</useCompass>
<compassConfigFile>${project.basedir}/src/main/webapp/style/config.rb</compassConfigFile>
<resources>
<resource>
<source>
<directory>${project.basedir}/src/main/webapp/style/themes/colors/cyan</directory>
</source>
<destination>${project.build.directory}/${project.build.finalName}/style/themes/colors/cyan</destination>
</resource>
<resource>
<source>
<directory>${project.basedir}/src/main/webapp/style/themes</directory>
</source>
<destination>${project.build.directory}/${project.build.finalName}/style/themes/colors/cyan</destination>
</resource>
</resources>
The first resource load scss files for import in the second resource imported files are avaible. First resource generate nothing in destination but node is needed.

Maven: Need to copy images into Javadoc, LICENSE/NOTICE files into META-INF

I've been trying to accomplish two things that I figured would be pretty trivial with Maven (I'm using 3.0.3 and the latest versions of all the plugins), but no matter what I do I can't seem to make it work. I need to:
Copy all of the files (they're images) from build/javadoc-css/resources/ (relative to project root) to the resources/ directory in the Javadoc output. The stylesheet I'm using references images in that resources directory, and I need to copy the associated images into that resources/ directory. So far, it appears you can only copy files in doc-files/ directories. I'm hoping I'm missing something, because that is seriously lacking.
Copy all of the files (LICENSE.txt and NOTICE.txt, which I figured were common things that everyone needs to copy) from build/jar/META-INF to the META-INF directories in all of my output jars. (Note: I don't want to put the META-INF directory with my LICENSE and NOTICE files in the source directory, because I have many modules, and I don't want many copies of these files.)
To accomplish #1, I have focused my efforts on the <javadocDirectory> and <docfilessubdirs> tags, but these only work with doc-files/ directories it appears.
For #2, I've tried several variations of the <resource> tag:
Variation #1:
<resource>
<directory>build/jar/META-INF</directory>
<targetPath>META-INF</targetPath>
<includes>
<include>LICENSE.txt</include>
<include>NOTICE.txt</include>
</includes>
</resource>
Variation #2:
<resource>
<directory>build/jar</directory>
<targetPath>META-INF</targetPath>
<includes>
<include>META-INF</include>
</includes>
</resource>
Variation #3:
<resource>
<directory>build/jar/META-INF</directory>
<targetPath>META-INF</targetPath>
<includes>
<include>**</include>
</includes>
</resource>
No errors or files in the wrong place. It's just like my resource tags weren't even there. Nothing happens.
I would suggest to put the resources for javadoc into the appropriate folder which is in Maven:
yourproject
|-- src
|-- main
|-- java
| |-- org
| |-- apache
| |-- myapp
| `-- App.java
| `-- package-info.java
|-- javadoc
`-- overview.html
|-resources
|-- org
|-- apache
|-- myapp
`-- package.html
|-- doc-files
`-- app.png

Resources