Maven replacer plugin - Output directory - maven-2

I'm using maven replacer plugin http://code.google.com/p/maven-replacer-plugin/ with below configuration
<configuration>
<includes>
<include>api/**/*.java</include>
</includes>
<token>#Start.*?#End</token>
<value></value>
<regexFlags>
<regexFlag>DOTALL</regexFlag>
</regexFlags>
<outputBasedir>publicapi</outputBasedir>
</configuration>
My project structure is api/src/main/java/...xxx.java. I want replacer plugin to generate files in this structure - publicapi/src/main/java/...xxx.java whereas with current configuration, plugin generates files like this -publicapi/api/src/main/java/...xxx.java
I don't want "api" directory in between. Is there any property available in this plugin which can solve my problem.

Based on the docs it looks like outputDirectory is what you are searching for.

Related

Maven update jar before packaging in WAR

I have a project where I am packaging a WAR using simple maven-war-plugin. Along with all other dependencies one of the dependency say 'abc.jar' which is getting packaged in war contains a default spring configurations which I would like to update with the custom one before packaging. I have maven profile configured to be activated if following build command applied;
mvn clean install -DframeworkPacakging=XYZ
I am trying to use 'truezip-maven-plugin' to overwrite my custom spring configurations inside in 'abc.jar' present in 'target/aretfacts-id/WEB-INF/lib' but when maven-war-plugin finishes I loose my changes because war plugin takes the file from dependency definition. How can I solve this issue and what are my options?
P.S. Distributing configuration is not desirable as this setup would be used for Embedded Jetty Server running within Eclipse
to prevent inclusion of the original jar file, I would use go for approach suggested on: https://www.mail-archive.com/users#maven.apache.org/msg38537.html
Use <scope>provided</scope> for this dependency to keep it out of the
lib directory.
to include the repackaged one, I'd follow suggestion from: How to make Maven copy resource file into WEB-INF/lib directory?
Try changing the configuration of the maven war plugin to include a webResource:
<configuration>
<webResources>
<resource>
<directory>pathtorepackagedjar</directory>
<includes>
<include>**/abc.jar</include>
<includes>
<targetPath>WEB-INF/lib</targetPath>
</resource>
</webResources>
</configuration>

Maven: How to add files to the root inside an EAR with maven-ear-plugin?

I need to add 2 XML files inside an EAR generated with maven-ear-plugin.
Unfortunately, I haven't seen a way to add an arbitrary file to an EAR; the documentation of the plugin which reads "The EAR plugin supports the following artifacts: ejb, war, jar, ejb-client, rar, ejb3, par, sar, wsr and har". There's nothing for adding a regular file.
org.apache.maven.plugins
maven-ear-plugin
2.3.1
foo
foo
1.4
lib
${parent.groupId}
foo-web
/foo
org.richfaces.framework
richfaces-api
commons-lang
commons-lang
Many thanks in advance.
In maven-ear-plugin 2.4.2 you can use config elements earSourceDirectory, earSourceExcludes and earSourceIncludes to declare extra files to include in the EAR.
By default you simply place those files to ${basedir}/src/main/application folder.
I had the same issue. I had the ejb.properties file under ejbs/src/main/resources and had used earSourceDirectory and earSourceIncludes to pull the file from the ejbs directory to the ear. However, it did not reliably put it in the lib directory. The EJB does not find it in .; it looks for it in the lib directory.
To fix this, I created the src/main/application/lib directory and created a link to the ejb.properties file. I then removed the earSourceDirectory and Includes properties. Now when I do a mvn clean package, it automatically pulls the properties file and puts it in the lib directory of the ear.
Sorry to be late for #wishihadabettername but I had same issue recently, more I couldn't move the files to include because they were in other folder then earSourceDirectory. I read maven-ear-plugin ear:ear documentation and thought about how to move my release folder on the root of ear.
Binds by default to the lifecycle phase: package.
and
workDirectory - Directory that resources are copied to during the build.
Default value is: ${project.build.directory}/${project.build.finalName}.
Then my submodule pom.xml looks like this:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>prepare-package</phase> <!-- before package phase -->
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<!-- Work Directory of Ear plugin -->
<outputDirectory>${project.build.directory}/${project.build.finalName}</outputDirectory>
<resources>
<resource>
<!-- my resource folder -->
<directory>release</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
Hope it help people from now on

