maven-ear-plugin <packagingIncludes> not including project files - java

My ear project has the following structure:
And my pom.xml goes like this:
<plugin>
<artifactId>maven-ear-plugin</artifactId>
<version>2.10</version>
<configuration>
<earSourceIncludes>META-INF/*</earSourceIncludes>
<packagingIncludes>META-INF/*,**/*.war</packagingIncludes>
<version>7</version>
<modules>
<webModule>
<groupId>com.ex</groupId>
<artifactId>one</artifactId>
</webModule>
</modules>
<generateApplicationXml>false</generateApplicationXml>
</configuration>
</plugin>
What I want is to include in my ear file the contents of the folder META-INF, in a similar folder in the root of the ear file called META-INF.
I've tried multiple combinations with earSourceIncludes and packagingIncludes with no success: My ear file has the linked application .war, which is good, and a META-INF folder which doesn't have what I need, but a pregenerated MANIFEST.MF file and a maven folder with the pom instead.
I wonder if I need the earSourceIncludes at all. To be honest, I don't know why it didn't work with just the packagingIncludes parameter.

You could use the maven-resource-plugin to include the content of META-INF folder to root META-INF folder. See below.
Let
Project base directory - ${project.basedir}
EAR root directory - ${project.rootdir}
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/${project.build.finalName}/META-INF</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}/META-INF</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>

Related

Maven dependency plugin - include files and folders

I have a Maven project with a structure:
Project
|---lib
| |---<files and folders I want to include>
|
|---src
| |---<regular files and folders>
|
|---pom.xml
In my pom.xml I have:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies-third-party</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>target/dist/lib/third-party</outputDirectory>
<excludeGroupIds>...</excludeGroupIds>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
And it copies all my maven dependencies to target/dist/lib/third-party directory. How can I also include all files/folders from lib (see in structure above) folder to that location?
Since these files are configuration and properties files i would classify them as resources and would use the maven-resources-plugin to include them.
<resources>
<!-- The resources in the lib folder -->
<resource>
<directory>lib</directory>
<targetPath>${project.build.directory}/dist/lib/third-party</targetPath>
<!-- add this if you want to define parameters in these resources -->
<filtering>true</filtering>
</resource>
<!-- We need to redeclare the project resources again -->
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
use maven-assembly-plugin
It answers exactly your question

How to add multiple .properties files as build artifacts with Maven?

I have a Maven project where I want to have two build artifacts:
The jar file containing the compiled Java source.
A folder containing a number of .properties file.
How can I setup my Maven project to do this? And then, once I've done this, how can I consume them up the dependency graph?
Add a copy-resources goal of the Maven Resources Plugin to your POM.
<project>
...
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>copy-property-files</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/property-files</outputDirectory>
<resources>
<resource>
...
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
...
</build>
...
</project>
I can't understand what you mean exactly by "consume them up the dependency graph".

How to replace web.xml during build with Maven?

I have a problem with maven. i'm alredy using tomcat7-maven-plugin to deploy my war into a tomcat and it works great.
But now I would like to change a file into my generated war before to send it to servlet container. Is it possible?
Particularly I want to change default web.xml with another one.
I've already tried a maven-resources-plugin as showed below:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.2</version>
<executions>
<execution>
<id>default-copy-resources</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<overwrite>true</overwrite>
<outputDirectory>${project.build.directory}/${project.artifactId}/WEB-INF/</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}/src/main/webapp/ext/WEB-INF</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
but this plugin replace web.xml just on target WEB-INF folder, nothing will be changed into war file.
Someone can help me to find the right plugin to achive my purpose?
UPDATE:
I resolved using this: https://stackoverflow.com/a/3298876/2148530
You can define your profile(in your pom.xml) within it you override the maven.war.webxml property :
<properties>
<maven.war.webxml>path/to/your/custom/web.xml</maven.war.webxml>
</properties>
Solution (not mine), taken from question Maven: Customize web.xml of web-app project with Maven War Plugin and webXml parameter.
webXml The path to the web.xml file to use.
Sample usage
<profiles>
<profile>
<id>tomcat</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webXml>${project.basedir}/src/main/webapp/ext/WEB-INF/web.xml</webXml>
</configuration>
</plugin>
...

Specify common resources in a multi-module maven project

