Why is the Maven JAR plugin not including some resources? - maven

I have an enterprise application which I am in the process of converting from an Ant build to Maven. It's almost completely converted; this is the very last thing I need to fix. The application is packaged as an EAR file which contains two WARs and has a JAR module which provides all of the core functionality of the application.
I'm using the Freemarker templating library to generate, among other things, message bodies for automatic emails sent by the application. Freemarker needs its *.ftl template files to be on the classpath, and since this is core application functionality not specific to one WAR or the other, it needs to be in the JAR.
The Maven module which defines the JAR has the following POM:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<relativePath>../../pom.xml</relativePath>
<groupId>com.company.project</groupId>
<artifactId>projectName</artifactId>
<version>1.8.0</version>
</parent>
<artifactId>core</artifactId>
<packaging>jar</packaging>
<name>Core Application</name>
<profiles>
<!-- snip -->
</profiles>
<dependencies>
<!-- snip -->
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.ftl</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
<plugins>
<!-- snip -->
</plugins>
</build>
</project>
The *.ftl files are located at src/main/resources/template/, with some in a subdirectory within template/. There are other files within src/main/resources -- some .properties and some .xml, some at the root and some under a directory structure.
When I run the package phase on this module (or on the parent), the target/classes directory created as part of the build process contains the template directory, which in turn contains all of the *.ftl, *.xml, and *.properties files with an appropriate directory structure. If I JAR this directory up manually, everything works perfectly.
Here's where this gets weird and I get lost: when maven-jar-plugin creates the JAR, it includes the XML and properties files, but the template directory is completely absent from the JAR and its contents are nowhere to be found.
As you can see above, I tried explicitly including **/*.ftl. It doesn't make a difference; I can exclude the entire "includes" tag and I get the exact same behavior.
I'm using Maven 3.0.5 and maven-jar-plugin 2.4.

I figured out the answer RIGHT after submitting this question.
In a parent POM, I had the following:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<includes>
<include>**/*.class</include>
<include>**/*.jdo</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</configuration>
</plugin>
I added **/*.ftl to the list of includes and now it's working.
EDIT: Better yet, I removed the configuration tag entirely, and it's still working. I think it was a remnant from before I figured out that the .properties files and other things I needed on the classpath needed to be in src/main/resources and not src/main/java.

Related

How does Maven know where input files come from and where output files go during the generate-sources phase?

I am new to Maven. I have a project which includes a grammar definition file that is read by SableCC, which is a parser generator. SableCC reads a grammar file and generates Java source code that, once compiled, creates a parser. The parser is used by the rest of my project.
I don't think my question is a SableCC question, as I've seen similar POM.XML files that use Antlr (another parser generator that is comparable to SableCC) and the <plugin> section of that POM.XML looks nearly the same as my <plugin> section in my POM.XML file.
Here is my project's file structure, identifying those parts that are used by SableCC:
Here is my complete POM.XML file, which I've cobbled together based on the plugin examples I've seen so far (and in particular, the one for SableCC):
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mbm</groupId>
<artifactId>properties</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Properties</name>
<description>Define and process program arguments</description>
<dependencies>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sablecc-maven-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
My question is how does the SableCC plugin know where the input file is and where the 4 categories of generated output Java source files (analysis, lexer, node and parser) should go? I suspect I need to add more Maven "stuff" to my POM file, but I don't know what.
You can see the plugin configurations here:
https://github.com/tychobrailleur/sablecc-maven-plugin
More specifically:
sourceDirectory: Directory containing grammar files. By default, ${basedir}/src/main/sablecc
outputDirectory: Directory containing the generated sources. By default, ${project.build.directory}/generated-sources/sablecc
You can also extract the JAR and see these configurations in the plugin.xml file:
<configuration>
<outputDirectory implementation="java.lang.String">${project.build.directory}/generated-sources/sablecc</outputDirectory>
<timestampDirectory implementation="java.lang.String">${basedir}/target</timestampDirectory>
<sourceDirectory implementation="java.lang.String">${basedir}/src/main/sablecc</sourceDirectory>
<project implementation="org.apache.maven.project.MavenProject">${project}</project>
<staleMillis implementation="int" default-value="0">${lastModGranularityMs}</staleMillis>
</configuration>