maven surefire additionalClasspathElements single test

I have one main and one test config file which is shared by several maven projects. While testing, I am specifying the classpath to the directory where the test config file is.
This works fine if the whole project is tested/build but it doesn't work while running single tests. The config file is not found in the classpath.
here is the config:
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<additionalClasspathElements>
<element>${project.parent.basedir}/conf/test</element>
</additionalClasspathElements>
</configuration>
</plugin>
any ideas why surefire ignores the property for running single tests?
thank you
Shouldn't it be:
<additionalClasspathElements>
<additionalClasspathElement>${project.parent.basedir}/conf/test</additionalClasspathElement>
</additionalClasspathElements>
put the config file in src/test/resources folder and try it .It should automatically be picked up. OR try to put under src/main/resources and use tag to point to that config file in the pom.xml of the project .Make use of 2.5 version of maven-surefire plugin.

How can I merge resource files in a Maven assembly?

I'm using Maven and its assembly plugin to build a distribution package of my project like this:
one project assembles a basic runtime (based on Felix), with the appropriate directories and bundles, in a ZIP file.
third-party libraries are collected in one project each and either converted to OSGi bundles or, if they are already OSGi compatible, they are just copied
my own project consists of several modules that are built into OSGi bundles, too.
Now, I'm adding another project that unpacks the ZIP, drops all the other JARs into the proper directories, and repackages it for distribution. Now, my bundles might contain configuration files that I want to merge into, rather than replacing, identically named ones in the runtime assembly. How do I do that?
The files are plain text (property files), but I might run into a similar situation with XML files later.
Expanding a bit on Juergen's answer for those who stumble on this - the containerDescriptorHandler in the descriptor can take four values (v2.3), these are metaInf-services, file-aggregator, plexus, metaInf-spring. It's a bit buried in the code (found in the package org.apache.maven.plugin.assembly.filter) but it is possible to aggregate config/properties files.
Here's an example descriptor that aggregates the META-INF/services and
named property files located in com.mycompany.actions.
descriptor.xml
<assembly>
...
<containerDescriptorHandlers>
<containerDescriptorHandler>
<handlerName>metaInf-services</handlerName>
</containerDescriptorHandler>
<containerDescriptorHandler>
<handlerName>file-aggregator</handlerName>
<configuration>
<filePattern>com/mycompany/actions/action.properties</filePattern>
<outputPath>com/mycompany/actions/action.properties</outputPath>
</configuration>
</containerDescriptorHandler>
</containerDescriptorHandlers>
....
</assembly>
The file-aggregator can contain a regular expression in the filePattern to match multiple files. The following would match all files names 'action.properties'.
<filePattern>.+/action.properties</filePattern>
The metaInf-services and metaInf-spring are used for aggregating SPI and spring config files respectively whilst the plexus handler will aggregate META-INF/plexus/components.xml together.
If you need something more specialised you can add your own configuration handler by implementing ContainerDescriptorHandler and defining the component in META-INF/plexus/components.xml. You can do this by creating an upstream project which has a dependency on maven-assembly-plugin and contains your custom handler. It might be possible to do this in the same project you're assembling but I didn't try that. Implementations of the handlers can be found in org.apache.maven.plugin.assembly.filter.* package of the assembly source code.
CustomHandler.java
package com.mycompany;
import org.apache.maven.plugin.assembly.filter.ContainerDescriptorHandler;
public class CustomHandler implements ContainerDescriptorHandler {
// body not shown
}
then define the component in /src/main/resources/META-INF/plexus/components.xml
components.xml
<?xml version='1.0' encoding='UTF-8'?>
<component-set>
<components>
<component>
<role>org.apache.maven.plugin.assembly.filter.ContainerDescriptorHandler</role>
<role-hint>custom-handler</role-hint>
<implementation>com.mycompany.CustomHandler</implementation>
<instantiation-strategy>per-lookup</instantiation-strategy>
</component>
</components>
</component-set>
Finally you add this as a dependency on the assembly plugin in the project you wish to assemble
pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.1</version>
<configuration>
<descriptors>
<descriptor>...</descriptor>
</descriptors>
</configuration>
<dependencies>
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>sample-handler</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</plugin>
and define the handlerName in the descriptor
descriptor.xml
...
<containerDescriptorHandler>
<handlerName>custom-handler</handlerName>
</containerDescriptorHandler>
...
The maven-shade-plugin can also create 'uber-jars' and has some resource transforms for handling XML, licences and manifests.
J
Old question but stumbled over it while trying to solve similar problem: Assembly plugin 2.2 has capabilities to merge files: http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html#class_containerDescriptorHandler
e.g. handlerName "metaInf-services" (will concat all META-INF/services files), "metaInf-spring" are the only ones I know of (I personally needed metaInf-services)
I don't know of a robust solution to this problem. But a bit of looking around shows that somebody has created a plugin to merge properties files. By the look of it you need to tell it which files to merge, which is a good thing as you don't want this applied willy nilly.
Assuming you have used dependency-unpack to unpack the zip to a known location, it would be a case of configuring the plugin to merge each pair of properties files and specify the appropriate target location.
You could extend the plugin to handle XML by using something like xmlmerge from EL4J, as described in this Javaworld article.
Ive also created a merge files plugin, in my case i use it to merge SQL files from various projects into a single installer SQL file which can create all the schemas/tables/static data etc for our apps in a single file, http://croche.googlecode.com/svn/docs/maven-merge-files-plugin/0.1/usage.html
https://github.com/rob19780114/merge-maven-plugin (available on maven central) also seems to do the job.
See below for an example configuration
<plugin>
<groupId>org.zcore.maven</groupId>
<artifactId>merge-maven-plugin</artifactId>
<version>0.0.3</version>
<executions>
<execution>
<id>merge</id>
<phase>generate-resources</phase>
<goals>
<goal>merge</goal>
</goals>
<configuration>
<mergers>
<merger>
<target>${build.outputDirectory}/output-file-1</target>
<sources>
<source>src/main/resources/file1</source>
<source>src/main/resources/file2</source>
</sources>
</merger>
<merger>
<target>${build.outputDirectory}/output-file-2</target>
<sources>
<source>src/main/resources/file3</source>
<source>src/main/resources/file4</source>
</sources>
</merger>
</mergers>
</configuration>
</execution>
</executions>

How to add a classpath entry when executing the app with exec plugin

One of the components is looking for the persistence.xml using the java.class.path system property. It is desired to keep this file separately from jars in the /conf folder.
When running the app with exec:exec, classpath is formed from the path to the main jar plus path to every dependency. I can't seem to figure out how to add the /conf entry to the classpath.
My command line looks like this:
mvn exec:exec -Dexec.executable="java" -Dexec.args="-classpath %classpath com.testjar.App"
I tried "arguments" parameter but the execution fails if I try to append anything to %classpath.
I also tried to add a Class-Path entry to the manifest by specifying
<manifestEntries>
<Class-Path>/conf</Class-Path>
</manifestEntries>
in the configuration for maven-jar-plugin, but the entry in the manifest has no effect on the value of java.class.path property.
You may use the element 'resources' in the 'build' section of your POM file. For example
<build>
<resources>
<resource>
<directory>src/main/resources/config</directory>
<includes>
<include>persistence.xml</include>
</includes>
<targetPath>/</targetPath>
</resource>
</resources>
...
</build>
This will copy the persistence.xml into the build output directory, i.e. it will place the persistence.xml on the classpath.