Is there any way to share resources between modules of a parent project in Maven? For example, I would like to specify one log4j.properties file for all the modules in a multi-module Maven project.
Generally, I use Eclipse IDE to create the parent project by choosing a general project and then convert it to a Maven project by specifying a packaging of pom. This creates a "clean" project structure without src and etc. folders. I wonder where such a shared resource should be put in this case.
EDIT1: I would like to put the common resources in the parent project.
I'd create one additional "base" module (project), packaging "jar", that contains the common resources in src/main/resources. Then I'd make the other modules depend on that project. Now they see the common resources on their classpaths.
Antoher possibility is to use a remote resource bundle. You would be able to configure it in the parent project. In this example I wanted to copy some files just for tests. If you use this you will need to create the bundle in another project.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-remote-resources-plugin</artifactId>
<version>1.5</version>
<configuration>
<resourceBundles>
<resourceBundle>es.sca:myBundle:1.0.0</resourceBundle>
</resourceBundles>
<attachToMain>false</attachToMain>
<attachToTest>true</attachToTest>
<appendedResourcesDirectory>${basedir}/src/test/resources</appendedResourcesDirectory>
</configuration>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
</execution>
</executions>
</plugin>
Another way, put in your project root pom:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<!-- don't propagate to child poms -->
<!-- this will only execute in root pom -->
<inherited>false</inherited>
<configuration>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
<!-- don't add classifier -->
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugins>
And example of assembly.xml
<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>resources</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.basedir}/resources/</directory>
<outputDirectory/>
<useDefaultExcludes>true</useDefaultExcludes>
<includes>
<include>**</include>
</includes>
</fileSet>
</fileSets>
</assembly>
Assembly plugin will generate artifact and attach it to current reactor, so it will be installed and deployed.
No you can use it as standard dependency event in the same pom.
Important is to trigger assembly (proper phase) before another plugin which will use generated artifact.
Eg. You can have in your root pom, bellow configuration will be propagated to all your module:
<plugin>
<artifactId>some-maven-plugin</artifactId>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>goal</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>your.project.groupid</groupId>
<artifactI>your.project.artifactId</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
You can see this method in project:
https://github.com/s4u/pgp-keys-map resources directory is shared by all module.
Yes, it seems as a possible solution. But I was interested whether it
is possible to specify these resources in the parent project (without
introducing additional module) since the parent project specifies all
the common dependencies and Maven configurations for the child
modules, I think that the parent project is the most suitable place
also for the common resources.
In case of packaging type pom , when goal package specified to manage your shared resources, just add next (check folders) into build section of pom file :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-config-files</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/logconfig</outputDirectory>
<resources>
<resource>
<filtering>false</filtering>
<directory>${project.basedir}/src/main/resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
I think you can just add the resources and/or testResources elements to your pom.
E.g. to access an additional test resource directory add:
<testResources>
<testResource>
<directory>src/test/resources</directory>
</testResource>
<testResource>
<directory>../global/src/test/resources</directory>
</testResource>
</testResources>
see Maven - Override test resource folder
I managed it to work like this:
I create a project/assembly/test/resources/META-INF/persistence.xml file, and add this to my pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-test-persistence-xml-resources</id>
<phase>process-test-sources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>src/</outputDirectory>
<resources>
<resource>
<directory>${project.parent.basedir}/assembly/</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>

getResource() not locating the resource on classpath

I have set up basic maven project in java SE, with a resource:
main
-java
-resources
-config -> database.properties
now since I don't want this resource in the final jar, I define:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<excludes>
<exclude>config/database.properties</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>make-jar-ultimateParser</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/dist</outputDirectory>
<finalName>testApp</finalName>
<archive>
<compress>false</compress>
<!-- Manifest - MainClass & ClassPath -->
<manifest>
<mainClass>aa.bb.Class</mainClass>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
<manifestEntries>
<Class-Path>config/database.properties</Class-Path>
</manifestEntries>
</archive>
</configuration>
</execution>
</executions>
</plugin>
<!-- Copy configuration files -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/dist/config</outputDirectory>
<resources>
<resource>
<directory>src/main/resources/config</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
So the resource is on classpath and the directory with the resource is copied to the final jar.
My manifest looks like this: "Class-Path: config/database.properties"
But I'm not able to read it:
String db = "/config/database.properties";
properties = new Properties();
properties.load(getClass().getResourceAsStream(db));
I tried absolute/relative paths, getClass()/ClassLoader. Nothing. It works flawlessly in NetBeans, but that's about it.
You're misunderstanding what the Maven Resources plugin does. It simply copies resources (perhaps with transformation) into the build output directory. Where they're then included into the build artifact (JAR, WAR, whatever).
To make this work (referencing the JAR's directory using the Class-Path manifest entry), you need to distribute the config file separately. Or reference it using a File, and not bothering with the classpath.
A better approach is to distribute your application as an assembly, which contains the core app, any dependencies, and the configuration file. This would typically be packaged as a ZIPfile, and the user would unzip it to install.
I dont understand why you are excluding it. Maven uses its own resources dir.
Project
|-- pom.xml
-- src
-- main
`-- resources
wich it makes easier the life the programmer. Theres no need to get classpatch or other when you using this way.

Categories

Resources