Lib folder gets packaged with eclipse plugin jar while building through tycho

I am trying to build eclipse plugin using tycho-compiler-plugin from maven.
I have resolved many bundled dependencies from p2 repo. I have some jar dependencies which are present in bundle-classpath in manifest.mf -
Bundle-ClassPath: .,
lib/test1.jar,
lib/test2.jar
These jars are present in lib folder which is present at root level i.e where pom is present.
POM file looks like -
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>com.test.plugin</artifactId>
<version>0.0.0</version>
<packaging>eclipse-plugin</packaging>
<repositories>
<repository>
<id>Mars</id>
<layout>p2</layout>
<url>file:///E:/repo/eclipseRepo/</url>
</repository>
</repositories>
<build>
<directory>../../../../target</directory>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-maven-plugin</artifactId>
<version>0.25.0</version>
<extensions>true</extensions>
</plugin>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-compiler-plugin</artifactId>
<version>0.25.0</version>
</plugin>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-packaging-plugin</artifactId>
<version>0.25.0</version>
<configuration>
<buildDirectory>../../../../plugin</buildDirectory>
</configuration>
</plugin>
</plugins>
</build>
</project>
Now ti builds eclipse plugin properly , but while packaging it also includes lib folder. Attached jar screenshot -
I want to exclude this lib folder from jar.I have tried configuration in tycho-packaging-jar plugin to exclude it. But not working. How to exclude it?
Using a <directory> or <buildDirectory> outside the current project’s base directory looks fairly non-standard. In fact, I have never had the need to explicit configure either of these – or <sourceDirectory>, for that matter. That’s what the build.properties file is for, which is the default way to configure these things in both Eclipse PDE and Tycho.
From the screenshot it looks like there is no build.properties file present. I would suggest you configure the various locations through its properties rather than through POM elements. Something along the lines of this example, with bin.includes and bin.excludes handling your JAR inclusions.

How to configure POM.xml file to create JAR that would contain two different folders

I am planning to run JAR file from command line to execute my selenium tests.
something like that: "java selenium-0.0.1-snapshot.jar Test1.class"
I am trying to configure POM file that at the build time will result in the JAR file, that would contain classes from 2 locations :
first location is a default - /src/main/java/test
second location - /src/test/java/test;
I was trying the following :
<groupId>com.imapdata</groupId>
<artifactId>selenium</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>selenium</name>
<url>http://maven.apache.org</url>
<build>
<resources>
<resource>
<directory>${basedir}/src/main/java</directory>
</resource>
<resource>
<directory>${basedir}/src/test/java</directory>
</resource>
</resources>
...
</build>
that didn't work well. If anybody can give me a clue on how to do that properly.

jardesc creates a "src" folder in jar file

My jardesc was working great until yesterday when I converted my project to maven.
Now for some reason when I create a jar file containing code and packages: org.javacode and org.resource, I get the following folders in jar file:
org/javacode
src/org/resource
It seem to work fine for java packages but for those packages that contain image resources it creates an additional src folder at the root.
How can I fix this?
Edit 1: Here is the pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Energy3D</groupId>
<artifactId>Energy3D</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.ardor3d</groupId>
<artifactId>ardor3d-jogl</artifactId>
<version>0.9-SNAPSHOT</version>
<type>bundle</type>
</dependency>
<dependency>
<groupId>com.ardor3d</groupId>
<artifactId>ardor3d-lwjgl</artifactId>
<version>0.9-SNAPSHOT</version>
<type>bundle</type>
</dependency>
</dependencies>
</project>
Edit 2:
Also here is the relevant part of jardesc (the gui package is correctly placed in org/concord/energy3d/gui however the images package is mistakenly placed in src/org/concord/energy3d/resources/images when jar file is created):
<javaElement handleIdentifier="=Energy3D/src<org.concord.energy3d.gui"/>
<javaElement handleIdentifier="=Energy3D/src<org.concord.energy3d.resources.images"/>
You are not following the Maven conventions.
Your sources need to be underneath: src/main/java
Your resources need to be underneath: src/main/resources
Your test sources need to be underneath: src/test/java
Your test resources need to be underneath: src/test/resources
Maven's first rule is convention over configuration. You are focusing on the configuration bit, instead of following the convention. If you don't follow the convention, the plugins will not find things where they expect them. This will lead to them not doing what you want, so I recommend you address this properly.
Also, remove this part from your pom.xml:
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>

Generating OSGi bundles distribution with maven-assembly-plugin

I have a multi-module project, where each module is packaged as an OSGi bundle using the Apache Felix maven-bundle-plugin. The whole project is built using a parent POM that lists the above-mentioned modules. Some modules contain configuration resources (e.g. .properties files) that should not be jarred inside the bundles for deployment but rather externalized in a dedicated config folder. My goal is to create a distribution folder (possibly, a zip file) that would look something like this:
my-app-distribution
/bundles
module1-bundle.jar
module2-bundle.jar
etc.
/conf
external1.properties
external2.properties
etc.
where the properties files under the /conf directory are hand-picked files from the individual modules' /target folders. The reason the .properties files need to be picked up from the target folders vs. the src folders is that I am using Maven resource filtering, and the source property files contain ${..} placeholders for environment-specific values. Those placeholders are properly resolved during the build process - per build profiles - and the target/ folders contain actual environment-specific values.
I've done such distribution file manipulations many times - for distributions with executable JARs, etc. In this case I wanted to use the "moduleSets" configuration of the assembly descriptor - it is easy to pull all binaries/jars into a single distribution folder using moduleSet/binary descriptor. It is also easy to exclude certain files from being packaged into an OSGi bundle - in the maven-bundle-plugin. The only issue I am stuck with is creating the /conf distribution folder and collecting the necessary properties files there. I have tried to use "fileSets" inside the "moduleSet/sources" descriptor to include only specific files from **/target of each module, but that didn't seem to work.
Anyone have a suggestion/advice? There's got to be an easy way. Or should I not use at all?
Thanks,
CV
#PetrKozelka I am not sure that extracting configuration files specific to different bundles into a separate module is a good idea. The whole point of OSGi is for bundles to be independent and potentially reusable - both in development and distributions. It only makes sense that - in the source code - the functionality implementation and related configuration files are grouped together. For a particular distribution though I might need to extract some of the files - if there is a requirement for admins to have control of certain parameters. That may be different for a different distribution/application. The assembly configuration may change, but the bundles/sources would stay the same. Also, each bundle may potentially be developed and used separately, not all bundles have to always be part of the same uber project - as you seem to assume. What you are suggesting seems to fall into the same old category of packaging enterprise applications by the type of artifacts (e.g. "model", "services", "dataaccess", "config" etc.), not by functional domain/features. Such approach works ok within a single application/project, but fails on the enterprise level where there is often a need to reuse subsets of vertical components (split by functional domains).
To your point of being dependent on the file layout in the modules, I agree that there should be no such dependency. Files could be hand-picked by their explicit name or naming convention - per very specific distro requirements. (Which is exactly the case I am facing.)
I have actually figured out how to do it more or less elegantly. Posting the solution below in case someone else is looking to solve a similar problem.
SUMMARY
I am using the maven-assembly-plugin to extract the binaries (bundle JARs) from the individual modules and package them in the <my-distribution-folder>/bundles directory. In each module where some resource files should be externalized, I consolidate such files under the /src/main/resources/external directory, and use maven-resources-plugin to copy those resources during the packaging phase to the auto-generated directory in my dedicated distribution module that contains the assembly.xml descriptor file and is also built as part of the top project. I use maven-clean-plugin in the parent POM to clear the contents of the distribution staging directory during the CLEAN phase of the top-level project build.
MAVEN CONFIGURATION
Inside each bundle's module POM that contains resources that need to be externalized I add the following resource management configuration:
<build>
<defaultGoal>install</defaultGoal>
<!--
enable resource filtering for resolving ${...} placeholders with environment-specific values
exclude any files that must be externalized
-->
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>external/*.*</exclude>
</excludes>
</resource>
</resources>
...
<plugins>
<!-- Copies contents of resources/external to dedicated folder defined by property in parent -->
<!-- externalized resources will be packaged according to assembly instructions -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>
${project.parent.basedir}/${externalizableResourcesStageDir}
</outputDirectory>
<resources>
<resource>
<directory>src/main/resources/external</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<!-- builds a JAR file for this bundle -->
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
<Import-Package>*</Import-Package>
<Export-Package>
${project.groupId}.thismodulepackage*;version=${project.version}
</Export-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
where externalizableResourcesStageDir is a property defined in the top/parent POM. In the project, I include a special distribution module with the following structure:
distribution
/ext-resources (target auto-generated dir for external resources from modules)
/src
/assemble
assembly.xml (assembly descriptor)
The assembly.xml file looks like this:
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2
http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>bin</id>
<!-- generate a ZIP distribution -->
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<baseDirectory>/</baseDirectory>
<moduleSets>
<moduleSet>
<!-- Enable access to all projects in the current multi-module build -->
<useAllReactorProjects>true</useAllReactorProjects>
<!-- select projects to include-->
<includes>
<include>myGroupId:myModuleArtifactId1</include>
<include>myGroupId:myModuleArtifactId2</include>
...
</includes>
<!-- place bundle jars under /bundles folder in dist directory -->
<binaries>
<outputDirectory>${artifactId}/bundles</outputDirectory>
<unpack>false</unpack>
</binaries>
</moduleSet>
</moduleSets>
<!-- now take files from ext-resources in this module and place them into dist /conf subfolder-->
<fileSets>
<fileSet>
<directory>ext-resources</directory>
<outputDirectory>${artifactId}/conf/</outputDirectory>
<includes>
<include>*</include>
</includes>
</fileSet>
</fileSets>
</assembly>
The distribution module's POM would look like this:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>myGroupId</groupId>
<artifactId>parentArtifactId</artifactId>
<version>...</version>
</parent>
<groupId>myGroupId</groupId>
<artifactId>distribution</artifactId>
<version>...</version>
<packaging>pom</packaging>
<name>Distribution</name>
<description>This module creates the <MyProject> Distribution Assembly</description>
<url>http:...</url>
<!-- NOTE: These dependency declarations are only required to sort this project to the
end of the line in the multi-module build.
-->
<dependencies>
<dependency>
<groupId>myGroupId</groupId>
<artifactId>myModuleArtifactId1</artifactId>
<version>${project.version}</version>
</dependency>
...
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>dist-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>src/assemble/assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
The parent POM would list all the bundle modules, plus the distribution module and also define the assembly plugin:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>myGroupId</groupId>
<artifactId>myParentId</artifactId>
<version>...</version>
<packaging>pom</packaging>
<properties>
...
<!-- directory where build may place any sub-modules' resources that should be externalized -->
<!-- those resources may be picked up by maven-assembly-plugin and packaged properly for distribution -->
<externalizableResourcesStageDir>
esb-distribution/ext-resources
</externalizableResourcesStageDir>
</properties>
<!-- all project modules (OSGi bundles + distribution) -->
<modules>
<module>bundle-module1</module>
<module>bundle-module2</module>
...
<module>distribution</module>
</modules>
<dependencyManagement>
<dependencies>
...
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<!--
Cleans contents of the folder where the externalized resources will be consolidated
Each module adds its own external files to the distribution directory during its own build
-->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>clean-ext-resources</id>
<phase>clean</phase>
</execution>
</executions>
<configuration>
<filesets>
<fileset>
<directory>${externalizableResourcesStageDir}</directory>
<includes>
<include>*.*</include>
</includes>
<followSymlinks>false</followSymlinks>
</fileset>
</filesets>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<configuration>
<descriptors>
<descriptor>src/assemble/assembly.xml</descriptor>
</descriptors>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
NOTE: We've also made sure that the externalized resource files are excluded from being packaged inside the individual bundle JARs (see the resources section of the module POM.) The resulting unzipped distribution will look like this:
my-app-distribution
/bundles
module1-bundle.jar
module2-bundle.jar
etc.
/conf
external1.properties
external2.properties
etc.

Resources